CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

BadDataException will corrupt CsvParser instance

Open smbecker opened this issue 2 years ago • 0 comments

Describe the bug If a BadDataException is thrown when accessing a record, it will cause the CsvParser instance to no longer be usable because isProcessingField is not reset appropriately. This line should either be in a try/finally to ensure that it is reset regardless of an exception, or it should be reset when reading the next record.

To Reproduce Read CSV input that contains an invalid record (i.e. has a line feed in a quoted field when LineBreakInQuotedFieldIsBadData = true). This will result in a BadDataException. Future calls to CsvParser.Read will be successful but any future attempt to access CsvParser.Record or CsvParser[index] will fail because isProcessingField is still true from the previous read.

Expected behavior Future successful calls to CsvParser.Read should allow the records to be read.

As a work-around, I grabbed the isProcessingField through reflection and reset it prior to calling CsvParser.Read. I'm happy to submit a PR to reset this field if that would be helpful.

smbecker avatar Feb 07 '23 13:02 smbecker