Bug #39594 libmysql.dll crashes when calling mysql_ping() immediately after mysql_init(0)
Submitted: 22 Sep 2008 20:39 Modified: 23 Oct 2008 6:36
Reporter: Peter Pan Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S1 (Critical)
Version:4.1, 5.0, 5.1 OS:Any (MS Windows, Linux)
Assigned to: CPU Architecture:Any
Tags: mysql_ping

[22 Sep 2008 20:39] Peter Pan
Description:
libmysql.dll crashes when calling mysql_ping() immediately after mysql_init(0).

this happens to all the versions of mysql that i downloaded, incl v4.1, 5.0, 5.1.

it crashes EVERY TIME, without any exception.

How to repeat:
call mysql_ping() immediately after mysql_init(0).
[23 Sep 2008 6:54] Sveta Smirnova
Thank you for the report.

If write something like

#include "mysql.h"

MYSQL 	mysql;

int main()
{
     my_init();
     mysql_ping(&mysql);
}

program will crash for sure and it would be correct behavior, because mysql was not initialized. Please provide real example demonstrating problem in MySQL client libraries.
[23 Sep 2008 12:50] Peter Pan
Thank you for the quick response.

The crashing code IS NOT

     my_init();
     mysql_ping(&mysql);

BUT IS

     mysql_init(&mysql);
     mysql_ping(&mysql);

The same code did not crash with MySQL v3.xx.
[23 Sep 2008 15:55] Sveta Smirnova
Thank you for the feedback.

Definition of my_init is "void my_init(void)", see also http://dev.mysql.com/doc/refman/5.1/en/my-init.html And MySQL-3.23 doesn't allow to use  my_init(&mysql);

But after additional elaboration I think you are right and mysql_ping could return CR_COMMANDS_OUT_OF_SYNC instead of crashing. So I mark this report as verified.

gdb output:

(gdb) run
Starting program: /users/ssmirnova/src/bugs/bug39594 
Reading symbols for shared libraries ..+ done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000004
0x002094c1 in mysql_ping (mysql=0x2090) at libmysql.c:1421
1421      res= simple_command(mysql,COM_PING,0,0,0);
(gdb) bt
#0  0x002094c1 in mysql_ping (mysql=0x2090) at libmysql.c:1421
#1  0x00001f51 in main () at bug39594.c:17
[23 Sep 2008 18:21] Peter Pan
Thanks for the response and verification. 

I DO NOT use my_init().

I use mysql_init(), which is documented to call my_init() automatically.

Please try this code. It crashes at mysql_ping().

  MYSQL *mysql;
  mysql=mysql_init(0);
  mysql_ping(mysql);
[23 Sep 2008 18:40] Sveta Smirnova
Thank you for the feedback.

Seems I pasted wrong code. Here is correct one, although it fails in the same place:

$ gcc -o bug39594 bug39594.c ` /users/ssmirnova/mysql-5.1/bin/mysql_config --include --libs` -g

$gdb bug39594
GNU gdb 6.1-20040303 (Apple version gdb-437) (Fri Jan 13 18:45:48 GMT 2006)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin"...Reading symbols for shared libraries .... done

(gdb) run
Starting program: /Users/apple/Documents/web_project/MySQL/bugs/bug39594 
Reading symbols for shared libraries ..+ done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000004
0x002094c1 in mysql_ping (mysql=0x2090) at libmysql.c:1421
1421      res= simple_command(mysql,COM_PING,0,0,0);
(gdb) bt
#0  0x002094c1 in mysql_ping (mysql=0x2090) at libmysql.c:1421
#1  0x00001f50 in main () at bug39594.c:14
(gdb) q
The program is running.  Exit anyway? (y or n) y

$cat bug39594.c
#include "mysql.h"

char    *MySql_Host = "127.0.0.1";
char    *MySql_User = "root";
char    *MySql_Passwd = "";
char    *MySql_DB = "test";
int     MySql_Port = 3351;

MYSQL   mysql;

int main()
{
     mysql_init(&mysql);
     mysql_ping(&mysql);
}
[23 Oct 2008 6:36] Georg Richter
This is a bug in application, not in libmysql.

From http://dev.mysql.com/doc/refman/5.0/en/mysql-init.html:
"mysql_init(MYSQL *mysql)
Allocates or initializes a MYSQL object suitable for mysql_real_connect() ..."

So mysql_real_connect should be called before any other function which sends data to the server.