From a1ad26d1102880d2124d764aa0a80707f95dffee Mon Sep 17 00:00:00 2001 From: Ivo Gosemann Date: Thu, 4 May 2023 19:19:09 +0200 Subject: [PATCH] fixes csv parse errors being silently ignored --- rows.go | 8 ++++++-- rows_test.go | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/rows.go b/rows.go index 941544b..24b1bcd 100644 --- a/rows.go +++ b/rows.go @@ -4,6 +4,7 @@ import ( "bytes" "database/sql/driver" "encoding/csv" + "errors" "fmt" "io" "strings" @@ -208,8 +209,11 @@ func (r *Rows) FromCSVString(s string) *Rows { for { res, err := csvReader.Read() - if err != nil || res == nil { - break + if err != nil { + if errors.Is(err, io.EOF) { + break + } + panic(fmt.Sprintf("Parsing CSV string failed: %s", err.Error())) } row := make([]driver.Value, len(r.cols)) diff --git a/rows_test.go b/rows_test.go index ef17521..c2a9ebe 100644 --- a/rows_test.go +++ b/rows_test.go @@ -461,6 +461,15 @@ func TestCSVRowParser(t *testing.T) { } } +func TestCSVParserInvalidInput(t *testing.T) { + defer func() { + recover() + }() + _ = NewRows([]string{"col1", "col2"}).FromCSVString("a,\"NULL\"\"") + // shouldn't reach here + t.Error("expected panic from parsing invalid CSV") +} + func TestWrongNumberOfValues(t *testing.T) { // Open new mock database db, mock, err := New()