Skip to content

Commit

Permalink
Fix ResultReader.Read() to handle nil values
Browse files Browse the repository at this point in the history
The ResultReader.Read() method was erroneously converting nil values
to []byte{}.

#1987
  • Loading branch information
jackc committed May 9, 2024
1 parent e4f7207 commit 48ae1f4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pgconn/pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,10 @@ func (rr *ResultReader) Read() *Result {
values := rr.Values()
row := make([][]byte, len(values))
for i := range row {
row[i] = make([]byte, len(values[i]))
copy(row[i], values[i])
if values[i] != nil {
row[i] = make([]byte, len(values[i]))
copy(row[i], values[i])
}
}
br.Rows = append(br.Rows, row)
}
Expand Down
18 changes: 18 additions & 0 deletions pgconn/pgconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,24 @@ func TestResultReaderValuesHaveSameCapacityAsLength(t *testing.T) {
ensureConnValid(t, pgConn)
}

// https://github.com/jackc/pgx/issues/1987
func TestResultReaderReadNil(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()

pgConn, err := pgconn.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
require.NoError(t, err)
defer closeConn(t, pgConn)

result := pgConn.ExecParams(ctx, "select null::text", nil, nil, nil, nil).Read()
require.NoError(t, result.Err)
require.Nil(t, result.Rows[0][0])

ensureConnValid(t, pgConn)
}

func TestConnExecPrepared(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 48ae1f4

Please sign in to comment.