Skip to content

fix(math): fix off-by-one heap buffer overflow in prime_sieve - #1604

Open
SaifAli-S wants to merge 1 commit into
TheAlgorithms:masterfrom
SaifAli-S:fix-prime-sieve-bounds
Open

SaifAli-S wants to merge 1 commit into
TheAlgorithms:masterfrom
SaifAli-S:fix-prime-sieve-bounds

Conversation

@SaifAli-S

Copy link
Copy Markdown

Description of Change

Fixes #1599

math/prime_sieve.c previously used <= on MAX_SIZE and size, which caused both prime() and count() to index p[MAX_SIZE] and arr[size] respectively (1 element past the end of a buffer holding MAX_SIZE or size elements). This triggered an AddressSanitizer (ASan) heap-buffer-overflow error when allocated without padding.

This PR corrects the loop conditions:

  • i < MAX_SIZE and j < MAX_SIZE in prime()
  • i < size in count()
  • Updated test() with a second test case verifying prime counts up to 100 (25 primes).

References

Closes #1599

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: Fixes off-by-one heap buffer overflow in prime_sieve.c identified in #1599.

This branch has not been deployed

No deployments
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.

prime_sieve.c: off-by-one in prime() and count() - heap-buffer-overflow (ASan confirms)

1 participant