Skip to content

Commit

Permalink
fixes csv parse errors being silently ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
IvoGoman committed May 4, 2023
1 parent 44e746a commit a1ad26d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"database/sql/driver"
"encoding/csv"
"errors"
"fmt"
"io"
"strings"
Expand Down Expand Up @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit a1ad26d

Please sign in to comment.