Commit 9fbc06e6 authored by Justin Nuß's avatar Justin Nuß Committed by Ian Lance Taylor

encoding/csv: preserve \r\n in quoted fields

The parser mistakenly assumed it could always fold \r\n into \n, which
is not true since a \r\n inside a quoted fields has no special meaning
and should be kept as is.

Fix this by not folding \r\n to \n inside quotes fields.

Fixes #21201

Change-Id: Ifebc302e49cf63e0a027ee90f088dbc050a2b7a6
Reviewed-on: https://go-review.googlesource.com/52810Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent f9cf8e5a
...@@ -233,6 +233,13 @@ func (r *Reader) readRune() (rune, error) { ...@@ -233,6 +233,13 @@ func (r *Reader) readRune() (rune, error) {
return r1, err return r1, err
} }
// readRawRune works the same way as readRune, but does not fold \r\n to \n.
func (r *Reader) readRawRune() (rune, error) {
r1, _, err := r.r.ReadRune()
r.column++
return r1, err
}
// skip reads runes up to and including the rune delim or until error. // skip reads runes up to and including the rune delim or until error.
func (r *Reader) skip(delim rune) error { func (r *Reader) skip(delim rune) error {
for { for {
...@@ -351,7 +358,9 @@ func (r *Reader) parseField() (haveField bool, delim rune, err error) { ...@@ -351,7 +358,9 @@ func (r *Reader) parseField() (haveField bool, delim rune, err error) {
// quoted field // quoted field
Quoted: Quoted:
for { for {
r1, err = r.readRune() // use readRawRune instead of readRune to preserve \r\n
// in quotes fields.
r1, err = r.readRawRune()
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
if r.LazyQuotes { if r.LazyQuotes {
......
...@@ -284,6 +284,13 @@ x,,, ...@@ -284,6 +284,13 @@ x,,,
Line: 2, Line: 2,
Column: 2, Column: 2,
}, },
{ // issue 21201
Name: "CRLFInQuotedField",
Input: "\"Hello\r\nHi\"",
Output: [][]string{
{"Hello\r\nHi"},
},
},
} }
func TestRead(t *testing.T) { func TestRead(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment