Skip to content

Fix comment and string handling in the memory-function check#2488

Draft
mohamadm0meni wants to merge 1 commit into
open-quantum-safe:mainfrom
mohamadm0meni:fix-memory-check-comment-parsing
Draft

Fix comment and string handling in the memory-function check#2488
mohamadm0meni wants to merge 1 commit into
open-quantum-safe:mainfrom
mohamadm0meni:fix-memory-check-comment-parsing

Conversation

@mohamadm0meni

@mohamadm0meni mohamadm0meni commented Jul 4, 2026

Copy link
Copy Markdown

test_memory_functions tracked comments line by line, which had holes: a line containing both /* and */ was skipped entirely (free(p); /* cleanup */ passed the check), a /* inside a string literal flipped the tracker into comment state and hid everything up to the next */, and free (p) with a space before the parenthesis never matched. It also flagged names that only appear in comments or strings, which would force pointless IGNORE annotations.

Replaced that with a single pass that blanks out comments and string/character literals (newlines kept, so line numbers stay valid) and searches the result with one combined pattern. // IGNORE memory-check behaves as before. The \b_<func>\( exclusion is dropped: \b never matches between two word characters, so it had no effect. The checker is factored into its own function with parametrized unit tests; those run in the existing code conventions CI job. src/ is still clean under the stricter parser, no new annotations needed.

Doesn't close an issue, but related to #2179: adding memset to this check needs a parser that doesn't miss code next to comments or flag names inside them.

  • Does this PR change the input/output behaviour of a cryptographic algorithm (i.e., does it change known answer test values)? (If so, a version bump will be required from x.y.z to x.(y+1).0.)
  • Does this PR change the list of algorithms available -- either adding, removing, or renaming? Does this PR otherwise change an API? (If so, PRs in fully supported downstream projects dependent on these, i.e., oqs-provider will also need to be ready for review and merge by the time this is merged. Also, make sure to update the list of algorithms in the continuous benchmarking files: .github/workflows/kem-bench.yml and sig-bench.yml)

Blank out comments and string/character literals in a single pass before searching for standard memory functions, replacing the line-based state machine that skipped code sharing a line with a comment, lost track of state on comment markers inside string literals, missed calls like 'free (p)', and flagged names inside comments and strings. Add unit tests for the checker itself.

Related: open-quantum-safe#2179
Signed-off-by: Mohamad Momeni <mohamadmomeni@eng.uk.ac.ir>
@mohamadm0meni mohamadm0meni force-pushed the fix-memory-check-comment-parsing branch from 9131523 to a01d0e7 Compare July 4, 2026 15:53
@mohamadm0meni

Copy link
Copy Markdown
Author

Hello,
As this is my first PR here, the CI workflows are pending manual approval. Could a maintainer please trigger them?
I will mark the PR as ready for review once the tests pass.
Thank you.

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.

1 participant