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

Integer overflow in array allocations #32

Closed
mstewartgallus opened this issue Jul 28, 2014 · 1 comment
Closed

Integer overflow in array allocations #32

mstewartgallus opened this issue Jul 28, 2014 · 1 comment

Comments

@mstewartgallus
Copy link

malloc(n * size) is an antipattern as n * size can overflow (especially from an attacker). You'll want to use calloc or create your own malloc_array function. Something like the following could work:

static inline void * malloc_array(size_t nmemb, size_t size)
{
    /* Make the check so that a constant size deletes the zero check
     * and the division.
     */
    if (size != 0u && SIZE_MAX / size < nmemb) {
        errno = ENOMEM;
        return NULL;
    }

    return malloc(size * nmemb);
}
@mstewartgallus
Copy link
Author

Nevermind, all the allocations use constant sizes.

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

No branches or pull requests

1 participant