Repository navigation
Conversation
Contributor
|
See #107 (comment) and #11 (comment) |
Contributor
|
There shouldn't be unittests within The code (and possible failures) should be exercised using the extended testsuite (https://github.com/multi-objective/testsuite) or the Python/R wrappers. |
…bjective#11) * c/common.h (moocore_mul_overflow): New. (moocore_malloc): Stop with an error if nmemb * size overflows size_t. Do not report a NULL result of malloc(0) as a failure. (moocore_calloc, moocore_realloc): New. (MOOCORE_CALLOC, MOOCORE_REALLOC): New. * c/*.c, c/*.h, r/src/Rmoocore.c, r/src/Rcommon.h: Replace malloc(), calloc() and realloc() by MOOCORE_MALLOC(), MOOCORE_CALLOC() and MOOCORE_REALLOC(). Keep products that were computed in size_t in size_t. Remove NULL checks that can no longer trigger. hv_inex_list() keeps its malloc() call because it returns -1 on failure, which Python reports as MemoryError. * c/test_alloc.c: New unit tests of the wrappers. * c/Makefile (unittest): New target. * .github/workflows/C.yml: Run make unittest.
Contributor
Author
|
Thanks for the feedback, @MLopez-Ibanez. I rebased onto
|
M-Colley
force-pushed
the
fix/issue-11
branch
from
December 1, 2026 09:28
f7ac4c2 to
36289dc
Compare
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.
Closes #11
Split out of #103, which now covers only #8.
Changes
moocore_malloc()now checksnmemb * sizefor overflow, using the same test as OpenBSDreallocarray(). This resolves the FIXME incommon.h. Newmoocore_calloc()andmoocore_realloc(), with the macrosMOOCORE_CALLOC()andMOOCORE_REALLOC(), apply the same checks.malloc(0)may return NULL.malloc,callocandrealloccalls inc/andr/src/now use the wrappers. A script converted 141 calls of the formn * sizeof(T), and I converted 9 by hand (bit_array.h,eaf.c,io.c,dominatedsets.c).sizeofcame first in the original expression, the product was computed insize_t. The conversion keeps it insize_twith a cast. For example,malloc(sizeof(double) * nsamples * nobj)becomesMOOCORE_MALLOC((size_t) nsamples * nobj, double), sonsamples * nobjis not computed inint.read_point(),m_strcat(),x_strndup(),lex_sort_equal_z_and_setup_nodes(), and the asserts afterreallocineaf_realloc()andcvector.h. Some of these leaked the old pointer whenreallocfailed. The caller oflex_sort_equal_z_and_setup_nodes()ignored its-1return value.hv_inex_list()inhv.creturns-1whenmallocfails,fpli_hv()passes it on, and Pythonhypervolume()raisesMemoryError. Converting it would turn thatMemoryErrorinto a process exit. I can convert it too if you prefer one rule everywhere.Behaviour on failure: in R, the wrappers call
Rf_error(), so a failed allocation now raises an R error instead of crashing R. In C and Python, they print the error and callexit(EXIT_FAILURE). Before, an unchecked NULL was dereferenced.Overlap with #103: both PRs change
whv_preprocess_rectangles()inc/whv.cand the NEWS filesc/NEWS.mdandpython/doc/source/whatsnew/index.rst, so whichever PR merges second needs a rebase that keeps the rewrittenwhv_preprocess_rectangles()from #103.How I verified it
c/test_alloc.cand amake -C c unittesttarget, which runs in both jobs ofC.yml. It checksmoocore_mul_overflow()at the boundaries. For example,2 * (SIZE_MAX/2)does not overflow and2 * (SIZE_MAX/2 + 1)does. It also checks that the wrappers return usable memory, thatreallockeeps the contents, and thatMOOCORE_CALLOCzeroes the memory. Then it calls each wrapper withSIZE_MAX/2 + 1doubles and expects theoverflows size_terror. With the overflow check disabled, 4 assertions and all 3 overflow calls fail.make -C c all DEBUG=0 WERROR=1,make -C c all DEBUG=1 WERROR=1 SANITIZERS=andmake -C c shlibs DEBUG=0 WERROR=1build without warnings.mainand from this branch. Their outputs are byte-identical for 16 runs ofhv,hvapprox,nondominated -f,eaf,igd,epsilon,dominatedsetsandndsorton files fromr/inst/extdata(2D, 3D and 8D). These runs cover the converted command-line code (read_point(),x_strndup(), theresultsblock indominatedsets.c, andeaf_realloc()).pytest --doctest-modules tests src/moocore/_moocore.py): 101 passed, 2 xfailed. The extension was built with MSVC.testthatwithNOT_CRAN=true: 0 failures, 0 errors, 0 skipped.Environment: Windows 11, Python 3.13, R 4.6.1, GCC 14.3 from Rtools 4.5, MSVC from Visual Studio 18. I have not tested on Linux or macOS locally, so CI covers those.
AI assistance was used to prepare and review this patch.
🤖 Generated with Claude Code