Bug #56669 mysql_ping generates 'segmentation fault' libmysqlclient_r
Submitted: 9 Sep 2010 1:20 Modified: 10 Sep 2010 1:30
Reporter: Gabriel LePage Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S3 (Non-critical)
Version:5.1.41 OS:Linux
Assigned to: CPU Architecture:Any
Tags: mysql_ping mysql c api segmenation fault libmysqlclient_r

[9 Sep 2010 1:20] Gabriel LePage
Description:
Using:

linux 64 bit (ubuntu server 10.04 LTS)
libmysqlclient_r
version 5.1.41
mysql c api
gcc tool chain
using the flags and libs per mysql_config

Issue:

When calling mysql_ping before calling mysql_real_connect
produces a segmentation fault every time

Other:

You may be asking why anyone would want to call mysql_ping before connecting to the database. Well I was testing my code in situations I knew would fail, so that I can write the necessary code to recover and handle errors. The initial intent of this experimentation was to reconnect to the database if mysql_ping was unsuccessful. I have no real intent of using mysql_ping before connecting the database in the actual implementation. (I know that I can set options for auto-reconnect etc)

mysql_init has been called before calling mysql_ping

My impression is that mysql_ping should not generate a segmentation fault under any circumstances.

If I call mysql_real_connect and continuously call mysql_ping and break the connection I get no segmentation error.

How to repeat:
calling mysql_ping before connecting
produces a segmentation fault every time

Suggested fix:
not sure, do not call mysql_ping before mysql_real_connect
[10 Sep 2010 1:30] Alexey Kishkin
Confirm

$ cat tst56669.c
#include <my_global.h>
#include <mysql.h>

int main(int argc, char **argv)
{

  MYSQL *conn;
  conn = mysql_init(NULL);
  mysql_ping(conn);
  mysql_close(conn);
}

$ ./tst56669
Segmentation fault
[10 Nov 2011 16:53] plop plop
Same here with libmysqlclient16 5.1.58.

MYSQL db;
mysql_library_init(0, NULL, NULL)
mysql_init(&db);
mysql_ping(&db);
>> segfault

==5911== Invalid read of size 8
==5911==    at 0x532F09A: mysql_ping (in /usr/lib/libmysqlclient.so.16.0.0)
==5911==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==5911== 
==5911== 
==5911== Process terminating with default action of signal 11 (SIGSEGV)
==5911==  Access not within mapped region at address 0x8
==5911==    at 0x532F09A: mysql_ping (in /usr/lib/libmysqlclient.so.16.0.0)
[10 Nov 2011 22:57] plop plop
I have to mention that I use MYSQL_OPT_RECONNECT true. So I guess the segfault might happen while trying to reconnect ? I have not tried the crash test with MYSQL_OPT_RECONNECT false though but if the segfault happen to be caused by the reconnect attempt, I still believe that it should be up to the mysql API to deal with the possibility that mysql_real_connect() could very well not have been called yet.
[10 Nov 2011 23:27] plop plop
So I just checked, the segfault has nothing to do with MYSQL_OPT_RECONNECT, should it be true or false.
[7 Dec 2011 21:13] Stan Sieler
Doing: mysql_ping (NULL) generates a "Bus Error" (segmentation fault on some platforms).
Reproduced on Linux and Mac OS X.

Suggested fix, near start of mysql_ping code:

   if conn == NULL)
      return CR_CONN_IS_NULL   (a new error code, becuase CR_NULL_POINTER really isn't good enough here  :)

Yes, the workaround is "don't do that" ... but it should still be fixed.
[26 Mar 2019 1:41] Michael Robinton
mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64)

 ping failed: Calling a synchronous function on an asynchronous handle

Ubuntu 16.04.4

mysql_read_timeout set, async read operation
polling mysql_async_ready flag
next program step after failure is a ping to re-establish the connection 
using the same $dbh

mysql handler executed "DIE", should warn and return with error (ping failure)