Bug #64994 use of my_load_defaults() gives segmentation fault in my_stat()
Submitted: 16 Apr 2012 13:44 Modified: 18 Apr 2012 8:52
Reporter: Erik Remmelzwaal Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S3 (Non-critical)
Version:5.5.21, 5.5.24 OS:Linux (2.6.16.60 64-bit)
Assigned to: Jon Olav Hauglid CPU Architecture:Any
Tags: my_load_defaults segmentation fault, regression
Triage: Needs Triage: D1 (Critical)

[16 Apr 2012 13:44] Erik Remmelzwaal
Description:
I'm trying to use my_load_defaults() to get hold on client properties. However calling this function with 5.5.21 on SUSE 64 bit fails. Using the same code against MariaDB with 5.2.10 runs fine.

The error is:
$ ./show_defaults
Arguments 1:
 0: this
Memory fault(coredump)

$ gdb ./show_defaults ./core
GNU gdb (GDB) SUSE (6.8.50.20090302-44.1)
Core was generated by `./show_defaults'.
Program terminated with signal 11, Segmentation fault.
#0  my_stat (path=0x7fff9d2ce270 "/etc/mysql/my.cnf", stat_area=0x7fff9d2ce680, my_flags=0)
    at /pb2/build/sb_0-4846558-1328009453.53/rpm/BUILD/mysql-5.5.21/mysql-5.5.21/mysys/my_lib.c:413
413     /pb2/build/sb_0-4846558-1328009453.53/rpm/BUILD/mysql-5.5.21/mysql-5.5.21/mysys/my_lib.c: No such file or directory.
        in /pb2/build/sb_0-4846558-1328009453.53/rpm/BUILD/mysql-5.5.21/mysql-5.5.21/mysys/my_lib.c

When I create /etc/mysql.my.cnf, it brakes on /usr/etc/my.cnf, etc...

How to repeat:
Compile:
gcc  -g -I/dbms/MySQL/5.5.21/LINUX2.6.16/include/mysql show_defaults.c -o show_defaults -L/dbms/MySQL/5.5.21/LINUX2.6.16/lib64 -lmysqlclient

Source code:

#include <stdlib.h>
#include <stdio.h>
#include "my_global.h"
#include "mysql.h"

void print_args(int argc, char **argv);
int main(int argc, char **argv) {
        int argcc;
        const char *groups[3];
        char *argv_buff[10], ** argvv;
        groups[0] = "client"; groups[1] =  0;

        argcc = 1; argv_buff[0] = "this"; argv_buff[1] = 0;
        argvv = argv_buff;

        print_args(argcc, argvv);
        my_load_defaults("my", groups, &argcc, &argvv, NULL);
        print_args(argcc, argvv);
}

void print_args(int argc, char **argv) {
        int i;
        printf("\nArguments %d:\n", argc );
        for( i=0; i<argc; i++ ) {
                printf("%2d: %s\n", i, argv[i]);
        }
}
[17 Apr 2012 18:01] Sveta Smirnova
Thank you for the report.

Verified as described. Not repeatable with version 5.1
[17 Apr 2012 18:03] Sveta Smirnova
gdb output in my environment:

(gdb) run
Starting program: /home/sveta/src/bugs/bug64994 
[Thread debugging using libthread_db enabled]

Arguments 1:
 0: this
safe_mutex: Trying to lock unitialized mutex at /home/sveta/src/mysql-5.5/mysys/my_fopen.c, line 70

Program received signal SIGABRT, Aborted.
0x00000031d7432a45 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.25.el6.x86_64 zlib-1.2.3-25.el6.x86_64
(gdb) bt
#0  0x00000031d7432a45 in raise () from /lib64/libc.so.6
#1  0x00000031d7434225 in abort () from /lib64/libc.so.6
#2  0x00007ffff7afc0e7 in safe_mutex_lock (mp=0x7ffff7ffcbe0, try_lock=0 '\000', file=0x7ffff7b632d0 "/home/sveta/src/mysql-5.5/mysys/my_fopen.c", line=70)
    at /home/sveta/src/mysql-5.5/mysys/thr_mutex.c:106
#3  0x00007ffff7af3ffc in inline_mysql_mutex_lock (that=0x7ffff7ffcbe0, src_file=0x7ffff7b632d0 "/home/sveta/src/mysql-5.5/mysys/my_fopen.c", src_line=70)
    at /home/sveta/src/mysql-5.5/include/mysql/psi/mysql_thread.h:615
#4  0x00007ffff7af41ee in my_fopen (filename=0x7fffffffdaa0 "/etc/my.cnf", flags=0, MyFlags=0) at /home/sveta/src/mysql-5.5/mysys/my_fopen.c:70
#5  0x00007ffff7aee1c8 in inline_mysql_file_fopen (key=0, src_file=0x7ffff7b61f38 "/home/sveta/src/mysql-5.5/mysys/default.c", src_line=803, 
    filename=0x7fffffffdaa0 "/etc/my.cnf", flags=0, myFlags=0) at /home/sveta/src/mysql-5.5/include/mysql/psi/mysql_file.h:810
#6  0x00007ffff7aef850 in search_default_file_with_ext (opt_handler=0x7ffff7aeeb37 <handle_default_option>, handler_ctx=0x7fffffffded0, dir=0x601058 "/etc/", 
    ext=0x7ffff7b61f7f ".cnf", config_file=0x400854 "my", recursion_level=0) at /home/sveta/src/mysql-5.5/mysys/default.c:803
#7  0x00007ffff7aef53e in search_default_file (opt_handler=0x7ffff7aeeb37 <handle_default_option>, handler_ctx=0x7fffffffded0, dir=0x601058 "/etc/", 
    config_file=0x400854 "my") at /home/sveta/src/mysql-5.5/mysys/default.c:670
#8  0x00007ffff7aeea2e in my_search_option_files (conf_file=0x400854 "my", argc=0x7fffffffe04c, argv=0x7fffffffdfd8, args_used=0x7fffffffdf2c, 
    func=0x7ffff7aeeb37 <handle_default_option>, func_ctx=0x7fffffffded0, default_directories=0x601020) at /home/sveta/src/mysql-5.5/mysys/default.c:321
#9  0x00007ffff7aef0c0 in my_load_defaults (conf_file=0x400854 "my", groups=0x7fffffffe038, argc=0x7fffffffe04c, argv=0x7fffffffdfd8, default_directories=0x0)
    at /home/sveta/src/mysql-5.5/mysys/default.c:576
#10 0x00000000004006d0 in main (argc=1, argv=0x7fffffffe138) at bug64994.c:17
[18 Apr 2012 5:39] Davi Arnaut
Please read the documentation of my_init().
[18 Apr 2012 7:09] Erik Remmelzwaal
Thanks for pointing to my_init().
Adding my_init() or mysql_init() before calling my_load_default() gives correct behavior.
Call can be closed.
[18 Apr 2012 8:52] Jon Olav Hauglid
Closing. Thanks Davi!