jwerle / b64.c

Base64 encode/decode
MIT License
270 stars 102 forks source link

tests failing when run via valgrind(1) #6

Open stephenmathieson opened 9 years ago

stephenmathieson commented 9 years ago
stephenmathieson at UTA in ~/repos/github.com/littlstar/b64.c on master
$ valgrind ./test
==1936== Memcheck, a memory error detector
==1936== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==1936== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==1936== Command: ./test
==1936== 
==1936== Invalid write of size 1
==1936==    at 0x400E13: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==1936==    by 0x400815: main (in ~/repos/github.com/littlstar/b64.c/test)
==1936==  Address 0x51f1128 is 0 bytes after a block of size 8 alloc'd
==1936==    at 0x4C2B7B2: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1936==    by 0x400DED: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==1936==    by 0x400815: main (in ~/repos/github.com/littlstar/b64.c/test)
==1936== 
==1936== Invalid write of size 1
==1936==    at 0x400E4D: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==1936==    by 0x400815: main (in ~/repos/github.com/littlstar/b64.c/test)
==1936==  Address 0x51f11ca is 0 bytes after a block of size 10 alloc'd
==1936==    at 0x4C2B7B2: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1936==    by 0x400E3D: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==1936==    by 0x400815: main (in ~/repos/github.com/littlstar/b64.c/test)
==1936== 
==1936== Conditional jump or move depends on uninitialised value(s)
==1936==    at 0x400826: main (in ~/repos/github.com/littlstar/b64.c/test)
==1936== 
==1936== Conditional jump or move depends on uninitialised value(s)
==1936==    at 0x400839: main (in ~/repos/github.com/littlstar/b64.c/test)
==1936== 
test: test.c:28: main: Assertion `0 == strcmp("YnJhZGxleQ==", (char *) b64_encode((const unsigned char *) "bradley", strlen((char *) (const unsigned char *) "bradley")))' failed.
==1936== 
==1936== HEAP SUMMARY:
==1936==     in use at exit: 13 bytes in 1 blocks
==1936==   total heap usage: 11 allocs, 10 frees, 643 bytes allocated
==1936== 
==1936== LEAK SUMMARY:
==1936==    definitely lost: 13 bytes in 1 blocks
==1936==    indirectly lost: 0 bytes in 0 blocks
==1936==      possibly lost: 0 bytes in 0 blocks
==1936==    still reachable: 0 bytes in 0 blocks
==1936==         suppressed: 0 bytes in 0 blocks
==1936== Rerun with --leak-check=full to see details of leaked memory
==1936== 
==1936== For counts of detected and suppressed errors, rerun with: -v
==1936== Use --track-origins=yes to see where uninitialised values come from
==1936== ERROR SUMMARY: 6 errors from 4 contexts (suppressed: 2 from 2)
Aborted
stephenmathieson at UTA in ~/repos/github.com/littlstar/b64.c on master
$ 
jwerle commented 9 years ago

wtf

stephenmathieson commented 9 years ago

yeah, not only that, but it looks like the realloc()s are bad :/

jwerle commented 9 years ago

yeah.. this needs to be refactored this is bad

this would probably solve it:

tmp = (char *) realloc(enc, size + 4);
if (NULL != tmp) {
  enc = tmp;
}
stephenmathieson commented 9 years ago

lemme check

stephenmathieson commented 9 years ago

nope, still broken:

$ valgrind --leak-check=full ./test
==2060== Memcheck, a memory error detector
==2060== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==2060== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==2060== Command: ./test
==2060== 
==2060== Invalid write of size 1
==2060==    at 0x400A9E: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==2060==    by 0x400847: main (in ~/repos/github.com/littlstar/b64.c/test)
==2060==  Address 0x51f1128 is 0 bytes after a block of size 8 alloc'd
==2060==    at 0x4C2B7B2: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2060==    by 0x400A78: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==2060==    by 0x400847: main (in ~/repos/github.com/littlstar/b64.c/test)
==2060== 
==2060== Invalid write of size 1
==2060==    at 0x400AD8: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==2060==    by 0x400847: main (in ~/repos/github.com/littlstar/b64.c/test)
==2060==  Address 0x51f11ca is 0 bytes after a block of size 10 alloc'd
==2060==    at 0x4C2B7B2: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2060==    by 0x400AC8: b64_encode (in ~/repos/github.com/littlstar/b64.c/test)
==2060==    by 0x400847: main (in ~/repos/github.com/littlstar/b64.c/test)
==2060== 
==2060== Conditional jump or move depends on uninitialised value(s)
==2060==    at 0x4C2CBF4: __GI_strcmp (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2060==    by 0x40085E: main (in ~/repos/github.com/littlstar/b64.c/test)
==2060== 
==2060== Conditional jump or move depends on uninitialised value(s)
==2060==    at 0x400861: main (in ~/repos/github.com/littlstar/b64.c/test)
==2060== 
[...]
jwerle commented 9 years ago

hmm...! did you fix all `realloc() calls ?

brendanashworth commented 9 years ago

also getting this issue on OS X so +1

stephenmathieson commented 9 years ago

we should probably refactor to not rely on realloc and just compute the length of the encoded/decoded arrays beforehand so we just malloc once.

if a refactor is cool with you, i'll try to get something put together once i get some time together

jwerle commented 9 years ago

@stephenmathieson yeah absolutely

denji commented 9 years ago

Here found some leaks b64.c (https://github.com/littlstar/b64.c/commit/c58610f57badf248d2a0a04788867cb65a28057b)

[encode.c:41]: (error) Common realloc mistake: 'enc' nulled but not freed upon failure
[encode.c:66]: (error) Common realloc mistake: 'enc' nulled but not freed upon failure
[encode.c:73]: (error) Common realloc mistake: 'enc' nulled but not freed upon failure
[encode.c:79]: (error) Common realloc mistake: 'enc' nulled but not freed upon failure
[decode.c:61]: (error) Common realloc mistake: 'dec' nulled but not freed upon failure
[decode.c:95]: (error) Common realloc mistake: 'dec' nulled but not freed upon failure
[decode.c:102]: (error) Common realloc mistake: 'dec' nulled but not freed upon failure