Skip to content

add fix for findGotoLabel in misra.py - #8876

Merged
danmar merged 1 commit into
cppcheck-opensource:mainfrom
driftregion:main
Nov 7, 2026
Merged

danmar merged 1 commit into
cppcheck-opensource:mainfrom
driftregion:main

Conversation

@driftregion

Copy link
Copy Markdown
Contributor

Hello. Thank you for maintaining cppcheck.

While testing my library with the misra.py addon, I found that it gave false positives when the closing brace of an initializer list was mistaken for the closing brace of a function. This PR adds a failing test case and the code to make it pass.

Comment thread addons/misra.py
Comment on lines +1179 to 1180
if functionEnd is None and tok.str == '}' and tok.scope and tok.scope.type == 'Function':
break

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.

The fix looks correct to me. I ran the CI commands (cppcheck --dump -DDUMMY ... misra/misra-test.c --std=c89 --platform=unix64 + misra.py -verify): with the old misra.py the new test gives Not expected: misra/misra-test.c:1476 15.2, and with this PR it passes. This was the only place in misra.py that used the '}' + scope.type == 'Function' heuristic.

Optional: a goto is always inside a function, so functionScope should never be None here. The fallback keeps the old heuristic that caused this bug. Maybe drop it and just stop at functionEnd, to keep the function simpler.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no new problems. The fix makes findGotoLabel stop at the real bodyEnd of the enclosing function scope. Before, it stopped at the first } whose scope was Function, and that also matched the closing brace of an initializer list. bodyEnd comes from cppcheckdata's IdMap, so it is the same Token object, and the is comparison works. The only open point is the optional suggestion to drop the None fallback, which is already in comments.md.

Testing

  • I built cppcheck from pr/.
  • I ran the misra CI verification: cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test.c --std=c89 --platform=unix64, then misra.py -verify misra/misra-test.c.dump. It passed with no "Not expected" or "Not found" lines.
  • PYTHONPATH=addons:. python3 -m pytest addons/test/misra_test.py: 17 passed.
  • I wrote my own tests in scratch/a.c. All gave the expected results:
    • Nested initializer { {1,2}, 3 } inside a block between the goto and the label: no false 15.2.
    • goto whose label exists only in the next function: 15.2 is reported correctly. The search does not leak past the function end.
    • Label inside a nested block after an array initializer: 15.3 is reported correctly.

Automated review of ea8ef16 by Claude. The pull request was built and tested locally.

@danmar
danmar merged commit 9c0f147 into cppcheck-opensource:main Nov 7, 2026
71 of 72 checks passed
chrchr-github pushed a commit to chrchr-github/cppcheck that referenced this pull request Nov 8, 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.

3 participants