weird malloc & calloc behaviour (or i'm blind & stupid)

Hello!
For last few hours I've been hunting a bug in one of my C apps.
After calling malloc (same happened with calloc) one of variables suddenly changed value.

I've tested this many, many times. I still can't believe it [besides it's 2:10am here)

Could someone look this under the scope?
I've trimmed down source, so it's very small

I've also wrote small sh script to test this without debugger. Right now it's not all that much of a help, because I commented out part of code, that was reading stdin, and then manipulated, that data.

But still it sets environment, so you can simply ./run.sh to see what really happens.


It's really weird things going on with that source.

stderr output on my pc (32bit)
Code:
DEBUG1: 4
DEBUG2: 675295296
Bought were supposed to be equal (4)


Huge thanks in advance.


EDIT:
btw, static analysis didn't return any errors as well
 

Attachments

  • wtf.tar.gz
    3.5 KB · Views: 230
Code:
Starting program: /usr/home/yuri/wtf/sd1.cgi

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400977 in main () at main.c:52
52              fprintf(stderr, "DEBUG1: %d\n", cgi_get->len);  // cgi_get->len == 4

:) And with env variables set:
Code:
Program received signal SIGBUS, Bus error.
0x0000000000400d01 in cgiGet () at cgi.c:73
73                      if (qget_ptr[i] == '&') {

8.0/amd64
 
Code:
[cgi.c] struct s_KeyValue *cgi_get = malloc(sizeof(struct s_KeyValue *));

should be:

Code:
struct s_KeyValue *cgi_get = malloc(sizeof(struct s_KeyValue));

EDIT:

What did I tell you about cgi programs being pain in the ass to debug memory allocation faults? :p
 
expl said:
Code:
[cgi.c] struct s_KeyValue *cgi_get = malloc(sizeof(struct s_KeyValue *));

should be:

Code:
struct s_KeyValue *cgi_get = malloc(sizeof(struct s_KeyValue));

EDIT:

What did I tell you about cgi programs being pain in the ass to debug memory allocation faults? :p

wuaaaaaaaaaa, that was purely my fault, and it would happen to any C program :D
It's not CGi fault.

Huge thanks, I was hunting this bug for at least half day.
And he he he, really stupid and small mistake....

That's what makes C programming so much fun.

I first wrote cgiGet, and then copy past to cgiPost and cgiCookie..... later I just modified them. This bug was in all three of them. lol
 
Valgrind is a must for any modern UNIX C programmer.

I had some weird non-repeating segfaults a year ago, spent three days hunting them with gdb / printf :) methods, got tired, asked google, google said Valgrind, Valgrind gave answer as soon as problem appeared in program's runcycle ;)

That's just grind's memcheck module, there's a lot more...
 
killasmurf86 said:
wuaaaaaaaaaa, that mas purely my fault, and it would happen to any C program :D
It's not CGi fault.

Well its way more easy to debug a regular program than to simulate CGI and a web request(s) and debug in same time.
 
Back
Top