I am not sure this is already tracked by a bug ticket.
GCC and cppcheck do not detect that entries_bad_format is used as unitialized variable in case of for loops not being executed.
Simplified code snippet:
#include<stdio.h>#include<stdbool.h>intmain(){boolentries_bad_format;unsignedintn_entries=0;// In real example can be assigned by anything mostly > 0 but could be 0 (json_array_size)for(size_ti=0;i<n_entries;i++){entries_bad_format=true;}if(entries_bad_format)printf("hello");return0;}
gcc -Wall -Wextra test.c -o test
cppcheck test.c --enable=all
GCC:
test.c: In function ‘main’:
test.c:9:4: warning: ‘entries_bad_format’ is used uninitialized in this function [-Wuninitialized]
9 | if(entries_bad_format)
| ^
Sorry for the inconvenience.
Apparantly I was running it with 1.90, indeed on 2.4.1 this particular simplified case above is fixed. In our case the n_entries is filled in by size_t json_array_size(const json_t *array (jansson lib) and as cppcheck does not know that lib (no lib cfg yet) it fails to find it propably?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I am not sure this is already tracked by a bug ticket.
GCC and cppcheck do not detect that entries_bad_format is used as unitialized variable in case of for loops not being executed.
Simplified code snippet:
gcc -Wall -Wextra test.c -o test
cppcheck test.c --enable=all
When modifying code to:
GCC:
test.c: In function ‘main’:
test.c:9:4: warning: ‘entries_bad_format’ is used uninitialized in this function [-Wuninitialized]
9 | if(entries_bad_format)
| ^
cppcheck:
Checking test.c ...
test.c:9:5: error: Uninitialized variable: entries_bad_format [uninitvar]
if(entries_bad_format)
^
Thank you for making cppcheck better every day!
Kind regards,
Pieter
Last edit: Pieter D 2021-06-30
hmm it works for me from cppcheck-2.4. which version do you have?
Sorry for the inconvenience.
Apparantly I was running it with 1.90, indeed on 2.4.1 this particular simplified case above is fixed. In our case the n_entries is filled in by
size_t json_array_size(const json_t *array
(jansson lib) and as cppcheck does not know that lib (no lib cfg yet) it fails to find it propably?