Skip to content

fix: heap-buffer-overflow in prime factorization (Range allocation size)#1574

Closed
SAY-5 wants to merge 1 commit into
TheAlgorithms:masterfrom
SAY-5:fix-prime-factorization-heap-overflow
Closed

fix: heap-buffer-overflow in prime factorization (Range allocation size)#1574
SAY-5 wants to merge 1 commit into
TheAlgorithms:masterfrom
SAY-5:fix-prime-factorization-heap-overflow

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented May 12, 2026

Fixes #1568.

int_fact() allocated the Range struct with malloc(sizeof(range)), where range is an int * local variable, so only pointer-size bytes were allocated. Writing pstr->length then ran past the end of the allocation, which AddressSanitizer flags as a heap-buffer-overflow.

Changed to malloc(sizeof(*pstr)) so the full struct data is allocated.

Built locally with -fsanitize=address,undefined; ASan no longer reports the overflow.

malloc(sizeof(range)) allocated only pointer size instead of the full
struct, causing a heap-buffer-overflow when writing pstr->length.
Use sizeof(*pstr) so the whole struct is allocated. Fixes TheAlgorithms#1568.
@SAY-5
Copy link
Copy Markdown
Author

SAY-5 commented May 12, 2026

Closing as duplicate of my earlier #1573.

@SAY-5 SAY-5 closed this May 12, 2026
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.

[BUG] math/prime_factoriziation.c type error

1 participant