Skip to content

Update tools/MT-Unsafe.py to find all the unsafe functions listed in man elf_flagelf - #8642

Open
andrew-aitchison wants to merge 3 commits into
cppcheck-opensource:mainfrom
andrew-aitchison:many.unsafe.functions.one.line
Open

andrew-aitchison wants to merge 3 commits into
cppcheck-opensource:mainfrom
andrew-aitchison:many.unsafe.functions.one.line

Conversation

@andrew-aitchison

Copy link
Copy Markdown
Contributor

Update tools/MT-Unsafe.py to find all the unsafe functions listed in man elf_flagelf

  • this man page lists several unsafe functions on one source line.

Update addons/threadsafety.py to include these functions.

@danmar

danmar commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

I would like that the PR title is not chopped like this.

it wouldn't hurt with a line in releasenotes to mention this improvement.

testing: I am not sure what we can test but feel free to create a regression test if you can. Provide a made up input to MT-Unsafe.py and check that it outputs certain symbols?

@andrew-aitchison
andrew-aitchison marked this pull request as draft June 20, 2026 16:09
@andrew-aitchison

Copy link
Copy Markdown
Contributor Author

Title fixed.
The other two suggestions are also good.

@andrew-aitchison

Copy link
Copy Markdown
Contributor Author

testing: I am not sure what we can test but feel free to create a regression test if you can. Provide a made up input to MT-Unsafe.py and check that it outputs certain symbols?

I have an input for MT-Unsafe.py and an expected output.
testrunner is probably not a good place to run a python script; where should I add this to the regression tests ?

@andrew-aitchison andrew-aitchison changed the title Update tools/MT-Unsafe.py to find all the unsafe functions listed in Update tools/MT-Unsafe.py to find all the unsafe functions listed in man elf_flagelf Jul 4, 2026
@andrew-aitchison
andrew-aitchison force-pushed the many.unsafe.functions.one.line branch from 619935e to c28bcf1 Compare August 29, 2026 18:22
@andrew-aitchison
andrew-aitchison marked this pull request as ready for review August 30, 2026 10:11
@danmar

danmar commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

testrunner is probably not a good place to run a python script; where should I add this to the regression tests ?

that is correct.

I suggest some pytest testing in addons/test folder. there is no threadsafety_test.py there now which I spontanously feel is weird. but there is a threadsafety folder there I don't know how that is used.

https://github.com/cppcheck-opensource/cppcheck/tree/main/addons/test

@andrew-aitchison

Copy link
Copy Markdown
Contributor Author

I suggest some pytest testing in addons/test folder. there is no threadsafety_test.py there now which I spontanously feel is weird. but there is a threadsafety folder there I don't know how that is used.

Hmm. Looks like I did that. I'll have to figure out what I was thinking ...

@andrew-aitchison
andrew-aitchison force-pushed the many.unsafe.functions.one.line branch from 7732ae4 to 1e35010 Compare October 25, 2026 10:36
  man elf_flagelf
Also update addons/threadsafety.py to include these functions.

This man page lists several unsafe functions on one line.
@andrew-aitchison
andrew-aitchison force-pushed the many.unsafe.functions.one.line branch from 1e35010 to 856869c Compare October 25, 2026 10:47

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI review

The new .BR parsing in tools/MT-Unsafe.py turns troff escapes it doesn't handle into bad entries. Real man pages use \% and quoted arguments, and the base version simply skipped such lines. The PR now outputs names like '%pthread_attr_setaffinity_np', '"strtok"' and 'foo_r"and"bar'. Since this output gets merged into addons/threadsafety.py, those entries would be wrong there. Separately, the new regression input test/tools/MT-Unsafe.3 plus its .expected file is not run by any test, so it does not protect against regressions yet. The six elf_flag* names added to addons/threadsafety.py look correct.

Testing

This change is Python only, so I didn't build the C++ binaries.

  • python3 tools/MT-Unsafe.py test/tools/MT-Unsafe.3 | diff - test/tools/MT-Unsafe.3.expected gives no diff, as the PR intends.
  • The same file gzipped (gzip -c test/tools/MT-Unsafe.3 > t.3.gz) gives output that differs from .expected: the '' type entry is missing. This is minor because it only affects the types list.
  • I wrote scratch/a.3 with attribute-table rows in the styles man-pages uses, and ran it through both base and PR:
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR \%pthread_attr_setaffinity_np (),
.BR \%pthread_attr_getaffinity_np ()
T}	Thread safety	MT-Unsafe env
T{
.BR "strtok" ()
T}	Thread safety	MT-Unsafe race:strtok
T{
.BR foo_r " and " bar ()
T}	Thread safety	MT-Unsafe race:x
.TE

python3 pr/tools/MT-Unsafe.py a.3 outputs these API entries:

    '"strtok"',
    '%pthread_attr_getaffinity_np',
    '%pthread_attr_setaffinity_np',
    'foo_r"and"bar',

The .gz version of the file gives the same result. Base output contained only foo_r from these rows, so base missed them but produced no garbage.


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

Comment thread tools/MT-Unsafe.py
dprint(1, 'function_names 1a:\n\t%s\n' % (function_names))
function_names = function_names.replace(r'\n', '')
dprint(1, 'function_names 2:\n\t%s\n' % (function_names))
function_names = re.sub(r"[~() '\\\n]+", '', function_names)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI review

This character class only strips ~ ( ) space ' \\ and newline, so other troff syntax ends up in the function names.

  • .BR \%pthread_attr_setaffinity_np (), gives '%pthread_attr_setaffinity_np'. The \% hyphenation escape is common in current man-pages.
  • .BR "strtok" () gives '"strtok"'.
  • .BR foo_r " and " bar () gives 'foo_r"and"bar'.

The old regex just skipped these lines; now they produce invalid identifiers that would be merged into threadsafety.py. Extracting identifiers with something like re.findall(r'[A-Za-z_]\w*(?=\s*\\?\(|\s*"?\s*\(|,|$)', ...), or at least stripping \% and " before the split, would avoid this.

Comment thread test/tools/MT-Unsafe.3
.I test/tools/MT-Unsafe.3.expected\fR.
E.g.:

.I tools/MT-Unsafe.py test/tools/MT-Unsafe.3 | diff - test/tools/MT-Unsafe.3.expected

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

AI review

Nothing runs this comparison: no pytest or CI step uses MT-Unsafe.3 / MT-Unsafe.3.expected, so it is only a manual check. A small pytest that runs the script and compares its stdout with the expected file (as danmar suggested for addons/test) would make it a real regression test. Note that running it on a gzipped copy of this page gives different output: the '' type entry is missing.

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