List:MySQL++« Previous MessageNext Message »
From:Linda Messerschmidt Date:July 28 2011 8:55pm
Subject:Strange segfault on bad query
View as plain text  
Hi all,

I came across a strange crash today with a bad query.

Basically, I did a query like "SELECT value FROM NoSuchTable WHERE key
= 6" and got a segfault with randomly varying stack traces inside
libmysqlclient (mysql_send_query or mysql_slave_send_query) or in
libmysqlpp (in one of the Query::store functions)

When I wrote a simple test app, I (correctly, for the assigned
permissions) got an exception containing:

SELECT command denied to user 'example'@'127.0.0.1' for table 'NoSuchTable'

So obviously I've done something wrong in my real app.  Given the
random nature of the segfault, it seems like it must be memory
corruption.  Unfortunately I really don't know where to look.  The
real app runs correct queries by the thousands without any sort of
memory corruption.

Does anyone have any ideas where to look or what I might be doing to
cause things to go awry, but only on errors?

The sample program (which works) is:

#include <mysql++/mysql++.h>

int main() {
	try {
		mysqlpp::Connection con("ExampleDB", "127.0.0.1", "user", "password");
		mysqlpp::Query qry(con.query("SELECT NoSuchField FROM NoSuchTable
WHERE NoSuchField = 6"));
		mysqlpp::StoreQueryResult sqr = qry.store();
	} catch (std::exception &r_ex) {
		std::cerr << "EXCEPTION: " << r_ex.what() << std::endl;
	}
}

The real app uses the same calls in the same order, but spread across
a lot of unrelated source code.

The most recent stack trace is:

0x00000008009fd26f in mysqlpp::Query::store ()
   from /usr/local/lib/libmysqlpp.so.3
(gdb) where
#0  0x00000008009fd26f in mysqlpp::Query::store ()
   from /usr/local/lib/libmysqlpp.so.3
#1  0x00000008009fd7e4 in mysqlpp::Query::store ()
   from /usr/local/lib/libmysqlpp.so.3
#2  0x00000008009fd89e in mysqlpp::Query::store ()
   from /usr/local/lib/libmysqlpp.so.3
#3  0x0000000000447eb0 in MyNS::DB::queryOne (this=0x801f3e918,
    or_str=@0x7fffff5fac18, r_qry=@0x7fffff5facb0) at DB.cc:34
#4  0x000000000040eb40 in MyNS::DB::queryOne<std::string> (this=0x801f3e918,
    or_t=@0x7fffff5fb018, r_qry=@0x7fffff5facb0) at DB.h:45
#5  0x000000000040ea3c in MyNS::DBTable::getFieldByKey<std::string> (
    this=0x7fffff5faf78, cr_stField=@0x7fffff5faf80, i_key=6) at DBTable.h:34

The DB::queryOne method (#3) is merely:

void DB::queryOne(String &or_str, Query &r_qry) {
	StoreQueryResult sqr = r_qry.store();     // <----- line DB.cc:34
	if (!sqr)
		throw QueryErrorEx(r_qry);
	if (sqr.num_rows() != 1)
		throw ExpectOneResultEx(r_qry, sqr.size());
	or_str = sqr[0][0];
}

The DB::queryOne template version (#4):

template <typename T>
void DB::queryOne(T &or_t, mysqlpp::Query &r_qry) {
	mysqlpp::String str;
	queryOne(str, r_qry);  // <------ line DB.h:45
	if (str.is_null())
		throw ExpectNotNullEx(r_qry);
	std::string st;
	str.to_string(st);  // Not efficient, but easy to debug.
	or_t = boost::lexical_cast<T>(st);		
}

And DBTable::getFieldByKey (#5):

template <typename T>
T DBTable::getFieldByKey(const std::string &cr_stField, Key i_key) {
	mysqlpp::Query qry(getFieldKeyQuery(cr_stField, i_key));
	T t;
	db_.queryOne(t, qry);  // <----- line DBTable.h:34
	return t;
}

And getFieldKeyQuery looks like:

virtual mysqlpp::Query DBTable::getFieldKeyQuery(const std::string
&cr_stField, Key i_key) {
	mysqlpp::Query qry(conn_.query("SELECT "));
	qry << cr_stField << " FROM " << strTable << " WHERE " <<
strKeyField
<< " = " << i_key;
	return qry;
}

Everything's allocated on the stack, nothing's falling out of scope
before it's referenced.  I just don't know what could be going wrong.

I'll keep looking, but if someone happens to have seen this before and
could shove me in the right direction, I would really appreciate it.
:)

Thanks for any advice!
Thread
Strange segfault on bad queryLinda Messerschmidt28 Jul
  • Re: Strange segfault on bad queryKemin Zhou28 Jul
  • Re: Strange segfault on bad queryLinda Messerschmidt29 Jul