Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return failure rather than abort()ing on out-of-memory conditions. #18

Merged
merged 1 commit into from
Feb 27, 2014

Conversation

achurch
Copy link
Contributor

@achurch achurch commented Feb 26, 2014

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);

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);
kinetiknz added a commit that referenced this pull request Feb 27, 2014
Return failure rather than abort()ing on out-of-memory conditions.
@kinetiknz kinetiknz merged commit 3f9c26a into mozilla:master Feb 27, 2014
@kinetiknz
Copy link
Collaborator

Awesome, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants