Skip to content

Commit a1ad26d

Browse files
committed
fixes csv parse errors being silently ignored
1 parent 44e746a commit a1ad26d

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

rows.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"database/sql/driver"
66
"encoding/csv"
7+
"errors"
78
"fmt"
89
"io"
910
"strings"
@@ -208,8 +209,11 @@ func (r *Rows) FromCSVString(s string) *Rows {
208209

209210
for {
210211
res, err := csvReader.Read()
211-
if err != nil || res == nil {
212-
break
212+
if err != nil {
213+
if errors.Is(err, io.EOF) {
214+
break
215+
}
216+
panic(fmt.Sprintf("Parsing CSV string failed: %s", err.Error()))
213217
}
214218

215219
row := make([]driver.Value, len(r.cols))

rows_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ func TestCSVRowParser(t *testing.T) {
461461
}
462462
}
463463

464+
func TestCSVParserInvalidInput(t *testing.T) {
465+
defer func() {
466+
recover()
467+
}()
468+
_ = NewRows([]string{"col1", "col2"}).FromCSVString("a,\"NULL\"\"")
469+
// shouldn't reach here
470+
t.Error("expected panic from parsing invalid CSV")
471+
}
472+
464473
func TestWrongNumberOfValues(t *testing.T) {
465474
// Open new mock database
466475
db, mock, err := New()

0 commit comments

Comments
 (0)