I've been working on a large C++ program and I forgot to add my usual list of compiler flags/warnings when working on a C project. After enabling the -fanalyzer
flag, I began to get a lot of "warning: use of uninitialized value '<unknown>'" messages from GCC 12.2 throughout my code. Here is an isolated example I was able to generate in Compiler Explorer:
#include <string>
std::string square(int num) {
return std::to_string(num * num);
}
Compiler output:
<source>: In function 'std::string square(int)':
<source>:4:36: warning: use of uninitialized value '<unknown>' [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
4 | return std::to_string(num * num);
| ^
'std::string square(int)': events 1-2
|
| 3 | std::string square(int num) {
| | ^
| | |
| | (1) region created on stack here
| 4 | return std::to_string(num * num);
| | ~
| | |
| | (2) use of uninitialized value '<unknown>' here
|
<source>:4:36: warning: use of uninitialized value '<unknown>' [CWE-457] [-Wanalyzer-use-of-uninitialized-value]
4 | return std::to_string(num * num);
| ^
'std::string square(int)': events 1-2
|
| 3 | std::string square(int num) {
| | ^
| | |
| | (1) region created on stack here
| 4 | return std::to_string(num * num);
| | ~
| | |
| | (2) use of uninitialized value '<unknown>' here
|
Does this simple square
function really have such a problem? Or am I missing something bigger? Is the static analysis in GCC broken?
std::string
andstd::vector
is where I see this error, but not 100% of the time. Objects I've written myself have no such issues. – Strychninism