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/convert.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/convert.go')
| -rw-r--r-- | src/database/sql/convert.go | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/database/sql/convert.go b/src/database/sql/convert.go index dac3f246ae..8f71d5b867 100644 --- a/src/database/sql/convert.go +++ b/src/database/sql/convert.go @@ -237,7 +237,7 @@ func convertAssignRows(dest, src any, rows *Rows) error { if d == nil { return errNilPtr } - *d = append((*d)[:0], s...) + *d = rows.setrawbuf(append(rows.rawbuf(), s...)) return nil } case []byte: @@ -285,7 +285,7 @@ func convertAssignRows(dest, src any, rows *Rows) error { if d == nil { return errNilPtr } - *d = s.AppendFormat((*d)[:0], time.RFC3339Nano) + *d = rows.setrawbuf(s.AppendFormat(rows.rawbuf(), time.RFC3339Nano)) return nil } case decimalDecompose: @@ -366,8 +366,8 @@ func convertAssignRows(dest, src any, rows *Rows) error { } case *RawBytes: sv = reflect.ValueOf(src) - if b, ok := asBytes([]byte(*d)[:0], sv); ok { - *d = RawBytes(b) + if b, ok := asBytes(rows.rawbuf(), sv); ok { + *d = rows.setrawbuf(b) return nil } case *bool: |
