Repository navigation
add fix for findGotoLabel in misra.py - #8876
Conversation
| if functionEnd is None and tok.str == '}' and tok.scope and tok.scope.type == 'Function': | ||
| break |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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
cppcheckfrompr/. - I ran the misra CI verification:
cppcheck --dump -DDUMMY --suppress=uninitvar --inline-suppr misra/misra-test.c --std=c89 --platform=unix64, thenmisra.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.
- Nested initializer
Automated review of ea8ef16 by Claude. The pull request was built and tested locally.
Hello. Thank you for maintaining cppcheck.
While testing my library with the
misra.pyaddon, 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.