From 0add9a4dcfb4cbc0d99cd168752bd1bd641ce8e2 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Wed, 7 Mar 2018 14:14:19 -0800 Subject: encoding/csv: avoid mangling invalid UTF-8 in Writer In the situation where a quoted field is necessary, avoid processing each UTF-8 rune one-by-one, which causes mangling of invalid sequences into utf8.RuneError, causing a loss of information. Instead, search only for the escaped characters, handle those specially and copy everything else in between verbatim. This symmetrically matches the behavior of Reader. Fixes #24298 Change-Id: I9276f64891084ce8487678f663fad711b4095dbb Reviewed-on: https://go-review.googlesource.com/99297 Run-TryBot: Joe Tsai TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/encoding/csv/writer_test.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/encoding/csv/writer_test.go') diff --git a/src/encoding/csv/writer_test.go b/src/encoding/csv/writer_test.go index 8ddca0abe0..99bc84e998 100644 --- a/src/encoding/csv/writer_test.go +++ b/src/encoding/csv/writer_test.go @@ -39,6 +39,8 @@ var writeTests = []struct { {Input: [][]string{{"a", "a", ""}}, Output: "a,a,\n"}, {Input: [][]string{{"a", "a", "a"}}, Output: "a,a,a\n"}, {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"}, } func TestWrite(t *testing.T) { -- cgit v1.3-5-g9baa