I am currently debugging a Project I've been working on for a while, and have encountered some crazy errors involving free
. I can't upload the code, because there is no way to tell exactly where the problem lies (about 2500 lines of code split into 22 files), but I will explain what I know.
To start with, gdb
is being used for the whole debugging process. The error seems to rise from a call to free
. I get the following error message from gdb
, after the program exits with SIGABRT
:
*** Error in `application': free(): invalid next size (normal): 0x08052008 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x767e2)[0xb7e467e2]
/lib/i386-linux-gnu/libc.so.6(+0x77530)[0xb7e47530]
application[0x8049aef]
application[0x804a8aa]
application[0x8048bee]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0xb7de9935]
application[0x8048a51]
======= Memory map: ========
08048000-08050000 r-xp 00000000 00:16 1571817 application
08050000-08051000 r--p 00007000 00:16 1571817 application
08051000-08052000 rw-p 00008000 00:16 1571817 application
08052000-08073000 rw-p 00000000 00:00 0 [heap]
b7d9c000-b7db7000 r-xp 00000000 08:01 1309022 /lib/i386-linux-gnu/libgcc_s.so.1
b7db7000-b7db8000 r--p 0001a000 08:01 1309022 /lib/i386-linux-gnu/libgcc_s.so.1
b7db8000-b7db9000 rw-p 0001b000 08:01 1309022 /lib/i386-linux-gnu/libgcc_s.so.1
b7dce000-b7dd0000 rw-p 00000000 00:00 0
b7dd0000-b7f7d000 r-xp 00000000 08:01 1308997 /lib/i386-linux-gnu/libc-2.17.so
b7f7d000-b7f7f000 r--p 001ad000 08:01 1308997 /lib/i386-linux-gnu/libc-2.17.so
b7f7f000-b7f80000 rw-p 001af000 08:01 1308997 /lib/i386-linux-gnu/libc-2.17.so
b7f80000-b7f83000 rw-p 00000000 00:00 0
b7f83000-b7fc4000 r-xp 00000000 08:01 1309045 /lib/i386-linux-gnu/libm-2.17.so
b7fc4000-b7fc5000 r--p 00040000 08:01 1309045 /lib/i386-linux-gnu/libm-2.17.so
b7fc5000-b7fc6000 rw-p 00041000 08:01 1309045 /lib/i386-linux-gnu/libm-2.17.so
b7fd9000-b7fdd000 rw-p 00000000 00:00 0
b7fdd000-b7fde000 r-xp 00000000 00:00 0 [vdso]
b7fde000-b7ffe000 r-xp 00000000 08:01 1308973 /lib/i386-linux-gnu/ld-2.17.so
b7ffe000-b7fff000 r--p 0001f000 08:01 1308973 /lib/i386-linux-gnu/ld-2.17.so
b7fff000-b8000000 rw-p 00020000 08:01 1308973 /lib/i386-linux-gnu/ld-2.17.so
bffdf000-c0000000 rw-p 00000000 00:00 0 [stack]
It seems to be like a common double-free, yet there is more. I have the tendency to set all global pointers to NULL
when they don't contain anything, so even if I double-freed it wouldn't cause an error. Moreover, as my application deals with data encryption, I created two functions of my own that first overwrite the memory and then call free.
Can this problem be caused by reading from memory that is out of bounds? For example, if I have a 64-byte memory block and accidentally try to read the 65th byte, will this cause an error when calling free? I know that writing out of bounds, even if not immediately, causes an error...
I've been trying to locate the bug all day, but with no success. Are there any tools (apart from breaks, step, continue and watches) that gdb provides for my case? Can I see what my code really does in terms of memory allocation and management? For example, is there any way to see how much memory is actually allocated in a position pointed to by a specific pointer?
Thank you in advance for your time! :)