Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes csv parse errors being silently ignored #315

Merged
merged 1 commit into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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