Bug #1168 mysql_close gets SIGBUG on MacOSX
Submitted: 31 Aug 2003 1:14 Modified: 10 Dec 2003 18:00
Reporter: Max Matveev Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.0.14` OS:MacOS (macosx 10.2.6)
Assigned to: Michael Widenius CPU Architecture:Any

[31 Aug 2003 1:14] Max Matveev
Description:
If a user attempts to provide its own store for MYSQL handle, mysql_close get SIGBUS on MacOSX

If I compile the following program:

#include <stdio.h>
#include <unistd.h>

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

int
main (int ac, char *av[])
{
    int i;
    char *user = NULL;
    char *dbname = NULL;
    char *passwd = NULL;
    MYSQL *msh = NULL;
    MYSQL mm;

    while ((i = getopt (ac, av, "Iiu:p:d:")) >= 0) {
        switch (i) {
        case 'I':
                msh = calloc (1, sizeof(mm));
                break;
        case 'i':
                msh = &mm;
                memset (&mm, 0, sizeof(mm));
                break;

        case 'u':
                user = optarg;
                break;
        case 'p':
                passwd = optarg;
                break;
        case 'd':
                dbname = optarg;
                break;
        default:
                exit (1);
        }
    }

    msh = mysql_init (msh);
    printf ("Init handle\n");

    printf ("About to connect as %s to db %s\n", user, dbname);

    if ( ! mysql_real_connect (msh, 0, user, passwd, dbname, 0, 0, 0) ) {
        printf ("Connection Failed: %s\n", mysql_error(msh));
    } else {
        printf ("Connected\n");
    }
    mysql_close(msh);
        puts ("BYE");

    return(0);
}

Using Makefile

core:~/src/xam/test >cat Makefile
CFLAGS = $(shell /usr/local/mysql/bin/mysql_config --cflags)
LDLIBS = $(shell /usr/local/mysql/bin/mysql_config --libs)
core:~/src/xam/test >make sql
cc -I'/usr/local/mysql/include'    sql.c  -L'/usr/local/mysql/lib' -lmysqlclient -lz -lm -o sql
sql.c: In function `main':
sql.c:20: warning: assignment makes pointer from integer without a cast
core:~/src/xam/test >./sql -u root -i
Init handle
About to connect as root to db (null)
Connected
BYE
Bus error
core:~/src/xam/test >./sql -u root -I
Init handle
About to connect as root to db (null)
Connected
BYE
core:~/src/xam/test >./sql -u root   
Init handle
About to connect as root to db (null)
Connected
BYE

The symptoms sort of suggest that mysql_close attempt to free the memory unconditionally,
corrupts the heap and then exit dies with SIGBUS

How to repeat:
Compile, run as in example.
[10 Dec 2003 18:00] Michael Widenius
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at
http://bugs.mysql.com/how-to-report.php

If you can provide more information, feel free to add it
to this bug and change the status back to 'Open'.

Thank you for your interest in MySQL.

Additional info:

I tested this with MySQL 4.0.16, but didn't get an sigbus errors

The only reason I can think of why this would happen is if your include files is not in line with your library files.

Please update include and library files to MySQL 4.0.16 and try again.
If you still get this problem, please try compiling MySQL with 'configure --debug' and try to get a back trace (with gdb) for the crash you get.