Repository navigation
Mfactor: use CALLOC for the allocations #88 missed (factor_test.h, bit_map2) - #393
Conversation
…_test.h, bit_map2) primesearch#88 added the checked MALLOC/CALLOC/REALLOC macros and converted the bare allocation sites, but missed the 24 calloc() calls in factor_test.h (the Mfactor self-test) and the second bit-map array in factor.c. Neither checks the result, so an allocation failure is a NULL dereference (SIGSEGV) instead of the clean "out of memory" abort the other sites give. With a calloc() that returns NULL on request (LD_PRELOAD shim): before, failing the first calloc in the self-test (factor_test.h:335) crashes in mi64_nega with SIGSEGV, and failing the second calloc(4255, 8) (bit_map2) does too; the first, bit_map, which has its own check, gives a clean abort. After, both abort with the file, line and function. The sites left as they are, and why: allocations in disabled code (the TEST_TRIALDIV block in factor.c, a commented-out test in util.c, the CUDA-only code in util.c) and the ones that already check the result. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Follow-up to #88, from tdulcet's comment there about unchecked allocations that were missed (
factor_test.h).#88 added the checked
MALLOC/CALLOC/REALLOCmacros and converted the bare allocation sites, but missed two groups, both in Mfactor:calloc()calls infactor_test.h(the Mfactor self-test that runs at every start), andfactor.c(bit_map2, allocated right next tobit_map, which has its own check).Neither checks the result, so an allocation failure is a NULL dereference (SIGSEGV) instead of the "out of memory" abort with file, line and function that the other sites give. This changes those 25
calloccalls toCALLOC; nothing else.Testing
I used an
LD_PRELOADshim that makes the Nthcallocof a given size return NULL, and ranMfactor -m 521 -bmax 20(nosimd build):callocin the self-test (factor_test.h:335)mi64_nega(backtrace confirms the NULLq)ERROR: Function test_fac, at line 335 of file ../src/factor_test.h/calloc of 80 bytes failed - out of memorycalloc(4255, 8)(bit_map2)ERROR: Function main, at line 1521 of file ../src/factor.c/calloc of 34040 bytes failed - out of memorycalloc(4255, 8)(bit_map, already checked)The Mfactor build has the same 127 warnings before and after, and the Mlucas build (nosimd) still builds.
What I left alone
I went through every
malloc/calloc/reallocinsrc/on main. The rest either check the result already (theptr_prodpattern in the radix files, the inlineif(!x)checks, theASSERTs) or sit in code that is not built: theTEST_TRIALDIVblock infactor.c, a commented-out test inutil.c, and the CUDA-only code inutil.c, as in #88.Overlap with other open PRs
No open PR makes this change. #372 and #324 convert other
calloccalls infactor.c(different lines from these). Seven open PRs touchfactor_test.h(#213, #215, #223, #314, #331, #374, #380) and this merges cleanly with each of them one at a time.🤖 Generated with Claude Code