kinetiknz/nestegg — pull request #18 by achurch (2014-02-26 14:30:34+00:00)
Title: Return failure rather than abort()ing on out-of-memory conditions.

With this change, nestegg was verified to fail cleanly (without crashes,
invalid memory accesses, or memory leaks) for allocation failures at any
stage of processing for a simple WebM stream, using roughly:

int malloc_counter;
void *realloc(void *ptr, size_t size) {
if (--malloc_counter < 0) return NULL;
return real_realloc(ptr, size); // the libc realloc()
}
void *malloc(size_t size) {return realloc(NULL, size);}
void *calloc(size_t nmemb, size_t size) {
void *ptr = malloc(nmemb*size);
if (ptr) memset(ptr, 0, nmemb*size);
return ptr;
}
for (int i = 0; i < 10000; i++) {
malloc_counter = i;
nestegg ctx;
if (nestegg_init(&ctx, ...) != 0) continue;
if (nestegg_read_packet(&ctx, ...) > 0) break;
nestegg_destroy(&ctx);
}
assert(i < 10000);