Bug #29819 MySQL-functions are causing "Bus error".
Submitted: 16 Jul 2007 12:32 Modified: 16 Jul 2007 12:52
Reporter: Vincent David Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S2 (Serious)
Version:5.0.45 OS:MacOS (Tiger (10.4.10))
Assigned to: Sveta Smirnova CPU Architecture:Any
Tags: bus error, mysql_init, mysql_real_connect., segfault, segmentation fault

[16 Jul 2007 12:32] Vincent David
Description:
Hi,

This problem is reproducable and MacOsX-native. The same code runs under Linux.

I'm reporting this bug in reference to an old, unattended bug:

http://bugs.mysql.com/bug.php?id=3434&thanks=3&notify=71

I have the same problem, namely getting segfaults (aka 'Bus Error' on MacOsX) when using declaring a pointer to a MySQL object in the sense of

MYSQL *mysql;

As soon a I use this object for the first time by passing it over to mysql_init(mysql, ...) or mysql_real_connect(mysql, ...) I get a segfault.

The workaround is to declare:

MYSQL mysql;

and then call i.e.:

mysql_init(& mysql, ...)

This seams irrational to me especially since it works under Linux. My gcc-version is:

i686-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build 5367)

Any help would be appreceated!

I'll attach a exemplary c-file and a gdb-stack.

How to repeat:
Run code on MacOsX (any I guess)

Suggested fix:
Fix pointer referencing under MacOsX
[16 Jul 2007 12:33] Vincent David
example source

Attachment: MySQLTest_broken.c (text/plain), 760 bytes.

[16 Jul 2007 12:35] Vincent David
gdb dump

Attachment: gdb_dump (application/text, text), 489 bytes.

[16 Jul 2007 12:52] Hartmut Holzgraefe
We're sorry, but the bug system is not the appropriate forum for asking help on using MySQL products. Your problem is not the result of a bug.

Support on using our products is available both free in our forums at http://forums.mysql.com/ and for a reasonable fee direct from our skilled support engineers at http://www.mysql.com/support/

The problem is that you are using *unallocated* memory in your first example while using (implicitly) allocated memory in the 2nd where the MYSQL structure
is implicitly allocated on the functions local variable stack.

Passing on an unallocated/uninitialized pointer may fail right away
or may seem to work at first depending on compiler options etc...
but is a wrong thing nonetheless.

Adding a 

  mysql = (MYSQL *)malloc(sizeof(MYSQL));

would help to prevent the problem you are facing just the same way 
that making it a local variable does.

Thank you for your interest in MySQL.