c - sqlite3 sqlite3_prepare_v2 segment fault -
in test program:
#include <sqlite3.h> #include <stdio.h> #include <stdlib.h> int main(void) { sqlite3 *conn; sqlite3_stmt *res; int rc = 0; int rec_count = 0; const char *errmsg; const char *tail; const char *sql; rc = sqlite3_open("ljdata.sl3", &conn); if (rc) { puts("can not open database"); exit(0); } printf ("database open\n"); sql = "create table if not exists people (id int, firstname varchar(20), lastname varchar(20), phonenumber char(10))"; rc = sqlite3_exec(conn, sql, 0, 0, 0); if (rc != sqlite_ok) { printf ("cannot access table, rc = %d,\n%s\n", rc, sqlite3_errmsg (conn)); printf ("sql = %s", sql); return -2; } printf("db table (?)created\n"); rc = sqlite3_exec(conn, "update people set phonenumber=\'5055559999\' id=3", 0, 0, 0); //printf ("1st update done rc = %d\n", rc); rc = sqlite3_prepare_v2(conn, "select lastname,firstname,phonenumber,id people order id", 1000, &res, &tail); printf ("prepare statement executed\n"); if (rc != sqlite_ok) { puts("we did not data!"); exit(0); } puts("=========================="); while (sqlite3_step(res) == sqlite_row) { printf("%s|", sqlite3_column_text(res, 0)); printf("%s|", sqlite3_column_text(res, 1)); printf("%s|", sqlite3_column_text(res, 2)); printf("%u\n", sqlite3_column_int(res, 3)); rec_count++; } puts("=========================="); printf("we received %d records.\n", rec_count); sqlite3_finalize(res); sqlite3_close(conn); return 0; }
if comment out:
printf ("1st update done rc = %d\n", rc); // <<== statement
segment fault. otherwise appear normal completion. gcc -v -->
using built-in specs.
collect_gcc=gcc
collect_lto_wrapper=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
target: x86_64-linux-gnu
configured with: ../src/configure -v --with-pkgversion='debian 4.7.2-5' --with-bugurl=file:///usr/share/doc/gcc-4.7/readme.bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
thread model: posix
gcc version 4.7.2 (debian 4.7.2-5)
however, (less analyzed) segment fault on debian testing.
suggestions? (i note similar error answered "you need allocate space sql string, i'm using literal, shouldn't apply.)
off top of head, sqlite3_prepare_v2
call fails because lie string length:
rc = sqlite3_prepare_v2(conn, "select lastname,firstname,phonenumber,id people order id", 1000, &res, &tail);
it appears work if include printf
call because string literal placed in same section string who's length lie about, , whatever sqlite3_prepare_v2
doing spills on other string.
what have here buffer overrun.
Comments
Post a Comment