diff --git a/zio/csvio/preprocess.go b/zio/csvio/preprocess.go index dca3fd0b15..140dbd0f88 100644 --- a/zio/csvio/preprocess.go +++ b/zio/csvio/preprocess.go @@ -79,7 +79,7 @@ func (p *preprocess) parseField() ([]byte, error) { ending := []byte{c} if hasstr { // If field had quotes wrap entire field in quotes. - if last := len(p.scratch) - 1; p.scratch[last] == '\r' { + if last := len(p.scratch) - 1; last > 0 && p.scratch[last] == '\r' { ending = []byte("\r\n") p.scratch = p.scratch[:last] } diff --git a/zio/csvio/preprocess_test.go b/zio/csvio/preprocess_test.go index 84638390e9..444686cda7 100644 --- a/zio/csvio/preprocess_test.go +++ b/zio/csvio/preprocess_test.go @@ -12,12 +12,18 @@ import ( func TestPreprocess(t *testing.T) { const input = ` +,, +"","","" +"""","""","""" field1,"field"2,field"3" my friend field4,"field"5 with "multiple" quotes "to" escape,field6 """,""",""" has a couple "" embedded quotes and a , comma",""" """ x,"hello, "" world , " foo,y` const expected = ` +,, +"","","" +"""","""","""" field1,"field2","field3 my friend" field4,"field5 with multiple quotes to escape",field6 """,""",""" has a couple "" embedded quotes and a , comma",""" """