db.pg: fix invalid memory access in res_to_rows (#20248)

This commit is contained in:
jacksonmowry 2023-12-22 00:02:31 +00:00 committed by GitHub
parent 944b9554bd
commit 06a536eff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -92,6 +92,7 @@ const skip_test_files = [
'vlib/db/mysql/mysql_test.v', // mysql not installed
'vlib/db/mysql/prepared_stmt_test.v', // mysql not installed
'vlib/db/pg/pg_orm_test.v', // pg not installed
'vlib/db/pg/pg_test.v', // pg not installed
]
// These tests are too slow to be run in the CI on each PR/commit
// in the sanitized modes:

View File

@ -190,8 +190,7 @@ fn res_to_rows(res voidptr) []Row {
row.vals << none
} else {
val := C.PQgetvalue(res, i, j)
sval := unsafe { val.vstring() }
row.vals << sval
row.vals << unsafe { cstring_to_vstring(val) }
}
}
rows << row

24
vlib/db/pg/pg_test.v Normal file
View File

@ -0,0 +1,24 @@
module main
import db.pg
const query = 'SELECT ischema.table_schema, c.relname, a.attname, t.typname, t.typalign, t.typlen
FROM pg_class c
JOIN information_schema.tables ischema on ischema.table_name = c.relname
JOIN pg_attribute a ON (a.attrelid = c.oid)
JOIN pg_type t ON (t.oid = a.atttypid)
WHERE
a.attnum >= 0'
fn test_large_exec() {
db := pg.connect(pg.Config{ user: 'postgres', password: 'secret', dbname: 'postgres' })!
defer {
db.close()
}
rows := db.exec(query)!
for row in rows {
// We just need to access the memory to ensure it's properly allocated
row.str()
}
}