fix(writer): escape the escape byte when double_quote is disabled - #432
Open
jaideeppyne wants to merge 1 commit into
Open
fix(writer): escape the escape byte when double_quote is disabled#432jaideeppyne wants to merge 1 commit into
jaideeppyne wants to merge 1 commit into
Conversation
When `double_quote` is disabled, the writer escapes quote characters with the escape character (default `\`). However, it did not escape occurrences of the escape character itself in field data. Because the reader treats the escape character inside a quoted field as escaping the following byte, any escape character present in the data was silently consumed on read-back, corrupting fields (e.g. `C:\path\to` round-tripped to `C:pathto`, and a field consisting of a lone `\` swallowed the closing quote and following delimiter, mangling the whole record). The writer already force-quotes fields containing the escape character (`requires_quotes[escape] = true`), so the fix is to also double the escape byte inside `csv_core::write::quote` when `double_quote` is false, making the escaping self-consistent and the write/read round-trip lossless. Adds regression tests at both the `csv-core` and `csv` levels.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
double_quoteis disabled, the writer escapes quote characters with the escape character (default\), but it did not escape occurrences of the escape character itself in field data. Because the reader treats the escape character inside a quoted field as escaping the following byte, any escape character in the data was silently consumed on read-back.Repro (current
master):A lone
\field is worse: it swallows the closing quote and the following delimiter, mangling the whole record.The writer already force-quotes fields containing the escape byte (
requires_quotes[escape] = true), so the fix is to also double the escape byte insidecsv_core::write::quotewhendouble_quoteis false, making the escaping self-consistent and the round-trip lossless. The default (double_quote = true) path is unchanged. Adds regression tests at thecsv-coreandcsvlevels.