From d76540a83f82f9fd4b604f0080ecbf3f5a52b60b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 10 Jan 2025 10:25:15 +0100 Subject: [PATCH 1/3] mysql: Fix ColumnNumber() panic with nil Resultset --- mysql/resultset.go | 3 +++ mysql/resultset_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+) create mode 100644 mysql/resultset_test.go diff --git a/mysql/resultset.go b/mysql/resultset.go index eee819aee..b56628587 100644 --- a/mysql/resultset.go +++ b/mysql/resultset.go @@ -87,6 +87,9 @@ func (r *Resultset) RowNumber() int { } func (r *Resultset) ColumnNumber() int { + if r == nil { + return 0 + } return len(r.Fields) } diff --git a/mysql/resultset_test.go b/mysql/resultset_test.go new file mode 100644 index 000000000..b844a2127 --- /dev/null +++ b/mysql/resultset_test.go @@ -0,0 +1,10 @@ +package mysql + +import "testing" + +func TestColumnNumber(t *testing.T) { + r := Result{} + // Make sure ColumnNumber doesn't panic if ResultSet is nil + // https://github.com/go-mysql-org/go-mysql/issues/964 + r.ColumnNumber() +} From a37e609a7997fbec792d153c43ace7131e5fa3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 10 Jan 2025 10:30:29 +0100 Subject: [PATCH 2/3] Add more comments --- mysql/resultset.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mysql/resultset.go b/mysql/resultset.go index b56628587..0283a0382 100644 --- a/mysql/resultset.go +++ b/mysql/resultset.go @@ -79,6 +79,8 @@ func (r *Resultset) Reset(fieldsCount int) { } } +// RowNumber is returning the number of rows in the Resultset +// For a nil ResultSet 0 is returned func (r *Resultset) RowNumber() int { if r == nil { return 0 @@ -86,6 +88,8 @@ func (r *Resultset) RowNumber() int { return len(r.Values) } +// ColumnNumber is returning the number of fields in the Resultset +// For a nil ResultSet 0 is returned func (r *Resultset) ColumnNumber() int { if r == nil { return 0 From 0914121ac82075e8eddf86cfdfcf4e450fbe5282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Fri, 10 Jan 2025 10:41:40 +0100 Subject: [PATCH 3/3] Fix comments --- mysql/resultset.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mysql/resultset.go b/mysql/resultset.go index 0283a0382..f1fa19316 100644 --- a/mysql/resultset.go +++ b/mysql/resultset.go @@ -79,8 +79,9 @@ func (r *Resultset) Reset(fieldsCount int) { } } -// RowNumber is returning the number of rows in the Resultset -// For a nil ResultSet 0 is returned +// RowNumber is returning the number of rows in the [Resultset]. +// +// For a nil [Resultset] 0 is returned. func (r *Resultset) RowNumber() int { if r == nil { return 0 @@ -88,8 +89,9 @@ func (r *Resultset) RowNumber() int { return len(r.Values) } -// ColumnNumber is returning the number of fields in the Resultset -// For a nil ResultSet 0 is returned +// ColumnNumber is returning the number of fields in the [Resultset]. +// +// For a nil [Resultset] 0 is returned. func (r *Resultset) ColumnNumber() int { if r == nil { return 0