11

Avoid this Realloc Anti-pattern! It Can Leak Memory.

Do not use this:

foo = realloc(foo, ...);

Instead do this:

ret = realloc(foo, ...);
if (ret == NULL) {
    // [...] handle error
    free(foo);
    return ENOMEM;
}
foo = ret;
#SecureCoding #C/C++
Avoid the anti-pattern foo = realloc(foo, ...);. On failure, realloc returns NULL, but leaves the original buffer untouched. So this anti pattern risks losing the reference to the original buffer, causing a memory leak.
Icon with a waving hand

Get in touch

sigma star gmbh
Eduard-Bodem-Gasse 6, 1st floor
6020 Innsbruck | Austria

sigma star gmbh logo