]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/csv: disallow quote for use as Comma
authorJoe Tsai <joetsai@digital-static.net>
Thu, 8 Mar 2018 22:39:43 +0000 (14:39 -0800)
committerJoe Tsai <thebrokentoaster@gmail.com>
Fri, 9 Mar 2018 00:33:43 +0000 (00:33 +0000)
'"' has special semantic meaning that conflicts with using it as Comma.

Change-Id: Ife25ba43ca25dba2ea184c1bb7579a230d376059
Reviewed-on: https://go-review.googlesource.com/99696
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/encoding/csv/reader.go
src/encoding/csv/reader_test.go
src/encoding/csv/writer_test.go

index 2efc7ad0943082246e5990b41e3feeb0254a4b66..a2fd4c0970d17a83fa87843f4aa04ae721b60437 100644 (file)
@@ -91,7 +91,7 @@ var (
 var errInvalidDelim = errors.New("csv: invalid field or comment delimiter")
 
 func validDelim(r rune) bool {
-       return r != 0 && r != '\r' && r != '\n' && utf8.ValidRune(r) && r != utf8.RuneError
+       return r != 0 && r != '"' && r != '\r' && r != '\n' && utf8.ValidRune(r) && r != utf8.RuneError
 }
 
 // A Reader reads records from a CSV-encoded file.
index 1fc69f9ab88314eb45976762ac1fc5f9f7b4b3c2..5121791cb36005736417730b981a386d38494e86 100644 (file)
@@ -359,6 +359,10 @@ x,,,
                Error: errInvalidDelim,
        }, {
                Name:  "BadComma3",
+               Comma: '"',
+               Error: errInvalidDelim,
+       }, {
+               Name:  "BadComma4",
                Comma: utf8.RuneError,
                Error: errInvalidDelim,
        }, {
index 99bc84e998a2a2f37cabf6e7c74f09fbb2678e7a..011f01c172ac10ea6a162a67fc70c0e4b5db635f 100644 (file)
@@ -13,7 +13,9 @@ import (
 var writeTests = []struct {
        Input   [][]string
        Output  string
+       Error   error
        UseCRLF bool
+       Comma   rune
 }{
        {Input: [][]string{{"abc"}}, Output: "abc\n"},
        {Input: [][]string{{"abc"}}, Output: "abc\r\n", UseCRLF: true},
@@ -41,6 +43,9 @@ var writeTests = []struct {
        {Input: [][]string{{`\.`}}, Output: "\"\\.\"\n"},
        {Input: [][]string{{"x09\x41\xb4\x1c", "aktau"}}, Output: "x09\x41\xb4\x1c,aktau\n"},
        {Input: [][]string{{",x09\x41\xb4\x1c", "aktau"}}, Output: "\",x09\x41\xb4\x1c\",aktau\n"},
+       {Input: [][]string{{"a", "a", ""}}, Output: "a|a|\n", Comma: '|'},
+       {Input: [][]string{{",", ",", ""}}, Output: ",|,|\n", Comma: '|'},
+       {Input: [][]string{{"foo"}}, Comma: '"', Error: errInvalidDelim},
 }
 
 func TestWrite(t *testing.T) {
@@ -48,9 +53,12 @@ func TestWrite(t *testing.T) {
                b := &bytes.Buffer{}
                f := NewWriter(b)
                f.UseCRLF = tt.UseCRLF
+               if tt.Comma != 0 {
+                       f.Comma = tt.Comma
+               }
                err := f.WriteAll(tt.Input)
-               if err != nil {
-                       t.Errorf("Unexpected error: %s\n", err)
+               if err != tt.Error {
+                       t.Errorf("Unexpected error:\ngot  %v\nwant %v", err, tt.Error)
                }
                out := b.String()
                if out != tt.Output {