From c23579f031ecd09bf37c644723b33736dffa8b92 Mon Sep 17 00:00:00 2001 From: Damien Neil Date: Tue, 23 Jan 2024 15:59:47 -0800 Subject: 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 Reviewed-by: Roland Shoemaker LUCI-TryBot-Result: Go LUCI --- src/database/sql/convert.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/database/sql/convert.go') 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: -- cgit v1.3