From 3fe27292d05f035a3fccb6a324484118f6518f74 Mon Sep 17 00:00:00 2001 From: Bolt4243 <277300782+Bolt4243@users.noreply.github.com> Date: Wed, 9 Sep 2026 11:16:06 -0400 Subject: [PATCH 1/2] Reject CSV rows with missing columns --- rows.go | 3 +++ rows_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/rows.go b/rows.go index 01ea811..497b07f 100644 --- a/rows.go +++ b/rows.go @@ -215,6 +215,9 @@ func (r *Rows) FromCSVString(s string) *Rows { } panic(fmt.Sprintf("Parsing CSV string failed: %s", err.Error())) } + if len(res) != len(r.cols) { + panic(fmt.Sprintf("Expected number of values to match number of columns: expected %d, actual %d", len(r.cols), len(res))) + } row := make([]driver.Value, len(r.cols)) for i, v := range res { diff --git a/rows_test.go b/rows_test.go index c2d921a..beb333f 100644 --- a/rows_test.go +++ b/rows_test.go @@ -474,6 +474,17 @@ func TestCSVParserInvalidInput(t *testing.T) { t.Error("expected panic from parsing invalid CSV") } +func TestCSVParserRejectsShortRecord(t *testing.T) { + defer func() { + if r := recover(); r != nil { + return + } + t.Error("expected panic for a CSV record with too few values") + }() + + NewRows([]string{"order_id", "status", "created_at"}).FromCSVString("INV-2026-0908,paid") +} + func TestWrongNumberOfValues(t *testing.T) { // Open new mock database db, mock, err := New() From bf4d63b0ccaecbec0b7488c8171f905763c3b151 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Wed, 9 Sep 2026 11:26:35 -0400 Subject: [PATCH 2/2] Check the CSV cardinality panic in the regression test --- rows_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rows_test.go b/rows_test.go index beb333f..34df4d3 100644 --- a/rows_test.go +++ b/rows_test.go @@ -475,11 +475,11 @@ func TestCSVParserInvalidInput(t *testing.T) { } func TestCSVParserRejectsShortRecord(t *testing.T) { + const expectedPanic = "Expected number of values to match number of columns: expected 3, actual 2" defer func() { - if r := recover(); r != nil { - return + if r := recover(); r != expectedPanic { + t.Fatalf("expected panic %q, got %v", expectedPanic, r) } - t.Error("expected panic for a CSV record with too few values") }() NewRows([]string{"order_id", "status", "created_at"}).FromCSVString("INV-2026-0908,paid")