jmoiron / sqlx

general purpose extensions to golang's database/sql
http://jmoiron.github.io/sqlx/
MIT License
16.3k stars 1.09k forks source link

`Select()` mangling binary data when using `sql.RawBytes` #931

Open Kangaroux opened 5 months ago

Kangaroux commented 5 months ago

The conditions for this to happen are a little strange:

  1. db.Select() must be used
  2. The column type must be bytea
  3. The scan destination must be sql.RawBytes (or a struct field that is sql.RawBytes)
  4. The query must have a placeholder $1
postgres=# create table foo (data bytea);
CREATE TABLE
postgress=# insert into foo values ('\xdead'), ('\xbeef'), ('\xdeadbeef');
INSERT 0 3
postgres=# select * from foo;
  data  
--------
 \xdead
 \xbeef
 \xdeadbeef
(3 rows)
db := sqlx.MustConnect("postgres", ...)

var result [][]byte
db.Select(&result, `select * from foo where $1=1`, 1)
fmt.Printf("%x %x %x\n", result[0], result[1], result[2])

var result2 []sql.RawBytes
db.Select(&result2, `select * from foo where $1=1`, 1)
fmt.Printf("%x %x %x\n", result2[0], result2[1], result2[2])

/*
dead beef deadbeef
2033 2033 203300ef
*/

I didn't notice this problem with Query or Queryx.