Skip to content

Fix #15215 (misra.py: analyse_ctu_info() does not close the ctu-info files) - #9025

Open
g-chauvel wants to merge 1 commit into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-info-close
Open

g-chauvel wants to merge 1 commit into
cppcheck-opensource:mainfrom
g-chauvel:misra-ctu-info-close

Conversation

@g-chauvel

Copy link
Copy Markdown
Contributor

analyse_ctu_info() does not close the ctu-info files. This gives a ResourceWarning in Python dev mode.

PYTHONDEVMODE=1 cppcheck -q --addon=misra addons/test/misra/misra-ctu-1-test.c addons/test/misra/misra-ctu-2-test.c

Before PR:

:0:0: error: Bailing out from analysis: Whole program analysis failed: Failed to execute 'python3.exe ...\addons\runaddon.py ...\addons\misra.py --cli 22364.ctu-info'. ...\addons\misra.py:4917: ResourceWarning: unclosed file <_io.TextIOWrapper name='22364.ctu-info' mode='rt' encoding='utf-8'> [internalError]

After PR: no output.

…files)

Close the ctu-info files after reading them.
The files were only closed by the garbage collector, which gives a
ResourceWarning.

@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 problems. The change fixes the ResourceWarning: each ctu-info file is now closed after it has been read, and also when an exception is raised partway through a file. Results without dev mode are the same as before. There are no earlier review comments.

Testing

  • Build: ran make -j8 cppcheck in pr/, which succeeded.
  • Unit tests: ran PYTHONPATH=addons python3 -m pytest -q addons/test/misra_test.py. All 17 passed.
  • CTU case from the test file header: ran PYTHONDEVMODE=1 ./cppcheck -q --enable=information --enable=style --addon=misra --inline-suppr addons/test/misra/misra-ctu-*-test.c, swapping in the base misra.py for the comparison.
    • Base: prints Whole program analysis failed ... misra.py:4917: ResourceWarning: unclosed file <_io.TextIOWrapper name='739.ctu-info' ...> [internalError], followed by about 60 lines of fallout (unmatched suppressions, "There was critical errors").
    • PR: only the normal Active checkers line.
  • Same files without dev mode: base and PR give identical output.
  • Exception while reading: I checked that the file is closed when an exception is raised partway through a file. I called MisraChecker.analyse_ctu_info() directly from a script, with warnings.simplefilter('error', ResourceWarning) and python3 -X dev, on a malformed ctu-info file:
    {"summary":"MisraTypedefInfo","data":[{"nope":1}]}
    
    This raises KeyError 'name' in the loop.
    • Base: reports ResourceWarning: unclosed file <... name='bad.ctu-info' ...>.
    • PR: only the KeyError, with no warning. The generator is closed while the stack unwinds.
  • Missing file: the generator raises FileNotFoundError on the first iteration, so the existing except FileNotFoundError still catches it.

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

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.

2 participants