Skip to content

Commit

Permalink
db.pg: fix invalid memory access in res_to_rows (#20248)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonmowry committed Dec 22, 2023
1 parent 944b955 commit 06a536e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/tools/vtest-self.v
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions vlib/db/pg/pg.c.v
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions vlib/db/pg/pg_test.v
@@ -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()
}
}

0 comments on commit 06a536e

Please sign in to comment.