Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Kangaroux opened this issue Jun 19, 2024 · 0 comments
Open

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

Kangaroux opened this issue Jun 19, 2024 · 0 comments

Comments

@Kangaroux
Copy link

Kangaroux commented Jun 19, 2024

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.

  • Postgres 16.3 (using postgres:16-alpine docker image)
  • lib/pq v1.10.9
  • sqlx v1.4.0
@Kangaroux Kangaroux changed the title Select() mangling binary data Select() mangling binary data when using sql.RawBytes Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant