Repository navigation
Conversation
moocore_malloc() computed nmemb * size without checking whether the product wraps around, so a huge nmemb could quietly ask malloc() for a tiny buffer. Reject it up front, the same way reallocarray() does, and report it through moocore_perror() like a failed allocation. This resolves the FIXME in common.h and is a first step for multi-objective#11.
Contributor
|
Thanks! I would prefer to use the standard ckd_mul in <stdckdint.h> if that is available. See https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf The use of the standard or the builtin saves one multiplication: https://godbolt.org/z/vqPeKcbea When the minimum required C version becomes C23, we can delete the fall-backs. |
Use <stdckdint.h> where available, fall back to __builtin_mul_overflow, and to a manual check otherwise. In eaf_create(), pass maxsize as nmemb so the wrapper checks that product.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This picks off the FIXME in
moocore_malloc()("Check multiplication overflow"), which is the first item in #11.Right now
nmemb * sizeis passed straight tomalloc(). If the product wraps around, you get a small buffer back and a very confused program a few lines later. This adds the usualnmemb > SIZE_MAX / sizecheck (same idea asreallocarray()), and reports it throughmoocore_perror(), so it behaves like any other failed allocation, including under R where that turns into an R error.I kept it deliberately small: one function, no changes to any call site. I didn't touch the remaining raw
malloc/realloccalls. Happy to do those in follow-ups if you want, say amoocore_realloc()and then converting call sites file by file, so each PR stays easy to review.How I checked it, on Ubuntu with
make all DEBUG=1(so ASan and-Wall -Wextra -Wconversion): it builds without new warnings,make testpasses, a call likemoocore_malloc(SIZE_MAX/2, 4, ...)now stops withsize overflowand exit code 1, and a normalMOOCORE_MALLOC(10, int)works as before.Refs #11