Skip to content

fix: allocate sizeof(struct data) in prime factoriziation#1573

Open
SAY-5 wants to merge 1 commit into
TheAlgorithms:masterfrom
SAY-5:fix-prime-factorization-sizeof-typo
Open

fix: allocate sizeof(struct data) in prime factoriziation#1573
SAY-5 wants to merge 1 commit into
TheAlgorithms:masterfrom
SAY-5:fix-prime-factorization-sizeof-typo

Conversation

@SAY-5
Copy link
Copy Markdown

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

Description of Change

In math/prime_factoriziation.c::int_fact, the local int *range shadows the range typedef on the line above, so malloc(sizeof(range)) reserves only sizeof(int *) bytes (8 on a 64-bit system) instead of sizeof(struct data) (16). Writing pstr->length = count later in the same function overflows the heap allocation.

AddressSanitizer reports the issue on the assignment, matching the report in #1568. After replacing the argument with sizeof(struct data) (one of the two suggested fixes in the issue), ASan runs clean.

References

Closes #1568

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added tests and example, test must pass
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • Search previous suggestions before making a new one, as yours may be a duplicate.
  • I acknowledge that all my contributions will be made under the project's license.

Notes: One-line fix to a heap-buffer-overflow in the prime factoriziation example.

Inside int_fact, the local 'int *range' shadows the 'range' typedef, so
malloc(sizeof(range)) reserves only sizeof(int*) bytes. Writing
pstr->length one line later overflows the heap allocation. Reproduces
under AddressSanitizer.

Allocate sizeof(struct data) instead, which the issue author suggested
as a working fix.

Closes TheAlgorithms#1568

Signed-off-by: SAY-5 <say.apm35@gmail.com>
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