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



## graudeejs (Apr 27, 2010)

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)

```
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


----------



## crsd (Apr 27, 2010)

```
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:

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

8.0/amd64


----------



## expl (Apr 27, 2010)

```
[cgi.c] struct s_KeyValue *cgi_get = malloc(sizeof(struct s_KeyValue *));
```

should be:


```
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?


----------



## tankist02 (Apr 27, 2010)

Next time try Valgrind - a good tool to hunt memory problems.


----------



## graudeejs (Apr 27, 2010)

expl said:
			
		

> ```
> [cgi.c] struct s_KeyValue *cgi_get = malloc(sizeof(struct s_KeyValue *));
> ```
> 
> ...



wuaaaaaaaaaa, that was purely my fault, and it would happen to any C program 
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


----------



## graudeejs (Apr 27, 2010)

tankist02 said:
			
		

> Next time try Valgrind - a good tool to hunt memory problems.



I'll look in to it, right now,
Thanks for suggestion


----------



## Zare (Apr 27, 2010)

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...


----------



## expl (Apr 27, 2010)

killasmurf86 said:
			
		

> wuaaaaaaaaaa, that mas purely my fault, and it would happen to any C program
> 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.


----------

