diff options
| author | Damien Neil <dneil@google.com> | 2024-01-23 15:59:47 -0800 |
|---|---|---|
| committer | Damien Neil <dneil@google.com> | 2024-04-10 20:23:22 +0000 |
| commit | c23579f031ecd09bf37c644723b33736dffa8b92 (patch) | |
| tree | 9558bc5995b581328813f9f490c7d42124248508 /src/database/sql/sql_test.go | |
| parent | 5b5d6f87a8a19848b367a0e0d8e56c0141c02193 (diff) | |
| download | go-c23579f031ecd09bf37c644723b33736dffa8b92.tar.xz | |
database/sql: avoid clobbering driver-owned memory in RawBytes
Depending on the query, a RawBytes can contain memory owned by the
driver or by database/sql:
If the driver provides the column as a []byte,
RawBytes aliases that []byte.
If the driver provides the column as any other type,
RawBytes contains memory allocated by database/sql.
Prior to this CL, Rows.Scan will reuse existing capacity in a
RawBytes to permit a single allocation to be reused across rows.
When a RawBytes is reused across queries, this can result
in database/sql writing to driver-owned memory.
Add a buffer to Rows to store RawBytes data, and reuse this
buffer across calls to Rows.Scan.
Fixes #65201
Change-Id: Iac640174c7afa97eeb39496f47dec202501b2483
Reviewed-on: https://go-review.googlesource.com/c/go/+/557917
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Roland Shoemaker <roland@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/database/sql/sql_test.go')
| -rw-r--r-- | src/database/sql/sql_test.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/database/sql/sql_test.go b/src/database/sql/sql_test.go index 25ca5ff0ad..7dfc6434e0 100644 --- a/src/database/sql/sql_test.go +++ b/src/database/sql/sql_test.go @@ -4566,6 +4566,53 @@ func TestNilErrorAfterClose(t *testing.T) { } } +// Issue #65201. +// +// If a RawBytes is reused across multiple queries, +// subsequent queries shouldn't overwrite driver-owned memory from previous queries. +func TestRawBytesReuse(t *testing.T) { + db := newTestDB(t, "people") + defer closeDB(t, db) + + if _, err := db.Exec("USE_RAWBYTES"); err != nil { + t.Fatal(err) + } + + var raw RawBytes + + // The RawBytes in this query aliases driver-owned memory. + rows, err := db.Query("SELECT|people|name|") + if err != nil { + t.Fatal(err) + } + rows.Next() + rows.Scan(&raw) // now raw is pointing to driver-owned memory + name1 := string(raw) + rows.Close() + + // The RawBytes in this query does not alias driver-owned memory. + rows, err = db.Query("SELECT|people|age|") + if err != nil { + t.Fatal(err) + } + rows.Next() + rows.Scan(&raw) // this must not write to the driver-owned memory in raw + rows.Close() + + // Repeat the first query. Nothing should have changed. + rows, err = db.Query("SELECT|people|name|") + if err != nil { + t.Fatal(err) + } + rows.Next() + rows.Scan(&raw) // raw points to driver-owned memory again + name2 := string(raw) + rows.Close() + if name1 != name2 { + t.Fatalf("Scan read name %q, want %q", name2, name1) + } +} + // badConn implements a bad driver.Conn, for TestBadDriver. // The Exec method panics. type badConn struct{} |
