List:MySQL++« Previous MessageNext Message »
From:Edward D. Crump Date:February 28 2007 5:49pm
Subject:examples/dbinfo fails because of Query::preview() null byte addition
View as plain text  
(just for folks getting started with MySQL++ and seeing failures in the 

There is a known issue with Query::str() adding a null byte, which is 
apparently documented behavior to be fixed in the next major release.  
Query::preview() uses str().

This affects the dbinfo example where queries are printed out before 
being used:

    cout << "Query: " << query.preview() << endl;

If you remove the query.preview()'s from examples/dbinfo.cpp, the 
example works as expected. 

This causes the following error (Linux example):

    $ ./dbinfo localhost user1 passwd1
    MySQL version: 5.0.27

    Query: show databases
    Query error: You have an error in your SQL syntax; check the manual
    that corresponds to your MySQL server version for the right syntax
    to use near '' at line 1

Analysis ...

If you debug the code, you'll see that the string which gets passed to 
mysql_real_query() has two NULL bytes at the end.

    (gdb) s
    mysqlpp::Query::store (this=0xbfb42844, str=0x807067c "show
    databases", len=16) at ./lib/query.cpp:436
    436             if (lock()) {
    (gdb) p str[14]
    $1 = 0 '\0'
    (gdb) p str[15]
    $2 = 0 '\0'

If you change the length of "str" (via gdb using "p len=15" in the 
example above), the syntax error goes away.  One null byte is ok, two 
apparently is interpreted as a second, empty "" statement by 

The same issue affects the custom2, custom3, fieldinf1, and multiquery 
examples.  custom5 works because the preview() is done after the queries.

I could patch Query::store() to trim extra trailing null bytes as 
mysql_real_query() clearly interprets these as empty queries, which is 
consistent with the MySQL documentation.  I've had about 5 minutes with 
the code base, so I may be missing something obvious (and documented).

Cheers ...


examples/dbinfo fails because of Query::preview() null byte addition...Edward D. Crump28 Feb
  • Re: examples/dbinfo fails because of Query::preview() null byte addition...Warren Young28 Feb