clibs / debug

Conditional debug logging for C
MIT License
17 stars 2 forks source link

Invalid usage of calloc #7

Open stephenmathieson opened 10 years ago

stephenmathieson commented 10 years ago

We're only requesting an array with one element, which causes nasal demons:

$ DEBUG=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z ./test
*** glibc detected *** ./test: double free or corruption (out): 0x0000000000ab7030 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7f0010080b96]
./test[0x4009b9]
./test[0x400868]
./test[0x400beb]
./test[0x4006ce]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7f001002376d]
./test[0x400765]
======= Memory map: ========
00400000-00402000 r-xp 00000000 ca:01 1058172                            /home/stephen/debug/test
00601000-00602000 r--p 00001000 ca:01 1058172                            /home/stephen/debug/test
00602000-00603000 rw-p 00002000 ca:01 1058172                            /home/stephen/debug/test
00ab7000-00ad8000 rw-p 00000000 00:00 0                                  [heap]
7f000fdec000-7f000fe01000 r-xp 00000000 ca:01 142751                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f000fe01000-7f0010000000 ---p 00015000 ca:01 142751                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0010000000-7f0010001000 r--p 00014000 ca:01 142751                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0010001000-7f0010002000 rw-p 00015000 ca:01 142751                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f0010002000-7f00101b7000 r-xp 00000000 ca:01 133594                     /lib/x86_64-linux-gnu/libc-2.15.so
7f00101b7000-7f00103b7000 ---p 001b5000 ca:01 133594                     /lib/x86_64-linux-gnu/libc-2.15.so
7f00103b7000-7f00103bb000 r--p 001b5000 ca:01 133594                     /lib/x86_64-linux-gnu/libc-2.15.so
7f00103bb000-7f00103bd000 rw-p 001b9000 ca:01 133594                     /lib/x86_64-linux-gnu/libc-2.15.so
7f00103bd000-7f00103c2000 rw-p 00000000 00:00 0 
7f00103c2000-7f00103e4000 r-xp 00000000 ca:01 142709                     /lib/x86_64-linux-gnu/ld-2.15.so
7f00105d4000-7f00105d7000 rw-p 00000000 00:00 0 
7f00105e1000-7f00105e4000 rw-p 00000000 00:00 0 
7f00105e4000-7f00105e5000 r--p 00022000 ca:01 142709                     /lib/x86_64-linux-gnu/ld-2.15.so
7f00105e5000-7f00105e7000 rw-p 00023000 ca:01 142709                     /lib/x86_64-linux-gnu/ld-2.15.so
7fff22444000-7fff22465000 rw-p 00000000 00:00 0                          [stack]
7fff22510000-7fff22511000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)
stephenmathieson commented 10 years ago

valgrind(1) provides a bit more detail:

$ DEBUG=a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z valgrind ./test
==3890== Memcheck, a memory error detector
==3890== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==3890== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==3890== Command: ./test
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x400984: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400BEA: _debug_active (in /home/stephen/debug/test)
==3890==    by 0x4006CD: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2040 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400BEA: _debug_active (in /home/stephen/debug/test)
==3890==    by 0x4006CD: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x40099B: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400BEA: _debug_active (in /home/stephen/debug/test)
==3890==    by 0x4006CD: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2048 is 7 bytes after a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400BEA: _debug_active (in /home/stephen/debug/test)
==3890==    by 0x4006CD: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid read of size 8
==3890==    at 0x400890: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400BEA: _debug_active (in /home/stephen/debug/test)
==3890==    by 0x4006CD: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2040 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400BEA: _debug_active (in /home/stephen/debug/test)
==3890==    by 0x4006CD: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x400984: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400B0A: _debug_inactive (in /home/stephen/debug/test)
==3890==    by 0x4006DC: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2570 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400B0A: _debug_inactive (in /home/stephen/debug/test)
==3890==    by 0x4006DC: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x40099B: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400B0A: _debug_inactive (in /home/stephen/debug/test)
==3890==    by 0x4006DC: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2578 is 7 bytes after a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400B0A: _debug_inactive (in /home/stephen/debug/test)
==3890==    by 0x4006DC: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid read of size 8
==3890==    at 0x400890: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400B0A: _debug_inactive (in /home/stephen/debug/test)
==3890==    by 0x4006DC: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2570 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400B0A: _debug_inactive (in /home/stephen/debug/test)
==3890==    by 0x4006DC: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x400984: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400E8A: _debug_foobar (in /home/stephen/debug/test)
==3890==    by 0x400714: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2aa0 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400E8A: _debug_foobar (in /home/stephen/debug/test)
==3890==    by 0x400714: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x40099B: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400E8A: _debug_foobar (in /home/stephen/debug/test)
==3890==    by 0x400714: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2aa8 is 7 bytes after a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400E8A: _debug_foobar (in /home/stephen/debug/test)
==3890==    by 0x400714: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid read of size 8
==3890==    at 0x400890: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400E8A: _debug_foobar (in /home/stephen/debug/test)
==3890==    by 0x400714: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2aa0 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400E8A: _debug_foobar (in /home/stephen/debug/test)
==3890==    by 0x400714: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x400984: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400DAA: _debug_foo_bar (in /home/stephen/debug/test)
==3890==    by 0x400723: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2fd0 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400DAA: _debug_foo_bar (in /home/stephen/debug/test)
==3890==    by 0x400723: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x40099B: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400DAA: _debug_foo_bar (in /home/stephen/debug/test)
==3890==    by 0x400723: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2fd8 is 7 bytes after a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400DAA: _debug_foo_bar (in /home/stephen/debug/test)
==3890==    by 0x400723: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid read of size 8
==3890==    at 0x400890: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400DAA: _debug_foo_bar (in /home/stephen/debug/test)
==3890==    by 0x400723: main (in /home/stephen/debug/test)
==3890==  Address 0x51f2fd0 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400DAA: _debug_foo_bar (in /home/stephen/debug/test)
==3890==    by 0x400723: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x400984: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400CCA: _debug_foobaz (in /home/stephen/debug/test)
==3890==    by 0x400732: main (in /home/stephen/debug/test)
==3890==  Address 0x51f3500 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400CCA: _debug_foobaz (in /home/stephen/debug/test)
==3890==    by 0x400732: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid write of size 8
==3890==    at 0x40099B: strsplit (in /home/stephen/debug/test)
==3890==    by 0x400867: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400CCA: _debug_foobaz (in /home/stephen/debug/test)
==3890==    by 0x400732: main (in /home/stephen/debug/test)
==3890==  Address 0x51f3508 is 7 bytes after a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400CCA: _debug_foobaz (in /home/stephen/debug/test)
==3890==    by 0x400732: main (in /home/stephen/debug/test)
==3890== 
==3890== Invalid read of size 8
==3890==    at 0x400890: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400CCA: _debug_foobaz (in /home/stephen/debug/test)
==3890==    by 0x400732: main (in /home/stephen/debug/test)
==3890==  Address 0x51f3500 is 0 bytes inside a block of size 1 alloc'd
==3890==    at 0x4C29DB4: calloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3890==    by 0x400852: debug_enabled (in /home/stephen/debug/test)
==3890==    by 0x400CCA: _debug_foobaz (in /home/stephen/debug/test)
==3890==    by 0x400732: main (in /home/stephen/debug/test)
==3890== 
==3890== 
==3890== HEAP SUMMARY:
==3890==     in use at exit: 145 bytes in 75 blocks
==3890==   total heap usage: 80 allocs, 5 frees, 405 bytes allocated
==3890== 
==3890== LEAK SUMMARY:
==3890==    definitely lost: 145 bytes in 75 blocks
==3890==    indirectly lost: 0 bytes in 0 blocks
==3890==      possibly lost: 0 bytes in 0 blocks
==3890==    still reachable: 0 bytes in 0 blocks
==3890==         suppressed: 0 bytes in 0 blocks
==3890== Rerun with --leak-check=full to see details of leaked memory
==3890== 
==3890== For counts of detected and suppressed errors, rerun with: -v
==3890== ERROR SUMMARY: 120 errors from 15 contexts (suppressed: 2 from 2)
stephenmathieson commented 10 years ago

@jwerle we could use something like occurrences.c, but that will make a noticeable performance impact :/