Open stephenmathieson opened 9 years ago
wtf
yeah, not only that, but it looks like the realloc()
s are bad :/
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;
}
lemme check
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==
[...]
hmm...! did you fix all `realloc() calls ?
also getting this issue on OS X so +1
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
@stephenmathieson yeah absolutely
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