Using mysqlclient library -- odd crash

Z

ZER0K3WL

The code is pasted to the following site:

http://www.rafb.net/paste/results/4Po5gU24.html

Basically, i just wrote wrappers to some mysqclient calls, they all work fine
except when im about to return from main, my stack seems messed up and main
tries to return to 0x00000 and i get a segfault.



I'm compiling with
gcc data.c -c
gcc test.c data.o -lmysqlclient -lz -o t


(e-mail address removed)

thanks
 
O

Old Wolf

The code is pasted to the following site:

http://www.rafb.net/paste/results/4Po5gU24.html

Basically, i just wrote wrappers to some mysqclient calls, they all work fine
except when im about to return from main, my stack seems messed up and main
tries to return to 0x00000 and i get a segfault.

I don't see anything obviously wrong with your code, maybe you could
post it to a MySQL discussion forum.

You could try and reduce your code to the minimum possible code
that gives the error, this is often a good way of finding bugs.
For example, start with a program that just calls the mysql
functions with fixed strings, and if that works, add in code
slowly, working towards the current situation.

Also since you have symptoms of a memory error, you could try
running a program like 'valgrind' to check for memory leaks
or out-of-bounds accesses. What compiler do you use?

There are 2 odd things in your code: you don't check the return
value of db_init() (if it was NULL then you might get that symptom),
and this code:
char *query;
query = malloc((sizeof(char) *
(strlen(table) + strlen(values) + 32)));

sizeof(char) is always 1, so you can omit it.
You should also check the return value of malloc.
sprintf(query, "INSERT INTO %s VALUES(%s)", table, values); #ifdef DEBUG
printf("Adding %s to %s\n", values, table);
printf("Query is: %s\n", query);
printf("Allocated %d bytes\n", (sizeof(char) *
(strlen(table) + strlen(values) + 21)));
#endif

21 is the wrong value here, it should be 22 (21 chars + the zero
terminator). If you had written 21 instead of 32 in the malloc,
then the symptoms you're seeing would be a likely error.
 
P

Peter Slootweg

ZER0K3WL said:
The code is pasted to the following site:

http://www.rafb.net/paste/results/4Po5gU24.html

Basically, i just wrote wrappers to some mysqclient calls, they all work fine
except when im about to return from main, my stack seems messed up and main
tries to return to 0x00000 and i get a segfault.



I'm compiling with
gcc data.c -c
gcc test.c data.o -lmysqlclient -lz -o t


(e-mail address removed)

thanks


[OT]
see the 3rd user comment at
http://dev.mysql.com/doc/mysql/en/mysql_init.html

You can either figure our what the packing/alignment issues are
OR
if you pass in NULL to mysql_init it will allocate the structure and the you
should have no packing/aligment issues. You do however have to check the
result
to ensure it was able to allocate the structure.

[/OT]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top