Skip to content

Commit

Permalink
Merge pull request #315 from IvoGoman/fix/invalidcsvstring
Browse files Browse the repository at this point in the history
fixes csv parse errors being silently ignored
  • Loading branch information
fatelei authored May 10, 2023
2 parents 44e746a + a1ad26d commit 3476f31
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 3476f31

Please sign in to comment.