Skip to content

Commit eb491f1

Browse files
committed
Copy byte slices when necessary to avoid overwriting the backing array of the original slice.
1 parent ea84ae4 commit eb491f1

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

sql/types/strings.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ func ConvertToBytes(ctx context.Context, v interface{}, t sql.StringType, dest [
397397
case string:
398398
val = append(dest, s...)
399399
case []byte:
400+
// We can avoid copying the slice if this isn't a conversion to BINARY
401+
// We'll check for that below, immediately before extending the slice.
400402
val = s
401403
start = 0
402404
case time.Time:
@@ -464,6 +466,11 @@ func ConvertToBytes(ctx context.Context, v interface{}, t sql.StringType, dest [
464466
}
465467

466468
if st.baseType == sqltypes.Binary {
469+
if b, ok := v.([]byte); ok {
470+
// Make a copy now to avoid overwriting the original allocation.
471+
val = append(dest, b...)
472+
start = len(dest)
473+
}
467474
val = append(val, bytes.Repeat([]byte{0}, int(st.maxCharLength)-len(val))...)
468475
}
469476
}

0 commit comments

Comments
 (0)