Skip to content

Fix #8192: FN condition always false in for loop condition#8446

Open
francois-berder wants to merge 1 commit intodanmar:mainfrom
francois-berder:pr-8192
Open

Fix #8192: FN condition always false in for loop condition#8446
francois-berder wants to merge 1 commit intodanmar:mainfrom
francois-berder:pr-8192

Conversation

@francois-berder
Copy link
Copy Markdown
Contributor

When a for loop's condition is impossible given the initial value (e.g. for (int i = 0; i > 10; i++)), cppcheck was not emitting a knownConditionTrueFalse warning.

Fix by populating memory1, memory2 and memoryAfter with the init state when the condition is immediately false (and no error occured). We can then set the value for the condition token and thus emit a knownConditionTrueFalse warning.

When a for loop's condition is impossible given the initial value (e.g.
`for (int i = 0; i > 10; i++)`), cppcheck was not emitting a
knownConditionTrueFalse warning.

Fix by populating memory1, memory2 and memoryAfter with the init state
when the condition is immediately false (and no error occured). We can
then set the value for the condition token and thus emit a
knownConditionTrueFalse warning.

Signed-off-by: Francois Berder <[email protected]>
@sonarqubecloud
Copy link
Copy Markdown

@danmar
Copy link
Copy Markdown
Owner

danmar commented Apr 16, 2026

tweaking the valueflow can have effects in various checkers..

I think it would be interesting if you run cppcheck/tools/test-my-pr.py script on 1000 or so packages and see what the changes are. Will you see more warnings, fewer warnings, which warnings..

A command like python3 tools/test-my-pr.py -p 1000 from inside your branch should work..

@francois-berder
Copy link
Copy Markdown
Contributor Author

tweaking the valueflow can have effects in various checkers..

I think it would be interesting if you run cppcheck/tools/test-my-pr.py script on 1000 or so packages and see what the changes are. Will you see more warnings, fewer warnings, which warnings..

A command like python3 tools/test-my-pr.py -p 1000 from inside your branch should work..

Unfortunately, I am having trouble testing my PR in this way. While running tools/test-my-pr.py, I keep getting connection refused error:

Cppcheck 2.21 dev
Connecting to server to get count of packages..
ConnectionRefusedError in __get_packages_count: [Errno 111] Connection refused
Trying __get_packages_count again in 5.0 seconds
Connecting to server to get count of packages..
ConnectionRefusedError in __get_packages_count: [Errno 111] Connection refused
Trying __get_packages_count again in 10.0 seconds
Connecting to server to get count of packages..
ConnectionRefusedError in __get_packages_count: [Errno 111] Connection refused
Trying __get_packages_count again in 20.0 seconds
Connecting to server to get count of packages..
ConnectionRefusedError in __get_packages_count: [Errno 111] Connection refused
Trying __get_packages_count again in 40.0 seconds
Connecting to server to get count of packages..
Maximum number of tries reached for __get_packages_count

I also tried in a different way by downloading the DACA2 packages using daca2-download.py script and then running ./tools/test-my-pr.py --packages-path ~/daca2-packages/ but it then failed at some point with the same error:

Package: /home/francois/daca2-packages/aha_0.5.1.orig.tar.xz
Removing existing temporary data...
Unpacking..
extracted 1 of 3 files (skipped 0)
Detecting library usage...
Found libraries: ['posix', 'gnu', 'bsd']
Analyze..
nice /home/francois/cppcheck-test-my-pr-workfolder/tree-main/cppcheck --library=posix --library=gnu --library=bsd  --inconclusive --enable=style,information --inline-suppr --template=daca2 --disable=missingInclude --suppress=unmatchedSuppression --check-library --debug-warnings --suppress=autoNoType --suppress=valueFlowBailout --suppress=bailoutUninitVar --suppress=symbolDatabaseWarning --suppress=normalCheckLevelConditionExpressions -D__GNUC__ --platform=unix64 -rp=/home/francois/cppcheck-test-my-pr-workfolder/temp -j1 /home/francois/cppcheck-test-my-pr-workfolder/temp
Number of issues: 12
cppcheck finished with 0 in 1.7s
Analyze..
nice /home/francois/projects/cppcheck/cppcheck --library=posix --library=gnu --library=bsd  --inconclusive --enable=style,information --inline-suppr --template=daca2 --disable=missingInclude --suppress=unmatchedSuppression --check-library --debug-warnings --suppress=autoNoType --suppress=valueFlowBailout --suppress=bailoutUninitVar --suppress=symbolDatabaseWarning --suppress=normalCheckLevelConditionExpressions -D__GNUC__ --platform=unix64 -rp=/home/francois/cppcheck-test-my-pr-workfolder/temp -j1 /home/francois/cppcheck-test-my-pr-workfolder/temp
Number of issues: 12
cppcheck finished with 0 in 1.8s
Diff results..
148 of 323 packages processed

Connecting to server to get assigned work..
ConnectionRefusedError in __get_package: [Errno 111] Connection refused
Trying __get_package again in 30.0 seconds
Connecting to server to get assigned work..
ConnectionRefusedError in __get_package: [Errno 111] Connection refused
Trying __get_package again in 30.0 seconds
Connecting to server to get assigned work..
Maximum number of tries reached for __get_package
Traceback (most recent call last):
  File "/home/francois/projects/cppcheck/tools/test-my-pr.py", line 129, in <module>
    package = lib.get_package(packages_idxs.pop())
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/francois/projects/cppcheck/tools/donate_cpu_lib.py", line 292, in get_package
    return try_retry(__get_package, fargs=(package_index,), max_tries=3, sleep_duration=30.0, sleep_factor=1.0)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/francois/projects/cppcheck/tools/donate_cpu_lib.py", line 100, in try_retry
    raise e
  File "/home/francois/projects/cppcheck/tools/donate_cpu_lib.py", line 87, in try_retry
    return fun(*fargs)
           ^^^^^^^^^^^
  File "/home/francois/projects/cppcheck/tools/donate_cpu_lib.py", line 280, in __get_package
    sock.connect(__server_address)
ConnectionRefusedError: [Errno 111] Connection refused

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