2010-10-31 12:31:11 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2022-02-05 11:45:17 +01:00
|
|
|
* Copyright (C) 2007-2022 Cppcheck team.
|
2010-10-31 12:31:11 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "check.h"
|
2015-08-08 08:53:08 +02:00
|
|
|
#include "checkuninitvar.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "ctu.h"
|
|
|
|
#include "errortypes.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "library.h"
|
2015-08-08 08:53:08 +02:00
|
|
|
#include "settings.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "testsuite.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <list>
|
2017-05-27 04:33:47 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2011-10-13 20:53:06 +02:00
|
|
|
class TestUninitVar : public TestFixture {
|
2010-10-31 12:31:11 +01:00
|
|
|
public:
|
2021-08-07 20:51:18 +02:00
|
|
|
TestUninitVar() : TestFixture("TestUninitVar") {}
|
2010-10-31 12:31:11 +01:00
|
|
|
|
|
|
|
private:
|
2014-03-24 09:59:05 +01:00
|
|
|
Settings settings;
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2022-02-10 23:02:24 +01:00
|
|
|
void run() override {
|
2014-03-24 09:59:05 +01:00
|
|
|
LOAD_LIB_2(settings.library, "std.cfg");
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
TEST_CASE(uninitvar1);
|
2019-03-02 13:17:15 +01:00
|
|
|
TEST_CASE(uninitvar_warn_once); // only write 1 warning at a time
|
2015-08-08 08:53:08 +02:00
|
|
|
TEST_CASE(uninitvar_decl); // handling various types in C and C++ files
|
2012-09-05 19:46:44 +02:00
|
|
|
TEST_CASE(uninitvar_bitop); // using uninitialized operand in bit operation
|
2010-10-31 12:31:11 +01:00
|
|
|
TEST_CASE(uninitvar_alloc); // data is allocated but not initialized
|
|
|
|
TEST_CASE(uninitvar_arrays); // arrays
|
|
|
|
TEST_CASE(uninitvar_class); // class/struct
|
|
|
|
TEST_CASE(uninitvar_enum); // enum variables
|
|
|
|
TEST_CASE(uninitvar_if); // handling if
|
|
|
|
TEST_CASE(uninitvar_loops); // handling for/while
|
|
|
|
TEST_CASE(uninitvar_switch); // handling switch
|
|
|
|
TEST_CASE(uninitvar_references); // references
|
2012-06-23 16:27:57 +02:00
|
|
|
TEST_CASE(uninitvar_return); // return
|
2016-02-07 18:48:57 +01:00
|
|
|
TEST_CASE(uninitvar_assign); // = {..}
|
2012-11-03 21:21:19 +01:00
|
|
|
TEST_CASE(uninitvar_strncpy); // strncpy doesn't always null-terminate
|
2011-02-26 20:08:37 +01:00
|
|
|
TEST_CASE(func_uninit_var); // analyse function calls for: 'int a(int x) { return x+x; }'
|
|
|
|
TEST_CASE(func_uninit_pointer); // analyse function calls for: 'void a(int *p) { *p = 0; }'
|
2010-12-27 21:05:33 +01:00
|
|
|
TEST_CASE(uninitvar_typeof); // typeof
|
2021-05-24 17:50:28 +02:00
|
|
|
TEST_CASE(uninitvar_ignore); // ignore cast, *&x, ..
|
2011-12-13 21:57:27 +01:00
|
|
|
TEST_CASE(uninitvar2);
|
2012-05-28 12:56:24 +02:00
|
|
|
TEST_CASE(uninitvar3); // #3844
|
2012-06-08 16:17:55 +02:00
|
|
|
TEST_CASE(uninitvar4); // #3869 (reference)
|
2012-06-13 19:09:51 +02:00
|
|
|
TEST_CASE(uninitvar5); // #3861
|
2012-12-20 19:45:30 +01:00
|
|
|
TEST_CASE(uninitvar2_func); // function calls
|
2012-12-24 19:11:13 +01:00
|
|
|
TEST_CASE(uninitvar2_value); // value flow
|
2021-10-30 22:13:58 +02:00
|
|
|
TEST_CASE(valueFlowUninit2_value);
|
2019-12-19 20:18:25 +01:00
|
|
|
TEST_CASE(uninitStructMember); // struct members
|
2013-01-20 18:40:24 +01:00
|
|
|
TEST_CASE(uninitvar2_while);
|
2013-01-22 06:55:25 +01:00
|
|
|
TEST_CASE(uninitvar2_4494); // #4494
|
2013-12-04 20:32:20 +01:00
|
|
|
TEST_CASE(uninitvar2_malloc); // malloc returns uninitialized data
|
2014-11-17 16:04:44 +01:00
|
|
|
TEST_CASE(uninitvar8); // ticket #6230
|
2015-01-19 06:38:54 +01:00
|
|
|
TEST_CASE(uninitvar9); // ticket #6424
|
2019-11-13 21:34:27 +01:00
|
|
|
TEST_CASE(uninitvar10); // ticket #9467
|
2020-04-10 12:33:15 +02:00
|
|
|
TEST_CASE(uninitvar11); // ticket #9123
|
2021-05-10 19:48:07 +02:00
|
|
|
TEST_CASE(uninitvar12); // #10218 - stream read
|
2021-07-06 14:24:07 +02:00
|
|
|
TEST_CASE(uninitvar13); // #9772
|
2015-01-22 13:51:43 +01:00
|
|
|
TEST_CASE(uninitvar_unconditionalTry);
|
2015-01-30 19:16:25 +01:00
|
|
|
TEST_CASE(uninitvar_funcptr); // #6404
|
2015-05-10 14:27:15 +02:00
|
|
|
TEST_CASE(uninitvar_operator); // #6680
|
2015-07-24 06:06:31 +02:00
|
|
|
TEST_CASE(uninitvar_ternaryexpression); // #4683
|
2015-08-29 19:00:09 +02:00
|
|
|
TEST_CASE(uninitvar_pointertoarray);
|
2015-10-24 12:06:40 +02:00
|
|
|
TEST_CASE(uninitvar_cpp11ArrayInit); // #7010
|
2015-10-28 16:37:46 +01:00
|
|
|
TEST_CASE(uninitvar_rangeBasedFor); // #7078
|
2018-09-22 16:52:34 +02:00
|
|
|
TEST_CASE(uninitvar_static); // #8734
|
2021-05-22 11:04:42 +02:00
|
|
|
TEST_CASE(checkExpr);
|
2015-07-25 19:02:20 +02:00
|
|
|
TEST_CASE(trac_4871);
|
2013-11-06 17:53:09 +01:00
|
|
|
TEST_CASE(syntax_error); // Ticket #5073
|
2016-05-14 22:36:54 +02:00
|
|
|
TEST_CASE(trac_5970);
|
2021-11-29 07:34:39 +01:00
|
|
|
TEST_CASE(valueFlowUninitTest);
|
2021-10-03 22:15:35 +02:00
|
|
|
TEST_CASE(valueFlowUninitBreak);
|
2021-10-21 14:44:48 +02:00
|
|
|
TEST_CASE(valueFlowUninitStructMembers);
|
2019-08-15 10:44:55 +02:00
|
|
|
TEST_CASE(uninitvar_ipa);
|
2019-08-16 07:48:54 +02:00
|
|
|
TEST_CASE(uninitvar_memberfunction);
|
Fix crash in CheckUninitVar (#2129)
http://cppcheck.osuosl.org:8000/ycmd
```
2019-08-30 23:21
ftp://ftp.se.debian.org/debian/pool/main/y/ycmd/ycmd_0+20181101+git600f54d.orig.tar.gz
cppcheck-options: -j1 --library=posix --library=gnu --library=qt --library=python --library=googletest --library=boost -D__GNUC__ --check-library --inconclusive --enable=style,information --platform=unix64 --template=daca2 -rp=temp temp
platform: Linux-4.15.0-58-generic-x86_64-with-Ubuntu-18.04-bionic
python: 2.7.15+
client-version: 1.1.31
cppcheck: head 1.88
count: Crash! 83
elapsed-time: -11.0 61.2
head results:
Checking temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc: __GNUC__=1...
Program received signal SIGSEGV, Segmentation fault.
CheckUninitVar::valueFlowUninit (this=this@entry=0x7fffffffb350) at build/checkuninitvar.cpp:2376
2376 if (!tok->variable())
#0 CheckUninitVar::valueFlowUninit (this=this@entry=0x7fffffffb350) at build/checkuninitvar.cpp:2376
#1 0x00005555556901ac in CheckUninitVar::runChecks (this=<optimized out>, tokenizer=0x7fffffffbcb0, settings=0x7fffffffcd10, errorLogger=<optimized out>) at lib/checkuninitvar.h:68
#2 0x00005555556a2210 in CppCheck::checkNormalTokens (this=this@entry=0x7fffffffcaf0, tokenizer=...) at build/cppcheck.cpp:730
#3 0x00005555556a6ee2 in CppCheck::checkFile (this=this@entry=0x7fffffffcaf0, filename="temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc", cfgname="", fileStream=...) at build/cppcheck.cpp:540
#4 0x00005555556aad4c in CppCheck::check (this=this@entry=0x7fffffffcaf0, path="temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc") at build/cppcheck.cpp:195
#5 0x00005555557ef167 in CppCheckExecutor::check_internal (this=this@entry=0x7fffffffd880, cppcheck=..., argv=argv@entry=0x7fffffffdc08) at cli/cppcheckexecutor.cpp:884
#6 0x00005555557efa9a in CppCheckExecutor::check (this=0x7fffffffd880, argc=17, argv=0x7fffffffdc08) at cli/cppcheckexecutor.cpp:198
#7 0x00005555555b1d1b in main (argc=17, argv=0x7fffffffdc08) at cli/main.cpp:95
```
2019-09-01 14:51:40 +02:00
|
|
|
TEST_CASE(uninitvar_nonmember); // crash in ycmd test
|
2014-03-19 21:08:48 +01:00
|
|
|
|
2016-08-02 14:27:51 +02:00
|
|
|
TEST_CASE(isVariableUsageDeref); // *p
|
2022-03-24 06:35:44 +01:00
|
|
|
TEST_CASE(isVariableUsageDerefValueflow); // *p
|
2016-08-02 14:27:51 +02:00
|
|
|
|
2022-01-02 08:14:10 +01:00
|
|
|
TEST_CASE(uninitvar_memberaccess); // (&(a))->b <=> a.b
|
|
|
|
|
2018-12-18 07:56:33 +01:00
|
|
|
// whole program analysis
|
2021-11-29 07:34:39 +01:00
|
|
|
TEST_CASE(ctuTest);
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
#define checkUninitVar(...) checkUninitVar_(__FILE__, __LINE__, __VA_ARGS__)
|
|
|
|
void checkUninitVar_(const char* file, int line, const char code[], const char fname[] = "test.cpp", bool debugwarnings = false) {
|
2015-01-21 16:26:27 +01:00
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
settings.debugwarnings = debugwarnings;
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
2021-11-29 07:34:39 +01:00
|
|
|
ASSERT_LOC(tokenizer.tokenize(istr, fname), file, line);
|
2015-01-21 16:26:27 +01:00
|
|
|
|
|
|
|
// Check for redundant code..
|
|
|
|
CheckUninitVar checkuninitvar(&tokenizer, &settings, this);
|
|
|
|
checkuninitvar.check();
|
|
|
|
|
|
|
|
settings.debugwarnings = false;
|
2021-02-24 22:00:06 +01:00
|
|
|
settings.certainty.enable(Certainty::experimental);
|
2015-01-21 16:26:27 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar1() {
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: int b; int c;
|
|
|
|
|
2010-11-16 20:01:57 +01:00
|
|
|
// Ticket #2207 - False negative
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" b = c - a;\n"
|
|
|
|
"}");
|
2010-11-16 20:01:57 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" b = a - c;\n"
|
|
|
|
"}");
|
2010-11-16 20:07:41 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-01-31 13:21:22 +01:00
|
|
|
// Ticket #6455 - some compilers allow const variables to be uninitialized
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.disable
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" const int a;\n"
|
|
|
|
" b = c - a;\n"
|
|
|
|
"}");
|
2015-01-31 13:21:22 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.enable
|
2015-01-31 13:21:22 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int *p;\n"
|
|
|
|
" realloc(p,10);\n"
|
|
|
|
"}");
|
2011-09-28 20:46:09 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n" // #5240
|
2015-07-23 20:53:50 +02:00
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" char *tmp = realloc(p,1000);\n"
|
|
|
|
" if (!tmp) free(p);\n"
|
|
|
|
"}");
|
2013-12-13 07:15:59 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int *p = NULL;\n"
|
|
|
|
" realloc(p,10);\n"
|
|
|
|
"}");
|
2011-09-28 20:46:09 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
// dereferencing uninitialized pointer..
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: struct Foo { void abcd(); };
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Foo *p;\n"
|
|
|
|
" p->abcd();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: template<class T> struct Foo { void abcd(); };
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Foo<int> *p;\n"
|
|
|
|
" p->abcd();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: struct Foo { void* a; };
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(Foo *p)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a;\n"
|
|
|
|
" p->a = malloc(4 * a);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int *p;\n"
|
|
|
|
" delete p;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int *p;\n"
|
|
|
|
" delete [] p;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int *p;\n"
|
|
|
|
" *p = 135;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int *p;\n"
|
|
|
|
" p[0] = 135;\n"
|
|
|
|
"}");
|
2011-04-26 20:26:09 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int *x;\n"
|
|
|
|
" int y = *x;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int *x;\n"
|
|
|
|
" int &y(*x);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int *y = &x;\n"
|
2021-05-16 22:27:04 +02:00
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int *x;\n"
|
|
|
|
" int *&y = x;\n"
|
2021-05-16 22:27:04 +02:00
|
|
|
"}");
|
2011-09-03 18:53:14 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x = xyz::x;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a;\n"
|
|
|
|
" a = 5 + a;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a;\n"
|
|
|
|
" a++;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" extern int a;\n"
|
|
|
|
" a++;\n"
|
|
|
|
"}");
|
2012-06-26 18:28:41 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: void bar(int);
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a;\n"
|
|
|
|
" bar(4 * a);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (i);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" for (int x = 0; i < 10; x++);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" for (int x = 0; x < 10; i++);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("static void foo(int x)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" i = 0;\n"
|
|
|
|
" i++;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int ar[10];\n"
|
|
|
|
" int i;\n"
|
|
|
|
" ar[i] = 0;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x, y;\n"
|
|
|
|
" x = (y = 10);\n"
|
|
|
|
" int z = y * 2;\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x, y;\n"
|
|
|
|
" x = ((y) = 10);\n"
|
|
|
|
"}");
|
2015-01-06 18:03:40 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2012-06-23 16:44:02 +02:00
|
|
|
// Ticket #3597
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" int b = 1;\n"
|
|
|
|
" (b += a) = 1;\n"
|
|
|
|
"}");
|
2019-02-10 19:00:01 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
|
2012-06-23 16:44:02 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a,b,c;\n"
|
|
|
|
" a = b = c;\n"
|
2021-01-05 17:51:32 +01:00
|
|
|
"}", "test.cpp", /*debugwarnings=*/ false);
|
2019-02-10 19:00:01 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout.str());
|
2012-06-23 16:44:02 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Foo p;\n"
|
|
|
|
" p.abcd();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Foo p;\n"
|
|
|
|
" int x = p.abcd();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2012-06-23 16:27:57 +02:00
|
|
|
// Unknown types
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.disable
|
2012-06-23 15:52:35 +02:00
|
|
|
{
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" A ret;\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}");
|
2012-06-23 15:52:35 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" A ret;\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
2015-01-21 16:17:58 +01:00
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ret\n", errout.str());
|
2012-06-23 15:52:35 +02:00
|
|
|
}
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.enable
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2021-05-16 22:27:04 +02:00
|
|
|
// #3916 - avoid false positive
|
|
|
|
checkUninitVar("void f(float x) {\n"
|
|
|
|
" union lf { long l; float f; } u_lf;\n"
|
|
|
|
" float hx = (u_lf.f = (x), u_lf.l);\n"
|
|
|
|
"}",
|
|
|
|
"test.c", false);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x[10];\n"
|
|
|
|
" int *y = x;\n"
|
2021-05-16 22:27:04 +02:00
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int *y = &x;\n"
|
|
|
|
" *y = 0;\n"
|
|
|
|
" x++;\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char x[10], y[10];\n"
|
|
|
|
" char *z = x;\n"
|
|
|
|
" memset(z, 0, sizeof(x));\n"
|
|
|
|
" memcpy(y, x, sizeof(x));\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2012-06-23 16:27:57 +02:00
|
|
|
// Handling >> and <<
|
|
|
|
{
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int a() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" std::cin >> ret;\n"
|
|
|
|
" ret++;\n"
|
|
|
|
"}");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int b) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" std::cin >> b >> a;\n"
|
|
|
|
" return a;"
|
|
|
|
"}");
|
2015-01-22 22:55:08 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-18 20:13:27 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int ret[2];\n"
|
|
|
|
" std::cin >> ret[0];\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int i) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" i >> a;\n"
|
|
|
|
"}");
|
2015-01-22 22:55:08 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int a() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" int a = value >> ret;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
|
2012-06-23 12:41:00 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n" // #3707
|
2015-07-23 20:53:50 +02:00
|
|
|
" Node node;\n"
|
|
|
|
" int x;\n"
|
|
|
|
" node[\"abcd\"] >> x;\n"
|
|
|
|
"}");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-04-24 19:50:54 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int a(FArchive &arc) {\n" // #3060 (initialization through operator<<)
|
2015-07-23 20:53:50 +02:00
|
|
|
" int *p;\n"
|
2021-05-24 13:17:50 +02:00
|
|
|
" arc << p;\n" // <- TODO initialization?
|
2015-07-23 20:53:50 +02:00
|
|
|
" return *p;\n"
|
|
|
|
"}");
|
2021-05-24 13:17:50 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
2011-09-04 12:53:53 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" a = value << ret;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
|
2012-12-18 19:02:30 +01:00
|
|
|
|
2021-05-24 13:17:50 +02:00
|
|
|
// #4320 says this is a FP. << is overloaded.
|
2021-05-16 22:27:04 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
2021-05-24 13:17:50 +02:00
|
|
|
" a << 1;\n" // <- TODO initialization?
|
2015-07-23 20:53:50 +02:00
|
|
|
" return a;\n"
|
|
|
|
"}");
|
2021-05-24 13:17:50 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2015-01-22 22:55:08 +01:00
|
|
|
|
|
|
|
// #4673
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" std::cout << a;\n"
|
|
|
|
"}");
|
2015-01-22 22:55:08 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(std::ostringstream& os) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" os << a;\n"
|
|
|
|
"}");
|
2015-01-22 22:55:08 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" std::cout << 1 << a;\n"
|
|
|
|
"}");
|
2015-01-22 22:55:08 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(std::ostringstream& os) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" os << 1 << a;\n"
|
|
|
|
"}");
|
2015-01-22 22:55:08 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2019-11-16 11:47:57 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
// #9422
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char *p = new char[10];\n"
|
|
|
|
" std::cout << (void *)p << 1;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char p[10];\n"
|
|
|
|
" std::cout << (void *)p << 1;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char *p = new char[10];\n"
|
|
|
|
" std::cout << p << 1;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
|
|
|
|
2022-02-02 12:24:32 +01:00
|
|
|
checkUninitVar("void f() {\n" // #9696
|
|
|
|
" int *p = new int[10];\n"
|
|
|
|
" std::cout << p << 1;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int i[10];\n"
|
|
|
|
" std::cout << i;\n"
|
|
|
|
" char c[10];\n"
|
|
|
|
" std::cout << c;\n"
|
|
|
|
" wchar_t w[10];\n"
|
|
|
|
" std::cout << w;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: c\n"
|
|
|
|
"[test.cpp:7]: (error) Uninitialized variable: w\n",
|
|
|
|
errout.str());
|
|
|
|
|
2019-11-16 11:47:57 +01:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char p[10];\n"
|
|
|
|
" std::cout << p << 1;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char p[10];\n"
|
|
|
|
" std::cout << *p << 1;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
}
|
2012-06-23 16:27:57 +02:00
|
|
|
}
|
2012-06-23 12:41:00 +02:00
|
|
|
|
2018-04-17 14:23:04 +02:00
|
|
|
// #8494 : Overloaded & operator
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" a & x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-18 21:03:43 +02:00
|
|
|
|
2018-04-17 14:23:04 +02:00
|
|
|
checkUninitVar("void f(int a) {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" a & x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2021-05-18 21:03:43 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int a,b,c;\n"
|
|
|
|
" ar & a & b & c;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a() {\n" // asm
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" asm();\n"
|
|
|
|
" x++;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x[10];\n"
|
|
|
|
" struct xyz xyz1 = { .x = x };\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" struct S *s;\n"
|
|
|
|
" s->x = 0;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char *buf = malloc(100);\n"
|
|
|
|
" struct ABC *abc = buf;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("class Fred {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"public:\n"
|
|
|
|
" FILE *f;\n"
|
|
|
|
" ~Fred();\n"
|
|
|
|
"}\n"
|
|
|
|
"Fred::~Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" fclose(f);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int c;\n"
|
|
|
|
" ab(sizeof(xyz), &c);\n"
|
|
|
|
" if (c);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int c;\n"
|
|
|
|
" a = (f2(&c));\n"
|
|
|
|
" c++;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int a)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" if (a) {\n"
|
|
|
|
" char *p;\n"
|
|
|
|
" *p = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
|
|
|
// +=
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int c;\n"
|
|
|
|
" c += 2;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a[10];\n"
|
|
|
|
" a[0] = 10 - a[1];\n"
|
|
|
|
"}");
|
2021-05-21 14:30:47 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a[1]\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
|
|
|
// goto/setjmp/longjmp..
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo(int x)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" long b;\n"
|
|
|
|
" if (g()) {\n"
|
|
|
|
" b =2;\n"
|
|
|
|
" goto found;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return;\n"
|
|
|
|
"\n"
|
|
|
|
"found:\n"
|
|
|
|
" int a = b;\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" jmp_buf env;\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int val = setjmp(env);\n"
|
|
|
|
" if(val)\n"
|
|
|
|
" return a;\n"
|
|
|
|
" a = 1;\n"
|
|
|
|
" longjmp(env, 1);\n"
|
|
|
|
"}");
|
2015-01-21 16:49:34 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
|
|
|
// macro_for..
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int retval;\n"
|
|
|
|
" if (condition) {\n"
|
|
|
|
" for12(1,2) { }\n"
|
|
|
|
" retval = 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" else\n"
|
|
|
|
" retval = 2;\n"
|
|
|
|
" return retval;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" goto exit;\n"
|
|
|
|
" i++;\n"
|
|
|
|
"exit:\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-08-01 03:33:43 +02:00
|
|
|
|
2015-10-27 12:40:52 +01:00
|
|
|
checkUninitVar("int foo() {\n"
|
|
|
|
" int x,y=0;\n"
|
|
|
|
"again:\n"
|
|
|
|
" if (y) return x;\n"
|
|
|
|
" x = a;\n"
|
|
|
|
" y = 1;\n"
|
|
|
|
" goto again;\n"
|
|
|
|
"}", "test.c", false);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2012-06-24 00:00:15 +02:00
|
|
|
// Ticket #3873 (false positive)
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("MachineLoopRange *MachineLoopRanges::getLoopRange(const MachineLoop *Loop) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" MachineLoopRange *&Range = Cache[Loop];\n"
|
|
|
|
" if (!Range)\n"
|
|
|
|
" Range = new MachineLoopRange(Loop, Allocator, *Indexes);\n"
|
|
|
|
" return Range;\n"
|
|
|
|
"}");
|
2012-06-24 00:00:15 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-08-27 15:48:21 +02:00
|
|
|
|
|
|
|
// #4040 - False positive
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int iter;\n"
|
|
|
|
" {\n"
|
|
|
|
" union\n"
|
|
|
|
" {\n"
|
|
|
|
" int asInt;\n"
|
|
|
|
" double asDouble;\n"
|
|
|
|
" };\n"
|
|
|
|
"\n"
|
|
|
|
" iter = x;\n"
|
|
|
|
" }\n"
|
|
|
|
" return 1 + iter;\n"
|
|
|
|
"}", "test.cpp", false);
|
2012-08-27 15:48:21 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2014-08-05 16:11:42 +02:00
|
|
|
|
|
|
|
// C++11 style initialization
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i = 0;\n"
|
|
|
|
" int j{ i };\n"
|
|
|
|
" return j;\n"
|
|
|
|
"}");
|
2014-08-05 16:11:42 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-08-25 21:19:19 +02:00
|
|
|
|
|
|
|
// Ticket #5646
|
|
|
|
checkUninitVar("float foo() {\n"
|
|
|
|
" float source[2] = {3.1, 3.1};\n"
|
|
|
|
" float (*sink)[2] = &source;\n"
|
|
|
|
" return (*sink)[0];\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2020-05-28 21:28:18 +02:00
|
|
|
|
|
|
|
// Ticket #9296
|
|
|
|
checkUninitVar("void f(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int z = (x) & ~__round_mask(1, 1);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int z = (x) | ~__round_mask(1, 1);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("int __round_mask(int, int);\n"
|
|
|
|
"void f(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int* z = &x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 13:17:15 +01:00
|
|
|
void uninitvar_warn_once() {
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: int a; int b;
|
|
|
|
|
2019-03-02 13:17:15 +01:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" a = x;\n"
|
|
|
|
" b = x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-08-08 08:53:08 +02:00
|
|
|
// Handling of unknown types. Assume they are POD in C.
|
|
|
|
void uninitvar_decl() {
|
|
|
|
const char code[] = "void f() {\n"
|
|
|
|
" dfs a;\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}";
|
|
|
|
|
|
|
|
// Assume dfs is a non POD type if file is C++
|
|
|
|
checkUninitVar(code, "test.cpp");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Assume dfs is a POD type if file is C
|
|
|
|
checkUninitVar(code, "test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
const char code2[] = "struct AB { int a,b; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" return ab;\n"
|
|
|
|
"}";
|
|
|
|
checkUninitVar(code2, "test.cpp");
|
2021-05-10 18:38:44 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n"
|
|
|
|
"[test.cpp:4]: (error) Uninitialized struct member: ab.b\n", errout.str());
|
2015-08-08 08:53:08 +02:00
|
|
|
checkUninitVar(code2, "test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab\n", errout.str());
|
|
|
|
|
|
|
|
// Ticket #3890 - False positive for std::map
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" std::map<int,bool> x;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Ticket #3906 - False positive for std::vector pointer
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" std::vector<int> *x = NULL;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}", "test.cpp", false);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Ticket #6701 - Variable name is a POD type according to cfg
|
|
|
|
const char xmldata[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def format=\"1\">"
|
|
|
|
" <podtype name=\"_tm\"/>"
|
|
|
|
"</def>";
|
2022-01-18 22:02:25 +01:00
|
|
|
ASSERT_EQUALS(true, settings.library.loadxmldata(xmldata, sizeof(xmldata)));
|
2015-08-08 08:53:08 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" Fred _tm;\n"
|
|
|
|
" _tm.dostuff();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2016-12-21 23:11:11 +01:00
|
|
|
|
|
|
|
// Ticket #7822 - Array type
|
|
|
|
checkUninitVar("A *f() {\n"
|
|
|
|
" A a,b;\n"
|
|
|
|
" b[0] = 0;"
|
|
|
|
" return a;\n"
|
|
|
|
"}", "test.c", false);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-08-08 08:53:08 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar3() { // #3844
|
2012-05-28 12:56:24 +02:00
|
|
|
// avoid false positive
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("namespace std _GLIBCXX_VISIBILITY(default)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
"_GLIBCXX_BEGIN_NAMESPACE_CONTAINER\n"
|
|
|
|
" typedef unsigned long _Bit_type;\n"
|
|
|
|
" struct _Bit_reference\n"
|
|
|
|
" {\n"
|
|
|
|
" _Bit_type * _M_p;\n"
|
|
|
|
" _Bit_type _M_mask;\n"
|
|
|
|
" _Bit_reference(_Bit_type * __x, _Bit_type __y)\n"
|
|
|
|
" : _M_p(__x), _M_mask(__y) { }\n"
|
|
|
|
" };\n"
|
|
|
|
"}");
|
2012-05-28 12:56:24 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2010-11-16 20:16:44 +01:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_bitop() {
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: int a; int c;
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int b;\n"
|
|
|
|
" c = a | b;\n"
|
|
|
|
"}");
|
2010-11-16 20:16:44 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: b\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int b;\n"
|
|
|
|
" c = b | a;\n"
|
|
|
|
"}");
|
2010-11-16 20:16:44 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: b\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
// if..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_if() {
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: struct Foo { void abcd(); };
|
|
|
|
checkUninitVar("static void foo(int x)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Foo *p;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" p = new Foo;\n"
|
|
|
|
" p->abcd();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo(int x)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a;\n"
|
|
|
|
" if (x==1);\n"
|
|
|
|
" if (x==2);\n"
|
|
|
|
" x = a;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2019-10-29 20:36:58 +01:00
|
|
|
checkUninitVar("int foo() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (1)\n"
|
|
|
|
" i = 11;\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-28 18:09:43 +02:00
|
|
|
checkUninitVar("int bar(int x) {\n"
|
|
|
|
" int n;\n"
|
|
|
|
" if ( x == 23)\n"
|
|
|
|
" n = 1;\n"
|
|
|
|
" else if ( x == 11 )\n"
|
|
|
|
" n = 2;\n"
|
|
|
|
" return n;\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" i = 22;\n"
|
|
|
|
" else\n"
|
|
|
|
" i = 33;\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2016-01-30 20:48:28 +01:00
|
|
|
checkUninitVar("int foo(int x)\n" // #5503
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x < 2)\n"
|
|
|
|
" i = 22;\n"
|
|
|
|
" else if (x >= 2)\n" // condition is always true
|
|
|
|
" i = 33;\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" i = 22;\n"
|
|
|
|
" else\n"
|
|
|
|
" {\n"
|
|
|
|
" char *y = {0};\n"
|
|
|
|
" i = 33;\n"
|
|
|
|
" }\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" {\n"
|
|
|
|
" struct abc abc1 = (struct abc) { .a=0, .b=0, .c=0 };\n"
|
|
|
|
" i = 22;\n"
|
|
|
|
" }\n"
|
|
|
|
" else\n"
|
|
|
|
" {\n"
|
|
|
|
" i = 33;\n"
|
|
|
|
" }\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo(int x)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Foo *p;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" p = new Foo;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" p->abcd();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo(int a)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int n;\n"
|
|
|
|
" int condition;\n"
|
|
|
|
" if(a == 1) {\n"
|
|
|
|
" n=0;\n"
|
|
|
|
" condition=0;\n"
|
|
|
|
" }\n"
|
|
|
|
" else {\n"
|
|
|
|
" n=1;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" if( n == 0) {\n"
|
|
|
|
" a=condition;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" C *c;\n"
|
|
|
|
" if (fun(&c));\n"
|
|
|
|
" c->Release();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-11-21 19:31:18 +01:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" C c;\n"
|
|
|
|
" if (fun(&c.d));\n"
|
|
|
|
" return c;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char a[10];\n"
|
|
|
|
" if (a[0] = x){}\n"
|
|
|
|
"}");
|
2012-07-17 07:03:40 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo(int x)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (one())\n"
|
|
|
|
" i = 1;\n"
|
|
|
|
" else\n"
|
|
|
|
" return 3;\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}");
|
2010-11-15 20:35:01 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Ticket #2207 - False positive
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" a = 1;\n"
|
|
|
|
" if (!x)\n"
|
|
|
|
" return;\n"
|
|
|
|
" b = (c - a);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int ret;\n"
|
|
|
|
" if (one())\n"
|
|
|
|
" ret = 1;\n"
|
|
|
|
" else\n"
|
|
|
|
" throw 3;\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int a)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int ret;\n"
|
|
|
|
" if (a == 1)\n"
|
|
|
|
" ret = 1;\n"
|
|
|
|
" else\n"
|
|
|
|
" XYZ ret = 2;\n" // XYZ may be an unexpanded macro so bailout the checking of "ret".
|
|
|
|
" return ret;\n"
|
|
|
|
"}");
|
2014-12-15 15:41:03 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: ret\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int a, int b)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" if (a)\n"
|
|
|
|
" x = a;\n"
|
|
|
|
" else {\n"
|
|
|
|
" do { } while (f2());\n"
|
|
|
|
" x = b;\n"
|
|
|
|
" }\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo(long verbose,bool bFlag)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" double t;\n"
|
|
|
|
" if (bFlag)\n"
|
|
|
|
" {\n"
|
|
|
|
" if (verbose)\n"
|
|
|
|
" t = 1;\n"
|
|
|
|
" if (verbose)\n"
|
|
|
|
" std::cout << (12-t);\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int test(int cond1, int cond2) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int foo;\n"
|
|
|
|
" if (cond1 || cond2) {\n"
|
|
|
|
" if (cond2)\n"
|
|
|
|
" foo = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
" if (cond2) {\n"
|
|
|
|
" int t = foo*foo;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2011-01-05 19:54:56 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-20 18:38:59 +02:00
|
|
|
checkUninitVar("void foo(int *pix) {\n"
|
|
|
|
" int dest_x;\n"
|
|
|
|
" {\n"
|
|
|
|
" if (pix)\n"
|
|
|
|
" dest_x = 123;\n"
|
|
|
|
" }\n"
|
|
|
|
" if (pix)\n"
|
|
|
|
" a = dest_x;\n" // <- not uninitialized
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
// ? :
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void foo(int v) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" x = v <= 0 ? -1 : x;\n"
|
|
|
|
"}");
|
2015-07-23 08:46:59 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" const char *msgid1, *msgid2;\n"
|
|
|
|
" int ret = bar(&msgid1);\n"
|
|
|
|
" if (ret > 0) {\n"
|
|
|
|
" ret = bar(&msgid2);\n"
|
|
|
|
" }\n"
|
|
|
|
" ret = ret <= 0 ? -1 :\n"
|
|
|
|
" strcmp(msgid1, msgid2) == 0;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo(int a, int b)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x; x = (a<b) ? 1 : 0;\n"
|
|
|
|
" int y = y;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: y\n", errout.str());
|
|
|
|
|
2016-12-25 22:43:29 +01:00
|
|
|
checkUninitVar("void foo() {\n" // pidgin-2.11.0/finch/libgnt/gnttree.c
|
|
|
|
" int x = (x = bar()) ? x : 0;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
// ; { .. }
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int retval;\n"
|
|
|
|
" if (condition) {\n"
|
|
|
|
" { }\n"
|
|
|
|
" retval = 1; }\n"
|
|
|
|
" else\n"
|
|
|
|
" retval = 2;\n"
|
|
|
|
" return retval;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" {\n"
|
|
|
|
" for (int i = 0; i < 10; ++i)\n"
|
|
|
|
" { }\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" { }\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-09-09 19:49:40 +02:00
|
|
|
|
2015-01-06 07:44:04 +01:00
|
|
|
// ({ .. })
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (abc) { x = 123; }\n"
|
|
|
|
" else { a = ({b=c;}); x = 456; }\n"
|
|
|
|
" ++x;\n"
|
|
|
|
"}");
|
2015-01-21 16:45:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-01-06 07:44:04 +01:00
|
|
|
|
2012-09-09 19:49:40 +02:00
|
|
|
// Ticket #3098 - False negative uninitialized variable
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char *c1,*c2;\n"
|
|
|
|
" if(strcoll(c1,c2))\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2012-09-09 19:49:40 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c1\n"
|
|
|
|
"[test.cpp:4]: (error) Uninitialized variable: c2\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(char *c1, char *c2)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" if(strcoll(c1,c2))\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2012-09-09 19:49:40 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char *c1;\n"
|
|
|
|
" c1=strcpy(c1,\"test\");\n"
|
|
|
|
"}");
|
2015-07-22 20:31:58 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c1\n", errout.str());
|
2012-09-09 19:49:40 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(char *c1)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" c1=strcpy(c1,\"test\");\n"
|
|
|
|
"}");
|
2012-09-09 19:49:40 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" X var;\n"
|
|
|
|
" memset(var, 0, sizeof(var));\n"
|
|
|
|
"}", "test.c");
|
2015-07-22 21:58:06 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-04-27 12:43:21 +02:00
|
|
|
|
|
|
|
checkUninitVar("int f() {\n" // #8692
|
|
|
|
" bool b = e();\n"
|
|
|
|
" int v;\n"
|
|
|
|
" if (b)\n"
|
|
|
|
" doStuff(&v);\n"
|
|
|
|
" int v2 = (b) ? v / 5 : 0;\n"
|
|
|
|
" int v3;\n"
|
|
|
|
" if (b)\n"
|
|
|
|
" v3 = 50;\n"
|
|
|
|
" int v4 = (b) ? v3 + 5 : 0;\n"
|
|
|
|
" int v5;\n"
|
|
|
|
" int v6 = v5;\n"
|
|
|
|
" doStuff(&v5);\n"
|
|
|
|
" int v7 = v5;\n"
|
|
|
|
" return v2 + v4 + v6 + v7;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:12]: (error) Uninitialized variable: v5\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// handling for/while loops..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_loops() {
|
2010-10-31 12:31:11 +01:00
|
|
|
// for..
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: void b(int);
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" for (int i = 0; i < 4; ++i) {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" b(4*a);\n"
|
|
|
|
" }"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int k;\n"
|
|
|
|
" for (int i = 0; i < 4; ++i) {\n"
|
|
|
|
" k = k + 2;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: k\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" gchar sel[10];\n"
|
|
|
|
" for (int i = 0; i < 4; ++i) {\n"
|
|
|
|
" int sz = sizeof(sel);\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-11-13 08:03:59 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("enum ABCD { A, B, C, D };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"\n"
|
|
|
|
"static void f(char *str ) {\n"
|
|
|
|
" enum ABCD i;\n"
|
|
|
|
" for (i = 0; i < D; i++) {\n"
|
|
|
|
" str[i] = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void x() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" do {\n"
|
|
|
|
" Token * tok;\n"
|
|
|
|
" for (tok = a; tok; tok = tok->next())\n"
|
|
|
|
" {\n"
|
|
|
|
" }\n"
|
|
|
|
" } while (tok2);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-20 22:56:14 +02:00
|
|
|
checkUninitVar("void foo(void) {\n"
|
|
|
|
" int a = 0;\n"
|
|
|
|
" int x;\n"
|
|
|
|
"\n"
|
|
|
|
" for (;;) {\n"
|
|
|
|
" if (!a || 12 < x) {\n" // <- x is not uninitialized
|
|
|
|
" a = 1;\n"
|
|
|
|
" x = 2;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void foo(void) {\n"
|
|
|
|
" int a = 0;\n"
|
|
|
|
" int x;\n"
|
|
|
|
"\n"
|
|
|
|
" for (;;) {\n"
|
|
|
|
" if (!a || 12 < x) {\n" // <- x is uninitialized
|
|
|
|
" a = 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2021-05-24 17:02:19 +02:00
|
|
|
checkUninitVar("void foo(int n) {\n"
|
|
|
|
" int one[10];\n"
|
|
|
|
" for (int rank = 0; rank < n; ++rank) {\n"
|
|
|
|
" for (int i=0;i<rank;i++)\n"
|
|
|
|
" f = one[i];\n"
|
|
|
|
" one[rank] = -1;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-11-24 18:08:21 +01:00
|
|
|
// Ticket #2226: C++0x loop
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" container c;\n"
|
|
|
|
" for (iterator it : c) {\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-11-24 18:08:21 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2010-12-23 09:15:45 +01:00
|
|
|
// Ticket #2345: False positive in sub-condition in if inside a loop
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" const PoolItem* pItem;\n"
|
|
|
|
" while (x > 0) {\n"
|
|
|
|
" if (GetItem(&pItem) && (*pItem != rPool))\n"
|
|
|
|
" { }\n"
|
|
|
|
" x--;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-12-23 09:15:45 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: struct PoolItem { bool operator!=(const PoolItem&) const; };
|
|
|
|
checkUninitVar("void f(int x, const PoolItem& rPool) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" const PoolItem* pItem;\n"
|
|
|
|
" while (x > 0) {\n"
|
|
|
|
" if (*pItem != rPool)\n"
|
|
|
|
" { }\n"
|
|
|
|
" x--;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2015-01-21 16:17:58 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: pItem\n", errout.str());
|
2011-02-19 20:19:46 +01:00
|
|
|
|
|
|
|
// #2231 - conditional initialization in loop..
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo(char *a) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
"\n"
|
|
|
|
" for (int i = 0; i < 10; ++i) {\n"
|
|
|
|
" if (a[i] == 'x') {\n"
|
|
|
|
" x = i;\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2015-07-23 17:20:11 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:11]: (error) Uninitialized variable: x\n", "", errout.str());
|
2011-07-25 16:35:30 +02:00
|
|
|
|
|
|
|
// Ticket #2796
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" while (true) {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" if (y) x = 0;\n"
|
|
|
|
" else break;\n"
|
|
|
|
" return x;\n" // <- x is initialized
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2011-07-25 16:35:30 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-11-04 09:49:49 +01:00
|
|
|
|
|
|
|
// Assignment in for. Ticket #3287
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo(char* in, bool b) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char* c;\n"
|
|
|
|
" if (b) for (c = in; *c == 0; ++c) {}\n"
|
|
|
|
" else c = in + strlen(in) - 1;\n"
|
|
|
|
" *c = 0;\n"
|
|
|
|
"}");
|
2013-02-02 16:01:34 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-11 20:35:15 +02:00
|
|
|
|
|
|
|
// #10273 - assignment in conditional code
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: extern const int PORT_LEARN_DISABLE;
|
2021-05-11 20:35:15 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
|
|
|
" int learn;\n"
|
|
|
|
" for (int index = 0; index < 10; index++) {\n"
|
|
|
|
" if (!(learn & PORT_LEARN_DISABLE))\n"
|
|
|
|
" learn = 123;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: learn\n", errout.str());
|
2021-05-14 21:36:51 +02:00
|
|
|
|
2021-05-15 14:59:00 +02:00
|
|
|
// extracttests.start: struct Entry { Entry *next; }; Entry *buckets[10];
|
2021-05-14 21:36:51 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
|
|
|
" Entry *entry, *nextEntry;\n"
|
|
|
|
" for(int i = 0; i < 10; i++) {\n"
|
|
|
|
" for(entry = buckets[i]; entry != NULL; entry = nextEntry) {\n" // <- nextEntry is not uninitialized
|
|
|
|
" nextEntry = entry->next;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void foo() {\n"
|
|
|
|
" Entry *entry, *nextEntry;\n"
|
|
|
|
" for(int i = 0; i < 10; i++) {\n"
|
|
|
|
" for(entry = buckets[i]; entry != NULL; entry = nextEntry) {\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: nextEntry\n", errout.str());
|
2021-05-15 11:16:32 +02:00
|
|
|
|
|
|
|
checkUninitVar("void f(int x) {\n"
|
|
|
|
" list *f = NULL;\n"
|
|
|
|
" list *l;\n"
|
|
|
|
"\n"
|
|
|
|
" while (--x) {\n"
|
|
|
|
" if (!f)\n"
|
|
|
|
" f = c;\n"
|
|
|
|
" else\n"
|
|
|
|
" l->next = c;\n" // <- not uninitialized
|
|
|
|
" l = c;\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-19 13:06:44 +02:00
|
|
|
|
2021-05-20 09:14:17 +02:00
|
|
|
// #6952 - do-while-loop
|
|
|
|
checkUninitVar("void f(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" int* p;\n"
|
|
|
|
" do\n"
|
|
|
|
" {\n"
|
|
|
|
" if (true) {;}\n"
|
|
|
|
" else\n"
|
|
|
|
" {\n"
|
2021-05-21 15:30:52 +02:00
|
|
|
" return;\n"
|
2021-05-20 09:14:17 +02:00
|
|
|
" }\n"
|
|
|
|
" *p = 7;\n" // <<
|
|
|
|
" p = new int(9);\n"
|
|
|
|
" } while (*p != 8);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:11]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
|
|
|
// #6952 - while-loop
|
|
|
|
checkUninitVar("void f(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" int* p;\n"
|
|
|
|
" while (*p != 8) {\n" // <<
|
2021-05-20 18:38:59 +02:00
|
|
|
" *p = 7;\n"
|
2021-05-20 09:14:17 +02:00
|
|
|
" p = new int(9);\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
2021-05-19 13:06:44 +02:00
|
|
|
// switch in loop
|
|
|
|
checkUninitVar("int foo(int *p) {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" while (true) {\n"
|
|
|
|
" switch (*p) {\n"
|
|
|
|
" case 1:\n"
|
|
|
|
" return x;\n"
|
|
|
|
" case 2:\n"
|
|
|
|
" x = 123;\n"
|
|
|
|
" break;\n"
|
|
|
|
" };\n"
|
|
|
|
" ++p\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// switch..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_switch() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" short c;\n"
|
|
|
|
" switch(x) {\n"
|
|
|
|
" case 1:\n"
|
|
|
|
" c++;\n"
|
|
|
|
" break;\n"
|
|
|
|
" };\n"
|
|
|
|
"}");
|
2015-07-23 17:20:11 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n", "", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("char * f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" static char ret[200];\n"
|
|
|
|
" memset(ret, 0, 200);\n"
|
|
|
|
" switch (x)\n"
|
|
|
|
" {\n"
|
|
|
|
" case 1: return ret;\n"
|
|
|
|
" case 2: return ret;\n"
|
|
|
|
" }\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo(const int iVar, unsigned int slot, unsigned int pin)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (iVar == 0)\n"
|
|
|
|
" {\n"
|
|
|
|
" switch (slot)\n"
|
|
|
|
" {\n"
|
|
|
|
" case 4: return 5;\n"
|
|
|
|
" default: return -1;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" else\n"
|
|
|
|
" {\n"
|
|
|
|
" switch (pin)\n"
|
|
|
|
" {\n"
|
|
|
|
" case 0: i = 2; break;\n"
|
|
|
|
" default: i = -1; break;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// #1855 - switch(foo(&x))
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int a()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" switch (foo(&x))\n"
|
|
|
|
" {\n"
|
|
|
|
" case 1:\n"
|
|
|
|
" return x;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-10-30 18:19:09 +01:00
|
|
|
|
|
|
|
// #3231 - ({ switch .. })
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" ({\n"
|
|
|
|
" switch(sizeof(int)) {\n"
|
|
|
|
" case 4:\n"
|
|
|
|
" default:\n"
|
|
|
|
" (a)=0;\n"
|
|
|
|
" break;\n"
|
|
|
|
" };\n"
|
|
|
|
" })\n"
|
|
|
|
"}", "test.cpp", false);
|
2011-10-30 18:19:09 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// arrays..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_arrays() {
|
2021-05-21 15:30:52 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a[10];\n"
|
|
|
|
" a[a[0]] = 0;\n"
|
|
|
|
"}");
|
2021-05-21 14:30:47 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a[0]\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a[10];\n"
|
|
|
|
" *a = '\\0';\n"
|
|
|
|
" int i = strlen(a);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a, b[10];\n"
|
|
|
|
" a = b[0] = 0;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a[10], b[10];\n"
|
|
|
|
" a[0] = b[0] = 0;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a[10], *p;\n"
|
|
|
|
" *(p = a) = 0;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char a[10], *p;\n"
|
|
|
|
" p = &(a[10]);\n"
|
|
|
|
"}");
|
2011-05-05 21:26:18 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-08-08 14:52:46 +02:00
|
|
|
// array usage in ?: (tests that the isVariableUsed() works)
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char a[10], *p;\n"
|
|
|
|
" p = c?a:0;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-08-08 14:52:46 +02:00
|
|
|
" char a[10], c;\n"
|
|
|
|
" c = *(x?a:0);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char a[10], c;\n"
|
|
|
|
" strcpy(dest, x?a:\"\");\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
2015-08-29 13:19:28 +02:00
|
|
|
|
|
|
|
checkUninitVar("void f(int x) {\n"
|
|
|
|
" int a[2];\n"
|
|
|
|
" y *= (x ? 1 : 2);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-08-08 14:52:46 +02:00
|
|
|
|
|
|
|
// passing array to library functions
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char c[50] = \"\";\n"
|
|
|
|
" strcat(c, \"test\");\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("void f(char *s2) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char s[20];\n"
|
|
|
|
" strcpy(s2, s);\n"
|
|
|
|
"};");
|
2015-07-23 14:51:38 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: s\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char s[20];\n"
|
|
|
|
" strcat(s, \"abc\");\n"
|
|
|
|
"};");
|
2015-07-23 14:51:38 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: s\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char s[20];\n"
|
|
|
|
" strchr(s, ' ');\n"
|
|
|
|
"};");
|
2015-07-23 14:51:38 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: s\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int y[2];\n"
|
|
|
|
" int s;\n"
|
|
|
|
" GetField( y + 0, y + 1 );\n"
|
2021-05-16 22:27:04 +02:00
|
|
|
" s = y[0] * y[1];\n"
|
|
|
|
"}");
|
2010-11-12 16:11:44 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a[2];\n"
|
|
|
|
" init(a - 1);\n"
|
|
|
|
" int b = a[0];\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-12-17 21:20:04 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Fred a[2];\n"
|
|
|
|
" Fred b = a[0];\n"
|
|
|
|
"}");
|
2010-12-17 21:20:04 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-12-18 10:06:21 +01:00
|
|
|
|
2021-05-24 20:55:04 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
|
|
|
" char buf[1024];\n"
|
|
|
|
" char *b = (char *) (((uintptr_t) buf + 63) & ~(uintptr_t) 63);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-25 07:31:01 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
|
|
|
" char buf[1024];\n"
|
|
|
|
" char x = *(char *) (((uintptr_t) buf + 63) & ~(uintptr_t) 63);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: buf\n", errout.str());
|
|
|
|
|
2021-05-14 11:23:51 +02:00
|
|
|
// Passing array to function
|
|
|
|
checkUninitVar("void f(int i);\n"
|
|
|
|
"void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int a[10];\n"
|
|
|
|
" f(a[0]);\n"
|
|
|
|
"}");
|
2021-05-14 11:54:18 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
|
2021-05-14 11:23:51 +02:00
|
|
|
|
2010-12-18 10:06:21 +01:00
|
|
|
// Ticket #2320
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char a[2];\n"
|
2021-05-16 22:27:04 +02:00
|
|
|
" unsigned long b = (unsigned long)(a+2) & ~7;\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"}");
|
2010-12-18 10:06:21 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-09-05 20:18:58 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // Ticket #3050
|
2015-07-23 20:53:50 +02:00
|
|
|
" char a[2];\n"
|
|
|
|
" printf(\"%s\", a);\n"
|
|
|
|
"}");
|
2015-07-23 17:20:11 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", "", errout.str());
|
2012-01-13 07:26:56 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // Ticket #5108 (fp)
|
2015-07-23 20:53:50 +02:00
|
|
|
" const char *a;\n"
|
|
|
|
" printf(\"%s\", a=\"abc\");\n"
|
|
|
|
"}");
|
2013-11-04 23:09:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // Ticket #3497
|
2015-07-23 20:53:50 +02:00
|
|
|
" char header[1];\n"
|
|
|
|
" *((unsigned char*)(header)) = 0xff;\n"
|
|
|
|
" return header[0];\n"
|
|
|
|
"}");
|
2012-01-13 07:26:56 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-01-21 20:42:41 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // Ticket #3497
|
2015-07-23 20:53:50 +02:00
|
|
|
" char header[1];\n"
|
|
|
|
" *((unsigned char*)((unsigned char *)(header))) = 0xff;\n"
|
|
|
|
" return header[0];\n"
|
|
|
|
"}");
|
2012-01-21 20:42:41 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-03-16 17:28:05 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" ABC abc;\n"
|
|
|
|
" int a[1];\n"
|
|
|
|
"\n"
|
|
|
|
" abc.a = a;\n"
|
|
|
|
" init(&abc);\n"
|
|
|
|
" return a[0];\n"
|
|
|
|
"}");
|
2012-03-16 17:28:05 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-04-26 13:37:13 +02:00
|
|
|
|
|
|
|
// ticket #3344
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(){\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char *strMsg = \"This is a message\";\n"
|
|
|
|
" char *buffer=(char*)malloc(128*sizeof(char));\n"
|
|
|
|
" strcpy(strMsg,buffer);\n"
|
|
|
|
" free(buffer);\n"
|
|
|
|
"}", "test.cpp", false);
|
2012-11-01 18:40:20 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str());
|
2012-05-25 16:28:32 +02:00
|
|
|
|
2020-02-23 19:53:17 +01:00
|
|
|
checkUninitVar("void f(){\n"
|
|
|
|
" char *strMsg = \"This is a message\";\n"
|
|
|
|
" char *buffer=static_cast<char*>(malloc(128*sizeof(char)));\n"
|
|
|
|
" strcpy(strMsg,buffer);\n"
|
|
|
|
" free(buffer);\n"
|
|
|
|
"}", "test.cpp", false);
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: buffer\n", errout.str());
|
|
|
|
|
2012-05-25 16:28:32 +02:00
|
|
|
// #3845
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a[1] = {5};\n"
|
|
|
|
" return a[0];\n"
|
|
|
|
"}");
|
2012-05-25 16:28:32 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a[2][2] = {{3,4}, {5,6}};\n"
|
|
|
|
" return a[0][1];\n"
|
|
|
|
"}");
|
2012-05-25 16:28:32 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a[1];\n"
|
|
|
|
" return a[0];\n"
|
|
|
|
"}");
|
2015-09-13 16:32:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2012-05-25 16:28:32 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a[2][2];\n"
|
|
|
|
" return a[0][1];\n"
|
|
|
|
"}");
|
2015-09-13 16:32:16 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("int foo() {\n"
|
|
|
|
" int a[10];\n"
|
|
|
|
" dostuff(a[0]);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-11-25 06:38:23 +01:00
|
|
|
|
|
|
|
// # 4740
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int *a[2][19];\n"
|
|
|
|
" int **b = a[0];\n"
|
|
|
|
"}");
|
2013-11-25 06:38:23 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-08-05 10:19:17 +02:00
|
|
|
|
|
|
|
// #6869 - FP when passing uninit array to function
|
|
|
|
checkUninitVar("void bar(PSTR x);\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" char x[10];\n"
|
|
|
|
" bar(x);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-09-16 14:42:55 +02:00
|
|
|
|
|
|
|
// struct
|
|
|
|
checkUninitVar("struct Fred { int x; int y; };\n"
|
|
|
|
""
|
|
|
|
"void f() {\n"
|
|
|
|
" struct Fred fred[10];\n"
|
|
|
|
" fred[1].x = 0;\n"
|
|
|
|
"}", "test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-01-27 19:45:27 +01:00
|
|
|
|
|
|
|
checkUninitVar("char f() {\n"
|
|
|
|
" std::array<char, 1> a;\n"
|
|
|
|
" return a[0];\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("std::string f() {\n"
|
|
|
|
" std::array<std::string, 1> a;\n"
|
|
|
|
" return a[0];\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2015-08-29 19:00:09 +02:00
|
|
|
void uninitvar_pointertoarray() {
|
|
|
|
checkUninitVar("void draw_quad(float z) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" float (*vertices)[2][4];\n"
|
|
|
|
" vertices[0][0][0] = z;\n"
|
|
|
|
" vertices[0][0][1] = z;\n"
|
|
|
|
" vertices[1][0][0] = z;\n"
|
|
|
|
" vertices[1][0][1] = z;\n"
|
|
|
|
" vertices[2][0][0] = z;\n"
|
|
|
|
" vertices[2][0][1] = z;\n"
|
|
|
|
" vertices[3][0][0] = z;\n"
|
|
|
|
" vertices[3][0][1] = z;\n"
|
|
|
|
" for (i = 0; i < 4; i++) {\n"
|
|
|
|
" vertices[i][0][2] = z;\n"
|
|
|
|
" vertices[i][0][3] = 1.0;\n"
|
|
|
|
" vertices[i][1][0] = 2.0;\n"
|
|
|
|
" vertices[i][1][1] = 3.0;\n"
|
|
|
|
" vertices[i][1][2] = 4.0;\n"
|
|
|
|
" vertices[i][1][3] = 5.0;\n"
|
|
|
|
" }\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-03-02 13:17:15 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: vertices\n",
|
2015-08-29 19:00:09 +02:00
|
|
|
errout.str());
|
2021-11-08 20:31:16 +01:00
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" std::array<int, 3> *PArr[2] = { p0, p1 };\n"
|
|
|
|
" (*PArr[0])[2] = 0;\n"
|
|
|
|
" (*PArr[1])[2] = 0;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-08-29 19:00:09 +02:00
|
|
|
}
|
|
|
|
|
2015-10-24 12:06:40 +02:00
|
|
|
void uninitvar_cpp11ArrayInit() { // #7010
|
|
|
|
checkUninitVar("double foo(bool flag) {\n"
|
|
|
|
" double adIHPoint_local[4][4]{};\n"
|
|
|
|
" function(*adIHPoint_local);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2010-10-31 12:31:11 +01:00
|
|
|
// alloc..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_alloc() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" char *s = (char *)malloc(100);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" strcat(s, \"abc\");\n"
|
|
|
|
"};");
|
2015-07-23 14:51:38 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: s\n", errout.str());
|
2012-05-16 09:56:39 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char *s1 = new char[10];\n"
|
|
|
|
" char *s2 = new char[strlen(s1)];\n"
|
|
|
|
"};");
|
2012-11-01 18:40:20 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: s1\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" char *p = (char*)malloc(64);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x = p[0];\n"
|
|
|
|
"}");
|
2021-05-16 22:27:04 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
2015-07-23 17:20:11 +02:00
|
|
|
|
2015-09-14 09:03:21 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" char *p = (char*)malloc(64);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" if (p[0]) { }\n"
|
|
|
|
"}");
|
2021-05-17 09:22:19 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p[0]\n", errout.str());
|
2015-07-23 17:20:11 +02:00
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("char f() {\n"
|
|
|
|
" char *p = (char*)malloc(64);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" return p[0];\n"
|
|
|
|
"}");
|
2015-09-14 09:03:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Fred *fred = new Fred;\n"
|
|
|
|
" fred->foo();\n"
|
|
|
|
"}");
|
2015-01-22 10:35:17 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct Fred { int i; Fred(int, float); };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f() {\n"
|
|
|
|
" Fred *fred = new Fred(1, 2);\n"
|
|
|
|
" fred->foo();\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Fred *fred = malloc(sizeof(Fred));\n"
|
|
|
|
" x(&fred->f);\n"
|
|
|
|
"}");
|
2010-11-15 17:11:08 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" Fred *fred = malloc(sizeof(Fred));\n"
|
|
|
|
" x(fred->f);\n"
|
|
|
|
"}");
|
2015-01-21 22:26:44 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-11-27 17:34:54 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo(char *s)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char *a = malloc(100);\n"
|
|
|
|
" *a = *s;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char *a;\n"
|
|
|
|
" if (a);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char *a = malloc(100);\n"
|
|
|
|
" if (a);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" ABC *abc = malloc(100);\n"
|
|
|
|
" abc->a = 123;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" ABC *abc = malloc(100);\n"
|
|
|
|
" abc->a.word = 123;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" ABC *abc = malloc(100);\n"
|
|
|
|
" abc->a = 123;\n"
|
|
|
|
" abc->a += 123;\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" ABC *abc = malloc(100);\n"
|
|
|
|
" free(abc);\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" char *s = (char*)malloc(100);\n"
|
2021-05-17 09:22:19 +02:00
|
|
|
" if (!s)\n"
|
|
|
|
" return;\n"
|
|
|
|
" char c = *s;\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory is allocated but not initialized: s\n", "", errout.str());
|
2012-05-14 18:34:39 +02:00
|
|
|
|
|
|
|
// #3708 - false positive when using ptr typedef
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" uintptr_t x = malloc(100);\n"
|
|
|
|
" uintptr_t y = x + 10;\n" // <- not bad usage
|
|
|
|
"}");
|
2012-05-14 18:34:39 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-11-23 10:24:29 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" z_stream strm;\n"
|
|
|
|
" char* buf = malloc(10);\n"
|
|
|
|
" strm.next_out = buf;\n"
|
|
|
|
" deflate(&strm, Z_FINISH);\n"
|
|
|
|
" memcpy(body, buf, 10);\n"
|
|
|
|
"}");
|
2013-11-23 10:24:29 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-01-23 19:38:39 +01:00
|
|
|
|
|
|
|
// #6451 - allocation in subscope
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct StgStrm {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" StgIo& rIo;\n"
|
|
|
|
" StgStrm(StgIo&);\n"
|
|
|
|
" virtual sal_Int32 Write();\n"
|
|
|
|
"};\n"
|
|
|
|
"void Tmp2Strm() {\n"
|
|
|
|
" StgStrm* pNewStrm;\n"
|
|
|
|
" if (someflag)\n"
|
|
|
|
" pNewStrm = new StgStrm(rIo);\n"
|
|
|
|
" else\n"
|
|
|
|
" pNewStrm = new StgStrm(rIo);\n"
|
|
|
|
" pNewStrm->Write();\n"
|
|
|
|
"}");
|
2015-01-23 19:38:39 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// #6450 - calling a member function is allowed if memory was allocated by new
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct EMFPFont {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" bool family;\n"
|
|
|
|
" void Initialize();\n"
|
|
|
|
"};\n"
|
|
|
|
"void processObjectRecord() {\n"
|
|
|
|
" EMFPFont *font = new EMFPFont();\n"
|
|
|
|
" font->Initialize();\n"
|
|
|
|
"}");
|
2015-01-23 19:38:39 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2016-10-07 21:30:23 +02:00
|
|
|
|
|
|
|
// #7623 - new can also initialize the memory, don't warn in this case
|
|
|
|
checkUninitVar("void foo(){\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
" int* p1 = new int(314);\n"
|
|
|
|
" int* p2 = new int();\n"
|
|
|
|
" int* arr = new int[5]();\n"
|
|
|
|
" std::cout << *p1 << *p2 << arr[0];\n"
|
2016-10-07 21:30:23 +02:00
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-11-03 17:08:28 +01:00
|
|
|
// new in C code does not allocate..
|
|
|
|
checkUninitVar("int main() {\n"
|
|
|
|
" char * pBuf = new(10);\n"
|
|
|
|
" a = *pBuf;\n"
|
|
|
|
"}", "test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-03-24 20:08:51 +01:00
|
|
|
|
|
|
|
checkUninitVar("class A {};\n" // #10698
|
|
|
|
"void f() {\n"
|
|
|
|
" A* a = new A{};\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// class / struct..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_class() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("class Fred\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" int a() { return i; }\n"
|
|
|
|
"};");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" struct Relative {\n"
|
|
|
|
" Surface *surface;\n"
|
|
|
|
" void MoveTo(int x, int y) {\n"
|
|
|
|
" surface->MoveTo();\n"
|
|
|
|
" }\n"
|
|
|
|
" };\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" static const struct ab {\n"
|
|
|
|
" int a,b;\n"
|
|
|
|
" int get_a() { return a; }"
|
|
|
|
" } = { 0, 0 };\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" {\n"
|
|
|
|
" union ab {\n"
|
|
|
|
" int a,b;\n"
|
|
|
|
" }\n"
|
|
|
|
" i = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
" return i;\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-11-30 18:40:36 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct AB ab;\n"
|
|
|
|
" x = ab.x = 12;\n"
|
|
|
|
"}");
|
2010-11-30 18:40:36 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// enum..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_enum() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" enum AB { a, b };\n"
|
|
|
|
" AB ab;\n"
|
|
|
|
" if (ab);\n"
|
|
|
|
"}");
|
2015-09-06 18:37:22 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// references..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_references() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int &b = a;\n"
|
|
|
|
" b = 0;\n"
|
|
|
|
" int x = a;\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(struct blame_entry *ent)\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" struct origin *suspect = ent->suspect;\n"
|
|
|
|
" char hex[41];\n"
|
|
|
|
" strcpy(hex, sha1_to_hex(suspect->commit->object.sha1));\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" const std::string s(x());\n"
|
|
|
|
" strchr(s.c_str(), ',');\n"
|
|
|
|
"}");
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-07-21 11:46:41 +02:00
|
|
|
|
|
|
|
// #6717
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct thing { int value; };\n"
|
|
|
|
" thing it;\n"
|
|
|
|
" int& referenced_int = it.value;\n"
|
|
|
|
" referenced_int = 123;\n"
|
|
|
|
"}");
|
2015-07-21 11:46:41 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_return() {
|
2012-06-23 16:27:57 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}");
|
2015-07-23 08:46:59 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: ret\n", errout.str());
|
2012-06-23 16:27:57 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" return ret+5;\n"
|
|
|
|
"}");
|
2015-07-23 08:46:59 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: ret\n", errout.str());
|
2012-06-23 16:27:57 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" return ret = 5;\n"
|
|
|
|
"}");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
{
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
2018-04-17 14:23:04 +02:00
|
|
|
" cin >> ret;\n"
|
|
|
|
" return ret;\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"}");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static int foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" return cin >> ret;\n"
|
|
|
|
"}\n", "test.c");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: ret\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-01-22 06:37:09 +01:00
|
|
|
// Ticket #6341- False negative
|
|
|
|
{
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("wchar_t f() { int i; return btowc(i); }");
|
2015-01-22 06:37:09 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:1]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("wchar_t f(int i) { return btowc(i); }");
|
2015-01-22 06:37:09 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Avoid a potential false positive (#6341)
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar(
|
2015-01-22 06:37:09 +01:00
|
|
|
"void setvalue(int &x) {\n"
|
|
|
|
" x = 0;\n"
|
|
|
|
" return 123;\n"
|
|
|
|
"}\n"
|
|
|
|
"int f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" return setvalue(x);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"}");
|
2015-01-22 06:37:09 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-01-22 06:53:11 +01:00
|
|
|
}
|
2015-01-22 06:37:09 +01:00
|
|
|
|
2015-01-22 06:53:11 +01:00
|
|
|
// Ticket #5412 - False negative
|
|
|
|
{
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(bool b) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" double f;\n"
|
|
|
|
" if (b) { }\n"
|
|
|
|
" else {\n"
|
|
|
|
" f = 0.0;\n"
|
|
|
|
" }\n"
|
|
|
|
" printf (\"%f\",f);\n"
|
|
|
|
"}");
|
2015-07-23 08:46:59 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: f\n", errout.str());
|
2015-01-22 06:53:11 +01:00
|
|
|
|
|
|
|
// Check for potential FP
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(bool b) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" double f;\n"
|
|
|
|
" if (b) { f = 1.0 }\n"
|
|
|
|
" else {\n"
|
|
|
|
" f = 0.0;\n"
|
|
|
|
" }\n"
|
|
|
|
" printf (\"%f\",f);\n"
|
|
|
|
"}");
|
2015-01-22 06:53:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-01-22 06:37:09 +01:00
|
|
|
}
|
|
|
|
|
2012-06-23 16:27:57 +02:00
|
|
|
// Ticket #2146 - False negative
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int y;\n"
|
|
|
|
" return x ? 1 : y;\n"
|
|
|
|
"}");
|
2014-06-10 19:30:13 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: y\n", errout.str());
|
2012-06-23 16:27:57 +02:00
|
|
|
|
|
|
|
// Ticket #3106 - False positive
|
|
|
|
{
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" return x(&i) ? i : 0;\n"
|
|
|
|
"}");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" return x() ? i : 0;\n"
|
|
|
|
"}");
|
2012-06-23 16:27:57 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
}
|
2016-02-07 18:48:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void uninitvar_assign() { // = { .. }
|
|
|
|
// #1533
|
|
|
|
checkUninitVar("char a()\n"
|
|
|
|
"{\n"
|
|
|
|
" char key;\n"
|
|
|
|
" struct A msg = { .buf = {&key} };\n"
|
|
|
|
" init(&msg);\n"
|
|
|
|
" key++;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-06-23 16:27:57 +02:00
|
|
|
|
2014-04-10 06:40:53 +02:00
|
|
|
// Ticket #5660 - False positive
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int result;\n"
|
|
|
|
" int *res[] = {&result};\n"
|
|
|
|
" foo(res);\n"
|
|
|
|
" return result;\n"
|
|
|
|
"}");
|
2014-04-10 06:40:53 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2016-02-07 18:48:57 +01:00
|
|
|
|
|
|
|
// #6873
|
|
|
|
checkUninitVar("int f() {\n"
|
|
|
|
" char a[10];\n"
|
|
|
|
" char *b[] = {a};\n"
|
|
|
|
" foo(b);\n"
|
|
|
|
" return atoi(a);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// = { .. }
|
|
|
|
checkUninitVar("int f() {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int *p[] = { &a };\n"
|
|
|
|
" *p[0] = 0;\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2012-06-23 16:27:57 +02:00
|
|
|
}
|
|
|
|
|
2012-11-03 21:21:19 +01:00
|
|
|
// strncpy doesn't always null-terminate..
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_strncpy() {
|
2015-07-23 17:20:11 +02:00
|
|
|
// TODO: Add this checking
|
|
|
|
// Can it be added without hardcoding?
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a[100];\n"
|
|
|
|
" strncpy(a, s, 20);\n"
|
|
|
|
" strncat(a, s, 20);\n"
|
|
|
|
"}");
|
2015-07-23 17:20:11 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always null-terminate it).\n", "", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a[100];\n"
|
|
|
|
" strncpy(a, \"hello\", 3);\n"
|
|
|
|
" strncat(a, \"world\", 20);\n"
|
|
|
|
"}");
|
2015-07-23 17:20:11 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Dangerous usage of 'a' (strncpy doesn't always null-terminate it).\n", "", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f()\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"{\n"
|
|
|
|
" char a[100];\n"
|
|
|
|
" strncpy(a, \"hello\", sizeof(a));\n"
|
|
|
|
" strncat(a, \"world\", 20);\n"
|
|
|
|
"}", "test.cpp", false);
|
2010-10-31 12:31:11 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-10-29 19:11:42 +02:00
|
|
|
|
|
|
|
// #3245 - false positive
|
|
|
|
{
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char a[100];\n"
|
|
|
|
" strncpy(a,p,10);\n"
|
|
|
|
" memcmp(a,q,10);\n"
|
|
|
|
"}");
|
2011-10-29 19:11:42 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char a[100];\n"
|
|
|
|
" strncpy(a,p,10);\n"
|
|
|
|
" if (memcmp(a,q,10)==0);\n"
|
|
|
|
"}");
|
2011-10-29 19:11:42 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2011-12-23 08:44:28 +01:00
|
|
|
|
|
|
|
// Using strncpy isn't necessarily dangerous usage
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(const char dev[], char *str) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char buf[10];\n"
|
|
|
|
" strncpy(buf, dev, 10);\n"
|
|
|
|
" strncpy(str, buf, 10);\n"
|
|
|
|
"}");
|
2011-12-23 08:44:28 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-08-29 13:12:05 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char dst[4];\n"
|
|
|
|
" const char* source = \"You\";\n"
|
|
|
|
" strncpy(dst, source, sizeof(dst));\n"
|
|
|
|
" char value = dst[2];\n"
|
|
|
|
"}", "test.cpp", false);
|
2013-08-29 13:12:05 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-10-31 12:31:11 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 20:08:37 +01:00
|
|
|
// valid and invalid use of 'int a(int x) { return x + x; }'
|
2014-11-20 14:20:09 +01:00
|
|
|
void func_uninit_var() {
|
2013-03-20 15:36:16 +01:00
|
|
|
const std::string funca("int a(int x) { return x + x; }");
|
2011-02-26 20:08:37 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar((funca +
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" a(x);\n"
|
|
|
|
"}").c_str());
|
2013-03-20 15:36:16 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
2011-02-26 20:08:37 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar((funca +
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" int *p;\n"
|
|
|
|
" a(*p);\n"
|
|
|
|
"}").c_str());
|
2013-03-20 15:36:16 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
2011-02-26 20:08:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-12 12:42:16 +01:00
|
|
|
// valid and invalid use of 'void a(int *p) { *p = 0; }'
|
2014-11-20 14:20:09 +01:00
|
|
|
void func_uninit_pointer() {
|
2013-03-20 15:36:16 +01:00
|
|
|
const std::string funca("void a(int *p) { *p = 0; }");
|
2011-02-12 12:42:16 +01:00
|
|
|
|
|
|
|
// ok - initialized pointer
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar((funca +
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" int buf[10];\n"
|
|
|
|
" a(buf);\n"
|
|
|
|
"}").c_str());
|
2011-02-12 12:42:16 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// not ok - uninitialized pointer
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar((funca +
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" int *p;\n"
|
|
|
|
" a(p);\n"
|
|
|
|
"}").c_str());
|
2013-03-20 15:36:16 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
2011-02-12 12:42:16 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar_typeof() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct Fred *fred;\n"
|
|
|
|
" typeof(fred->x);\n"
|
|
|
|
"}");
|
2010-12-27 21:05:33 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-12-27 21:18:36 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct SData * s;\n"
|
|
|
|
" ab(typeof(s->status));\n"
|
|
|
|
"}");
|
2010-12-27 21:18:36 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-12-30 21:30:46 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct SData * s;\n"
|
|
|
|
" TYPEOF(s->status);\n"
|
|
|
|
"}");
|
2015-01-21 16:45:15 +01:00
|
|
|
TODO_ASSERT_EQUALS("", "[test.cpp:3]: (error) Uninitialized variable: s\n", errout.str());
|
2013-10-12 18:46:14 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-10-07 14:05:55 +02:00
|
|
|
" int *n = ({ typeof(*n) z; (typeof(*n)*)z; })\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"}", "test.cpp", false);
|
2013-10-12 18:46:14 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2010-12-27 21:05:33 +01:00
|
|
|
}
|
2011-12-13 21:57:27 +01:00
|
|
|
|
2021-05-24 17:50:28 +02:00
|
|
|
void uninitvar_ignore() {
|
|
|
|
checkUninitVar("void foo() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" dostuff((int&)i, 0);\n" // <- cast is not use
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-24 17:53:16 +02:00
|
|
|
checkUninitVar("int foo() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" return (int&)i + 2;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2021-05-24 17:50:28 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" dostuff(*&i, 0);\n" // <- *& is not use
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-24 17:53:16 +02:00
|
|
|
|
|
|
|
checkUninitVar("int foo() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" return *&i;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: i\n", errout.str());
|
2021-05-24 17:50:28 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar2() {
|
2011-12-14 06:00:17 +01:00
|
|
|
// using uninit var
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" x++;\n"
|
|
|
|
"}");
|
2011-12-14 06:00:17 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: char str[10];
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" str[x] = 0;\n"
|
|
|
|
"}");
|
2011-12-14 19:56:58 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2021-05-15 14:14:16 +02:00
|
|
|
checkUninitVar("void f() {\n" // #7736
|
|
|
|
" int buf[12];\n"
|
|
|
|
" printf (\"%d\", buf[0] );\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: buf\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" int y = x & 3;\n"
|
|
|
|
"}");
|
2011-12-14 06:00:17 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" int y = 3 & x;\n"
|
|
|
|
"}");
|
2011-12-14 18:28:30 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" x = 3 + x;\n"
|
|
|
|
"}");
|
2013-06-27 16:53:15 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" x = x;\n"
|
|
|
|
"}");
|
2013-11-16 18:07:33 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: struct ABC {int a;};
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct ABC *abc;\n"
|
|
|
|
" abc->a = 0;\n"
|
|
|
|
"}");
|
2011-12-26 14:01:46 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: abc\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" static int x;\n"
|
|
|
|
" return ++x;\n"
|
|
|
|
"}");
|
2011-12-26 22:14:52 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" extern int x;\n"
|
|
|
|
" return ++x;\n"
|
|
|
|
"}");
|
2012-06-26 18:28:41 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // #3926 - weird cast.
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" *(((char *)&x) + 0) = 0;\n"
|
|
|
|
"}", "test.c", false);
|
2012-07-03 18:52:23 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // #4737 - weird cast.
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" do_something(&((char*)&x)[0], 1);\n"
|
|
|
|
"}");
|
2013-05-01 13:52:28 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" char *p = (char*)&x + 1;\n"
|
|
|
|
"}", "test.cpp", false);
|
2013-12-13 20:10:22 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" i=f(), i!=2;\n"
|
|
|
|
"}");
|
2013-06-29 09:33:51 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2011-12-14 18:28:30 +01:00
|
|
|
// using uninit var in condition
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (x) { }\n"
|
|
|
|
"}");
|
2013-01-26 08:16:53 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (1 == (3 & x)) { }\n"
|
|
|
|
"}");
|
2011-12-14 18:28:30 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2012-01-02 11:25:13 +01:00
|
|
|
// ?:
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int *ptr) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" int *p = ptr ? ptr : &a;\n"
|
|
|
|
"}");
|
2012-01-02 11:25:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int a) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (a==3) { x=2; }\n"
|
|
|
|
" y = (a==3) ? x : a;\n"
|
|
|
|
"}");
|
2013-11-17 12:42:53 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2013-11-17 12:11:18 +01:00
|
|
|
// = ({ .. })
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x = ({ 1 + 2; });\n"
|
|
|
|
" int y = 1 + (x ? y : y);\n"
|
|
|
|
"}");
|
2013-11-17 12:11:18 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: y\n", errout.str());
|
|
|
|
|
2011-12-15 16:55:55 +01:00
|
|
|
// >> => initialization / usage
|
|
|
|
{
|
|
|
|
const char code[] = "void f() {\n"
|
|
|
|
" int x;\n"
|
2018-04-18 16:13:24 +02:00
|
|
|
" if (i >> x) { }\n"
|
2011-12-15 16:55:55 +01:00
|
|
|
"}";
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar(code, "test.cpp");
|
2011-12-15 16:55:55 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar(code, "test.c");
|
2011-12-15 16:55:55 +01:00
|
|
|
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i, i2;\n"
|
|
|
|
" strm >> i >> i2;\n"
|
|
|
|
"}");
|
2011-12-16 20:34:44 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2011-12-26 17:52:32 +01:00
|
|
|
// unconditional initialization
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" if (a) { ret = 1; }\n"
|
|
|
|
" else { {} ret = 2; }\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}");
|
2011-12-26 17:52:32 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2017-04-21 22:33:27 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
|
|
|
" int ret;\n"
|
|
|
|
" if (a) { ret = 1; }\n"
|
|
|
|
" else { s=foo(1,{2,3},4); ret = 2; }\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2011-12-14 06:00:17 +01:00
|
|
|
// conditional initialization
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (y == 1) { x = 1; }\n"
|
|
|
|
" else { if (y == 2) { x = 1; } }\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2011-12-26 12:36:35 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (y == 1) { x = 1; }\n"
|
|
|
|
" else { if (y == 2) { x = 1; } }\n"
|
|
|
|
" if (y == 3) { }\n" // <- ignore condition
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2011-12-26 12:36:35 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: x\n", errout.str());
|
2011-12-13 21:57:27 +01:00
|
|
|
|
2011-12-14 17:17:24 +01:00
|
|
|
// initialization in condition
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (init(&a)) { }\n"
|
|
|
|
" a++;\n"
|
|
|
|
"}");
|
2011-12-14 17:17:24 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2011-12-26 16:46:10 +01:00
|
|
|
// return, break, continue, goto
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (y == 1) { return; }\n"
|
|
|
|
" else { x = 1; }\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2011-12-13 21:57:27 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (y == 1) { return; }\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2012-11-29 18:41:48 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int ret;\n"
|
|
|
|
" if (!x) {\n"
|
|
|
|
" ret = -123;\n"
|
|
|
|
" goto out1;\n"
|
|
|
|
" }\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"out1:\n"
|
|
|
|
"out2:\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}", "test.c");
|
2012-12-25 10:37:21 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (x) {\n"
|
|
|
|
" i = 1;\n"
|
|
|
|
" } else {\n"
|
|
|
|
" goto out;\n"
|
|
|
|
" }\n"
|
|
|
|
" i++;\n"
|
|
|
|
"}");
|
2011-12-26 16:46:10 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i,x;\n"
|
|
|
|
" for (i=0;i<9;++i)\n"
|
|
|
|
" if (foo) break;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2012-11-29 18:41:48 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str());
|
2011-12-27 08:18:05 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" while (foo)\n"
|
|
|
|
" if (bar) break;\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2012-11-30 06:30:04 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2013-12-24 07:39:15 +01:00
|
|
|
// try/catch : don't warn about exception variable
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" try {\n"
|
|
|
|
" } catch (CException* e) {\n"
|
|
|
|
" trace();\n"
|
|
|
|
" e->Delete();\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-12-24 07:39:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // #5347
|
2015-07-23 20:53:50 +02:00
|
|
|
" try {\n"
|
|
|
|
" } catch (const char* e) {\n"
|
|
|
|
" A a = e;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2014-01-14 06:24:09 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2011-12-14 05:45:52 +01:00
|
|
|
// exit
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (y == 1) { exit(0); }\n"
|
|
|
|
" else { x = 1; }\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2011-12-13 21:57:27 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-12-15 16:49:14 +01:00
|
|
|
|
|
|
|
// strange code.. don't crash (#3415)
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" ({ if (0); });\n"
|
|
|
|
" for_each(i) { }\n"
|
|
|
|
"}", "test.c", false);
|
2011-12-15 18:15:58 +01:00
|
|
|
|
|
|
|
// if, if
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int a) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (a) i = 0;\n"
|
|
|
|
" if (a) i++;\n"
|
|
|
|
"}");
|
2011-12-15 18:15:58 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-12-15 20:29:57 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a,b=0;\n"
|
|
|
|
" if (x) {\n"
|
|
|
|
" if (y) {\n"
|
|
|
|
" a = 0;\n"
|
|
|
|
" b = 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" if (b) a++;\n"
|
|
|
|
"}");
|
2011-12-16 19:56:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a=0, b;\n"
|
|
|
|
" if (x) { }\n"
|
|
|
|
" else { if (y==2) { a=1; b=2; } }\n"
|
|
|
|
" if (a) { ++b; }\n"
|
|
|
|
"}");
|
2011-12-26 18:17:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void f(int x, int y) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (x == 0) { a = y; }\n"
|
|
|
|
" if (x == 0 && (a == 1)) { }\n"
|
|
|
|
"}");
|
2011-12-26 18:32:42 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a=0, b;\n"
|
|
|
|
" if (something) { a = dostuff(&b); }\n"
|
|
|
|
" if (!a || b) { }\n"
|
|
|
|
"}");
|
2014-11-01 14:03:02 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void f(int x, int y) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (x == 0 && (a == 1)) { }\n"
|
|
|
|
"}", "test.cpp", false);
|
2011-12-26 18:56:40 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2011-12-26 18:32:42 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (x) { a = 0; }\n"
|
|
|
|
" if (x) { if (y) { a++; } }\n"
|
|
|
|
"}");
|
2011-12-27 13:16:16 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (x) { a = 0; }\n"
|
|
|
|
" if (x) { if (y) { } else { a++; } }\n"
|
|
|
|
"}");
|
2011-12-27 13:16:16 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" if (x) ab = getAB();\n"
|
|
|
|
" else ab.a = 0;\n"
|
|
|
|
" if (ab.a == 1) b = ab.b;\n"
|
|
|
|
"}", "test.c");
|
2013-01-25 18:20:57 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x) { noreturn(); }\n"
|
|
|
|
" else { i = 0; }\n"
|
|
|
|
" if (i==1) { a = 0; }\n"
|
|
|
|
" else { a = 1; }\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}");
|
2013-01-28 18:08:20 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int a) {\n" // #4560
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x = 0, y;\n"
|
|
|
|
" if (a) x = 1;\n"
|
|
|
|
" else return 0;\n"
|
|
|
|
" if (x) y = 123;\n" // <- y is always initialized
|
|
|
|
" else {}\n"
|
|
|
|
" return y;\n"
|
|
|
|
"}");
|
2013-05-06 18:45:00 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-04-08 15:35:04 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int a) {\n" // #6583
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" if (a < 2) exit(1);\n"
|
|
|
|
" else if (a == 2) x = 0;\n"
|
|
|
|
" else exit(2);\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2015-04-08 15:35:04 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-05-06 18:45:00 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int a) {\n" // #4560
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x = 1, y;\n"
|
|
|
|
" if (a) x = 0;\n"
|
|
|
|
" else return 0;\n"
|
|
|
|
" if (x) {}\n"
|
|
|
|
" else y = 123;\n" // <- y is always initialized
|
|
|
|
" return y;\n"
|
|
|
|
"}");
|
2013-05-06 18:45:00 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-11-19 13:09:45 +01:00
|
|
|
checkUninitVar("void f(int x) {\n" // #3948
|
|
|
|
" int value;\n"
|
|
|
|
" if (x !=-1)\n"
|
|
|
|
" value = getvalue();\n"
|
|
|
|
" if (x == -1 || value > 300) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2020-05-07 22:25:08 +02:00
|
|
|
checkUninitVar("enum t_err { ERR_NONE, ERR_BAD_ARGS };\n" // #9649
|
|
|
|
"struct box_t { int value; };\n"
|
|
|
|
"int init_box(box_t *p, int v);\n"
|
|
|
|
"\n"
|
|
|
|
"void foo(int ret) {\n"
|
|
|
|
" box_t box2;\n"
|
|
|
|
" if (ret == ERR_NONE)\n"
|
|
|
|
" ret = init_box(&box2, 20);\n"
|
|
|
|
" if (ret == ERR_NONE)\n"
|
|
|
|
" z = x + box2.value;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-11-21 10:00:21 +01:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
|
|
|
" int value;\n"
|
|
|
|
" if (x == 32)\n"
|
|
|
|
" value = getvalue();\n"
|
|
|
|
" if (x == 1)\n"
|
|
|
|
" v = value;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: value\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f(int x) {\n"
|
|
|
|
" int value;\n"
|
|
|
|
" if (x == 32)\n"
|
|
|
|
" value = getvalue();\n"
|
|
|
|
" if (x == 32) {}\n"
|
|
|
|
" else v = value;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: value\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static int x;" // #4773
|
2015-07-23 20:53:50 +02:00
|
|
|
"int f() {\n"
|
|
|
|
" int y;\n"
|
|
|
|
" if (x) g();\n"
|
|
|
|
" if (x) y = 123;\n"
|
|
|
|
" else y = 456;\n"
|
|
|
|
" return y;\n"
|
|
|
|
"}");
|
2013-05-06 18:45:00 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static int x;" // #4773
|
2015-07-23 20:53:50 +02:00
|
|
|
"int f() {\n"
|
|
|
|
" int y;\n"
|
|
|
|
" if (!x) g();\n"
|
|
|
|
" if (x) y = 123;\n"
|
|
|
|
" else y = 456;\n"
|
|
|
|
" return y;\n"
|
|
|
|
"}");
|
2017-09-07 23:08:55 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f(int a) {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" if (a) x=123;\n"
|
|
|
|
" if (!a) {\n"
|
|
|
|
" if (!a) {}\n"
|
|
|
|
" else if (x) {}\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-02-11 17:05:59 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2011-12-15 20:29:57 +01:00
|
|
|
// asm
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" asm();\n"
|
|
|
|
" x++;\n"
|
|
|
|
"}");
|
2011-12-15 20:29:57 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-12-17 09:51:45 +01:00
|
|
|
|
2011-12-27 17:03:48 +01:00
|
|
|
// sizeof / typeof / offsetof / etc
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" sizeof(i+1);\n"
|
|
|
|
"}");
|
2011-12-17 09:51:45 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-12-17 16:08:55 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (100 == sizeof(i+1));\n"
|
|
|
|
"}");
|
2011-12-17 16:08:55 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-12-27 10:18:49 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct ABC *abc;\n"
|
|
|
|
" int i = ARRAY_SIZE(abc.a);"
|
|
|
|
"}");
|
2021-05-16 22:27:04 +02:00
|
|
|
// FP ASSERT_EQUALS("", errout.str());
|
2011-12-27 17:03:48 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int *abc;\n"
|
|
|
|
" typeof(*abc);\n"
|
|
|
|
"}");
|
2011-12-27 17:03:48 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-01-13 23:30:43 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct ABC *abc;\n"
|
|
|
|
" return do_something(typeof(*abc));\n"
|
|
|
|
"}");
|
2012-12-25 13:31:54 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" A *a;\n"
|
|
|
|
" a = malloc(sizeof(*a));\n"
|
|
|
|
"}");
|
2013-07-06 08:52:47 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2012-12-28 12:32:15 +01:00
|
|
|
// &
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // #4426 - address of uninitialized variable
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a,b;\n"
|
|
|
|
" if (&a == &b);\n"
|
|
|
|
"}");
|
2012-12-28 12:32:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-03-13 17:32:47 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // #4439 - cast address of uninitialized variable
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
2017-09-08 22:52:16 +02:00
|
|
|
" x((LPARAM)(RECT*)&a);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"}");
|
2013-03-13 17:32:47 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-04-09 17:02:11 +02:00
|
|
|
|
2019-02-26 19:26:46 +01:00
|
|
|
checkUninitVar("int main() {\n"
|
|
|
|
" int done;\n"
|
2019-02-26 19:28:02 +01:00
|
|
|
" dostuff(1, (AuPointer) &done);\n" // <- It is not conclusive if the "&" is a binary or unary operator. Bailout.
|
2019-02-26 19:26:46 +01:00
|
|
|
"}");
|
2021-05-17 09:22:19 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-02-26 19:26:46 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // #4778 - cast address of uninitialized variable
|
2015-07-23 20:53:50 +02:00
|
|
|
" long a;\n"
|
|
|
|
" &a;\n"
|
|
|
|
"}");
|
2013-06-14 07:21:53 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // #4717 - ({})
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a = ({ long b = (long)(123); 2 + b; });\n"
|
|
|
|
"}", "test.c");
|
2013-04-09 17:02:11 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2011-12-13 21:57:27 +01:00
|
|
|
}
|
2012-06-08 16:17:55 +02:00
|
|
|
|
|
|
|
// #3869 - reference variable
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar4() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int buf[10];\n"
|
|
|
|
" int &x = buf[0];\n"
|
|
|
|
" buf[0] = 0;\n"
|
|
|
|
" x++;\n"
|
|
|
|
"}");
|
2012-06-08 16:17:55 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
2012-06-13 19:09:51 +02:00
|
|
|
|
|
|
|
// #3861
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar5() {
|
2012-06-13 19:09:51 +02:00
|
|
|
// ensure there is no false positive
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" x<char> c;\n"
|
|
|
|
" c << 2345;\n"
|
|
|
|
"}");
|
2012-06-13 19:09:51 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// ensure there is no false negative
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char c;\n"
|
|
|
|
" char a = c << 2;\n"
|
|
|
|
"}");
|
2012-06-13 19:09:51 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: c\n", errout.str());
|
|
|
|
}
|
2012-06-22 16:39:39 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar8() {
|
2014-11-17 16:04:44 +01:00
|
|
|
const char code[] = "struct Fred {\n"
|
|
|
|
" void Sync(dsmp_t& type, int& len, int limit = 123);\n"
|
|
|
|
" void Sync(int& syncpos, dsmp_t& type, int& len, int limit = 123);\n"
|
|
|
|
" void FindSyncPoint();\n"
|
|
|
|
"};\n"
|
|
|
|
"void Fred::FindSyncPoint() {\n"
|
|
|
|
" dsmp_t type;\n"
|
|
|
|
" int syncpos, len;\n"
|
|
|
|
" Sync(syncpos, type, len);\n"
|
|
|
|
"}";
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar(code, "test.cpp");
|
2014-11-17 16:04:44 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-01-19 06:38:54 +01:00
|
|
|
void uninitvar9() { // 6424
|
|
|
|
const char code[] = "namespace Ns { class C; }\n"
|
|
|
|
"void f1() { char *p; *p = 0; }\n"
|
|
|
|
"class Ns::C* p;\n"
|
|
|
|
"void f2() { char *p; *p = 0; }";
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar(code, "test.cpp");
|
2015-01-19 06:38:54 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: p\n"
|
|
|
|
"[test.cpp:4]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2019-11-13 21:34:27 +01:00
|
|
|
void uninitvar10() { // 9467
|
|
|
|
const char code[] = "class Foo {\n"
|
|
|
|
" template <unsigned int i>\n"
|
|
|
|
" bool bar() {\n"
|
|
|
|
" return true;\n"
|
|
|
|
" }\n"
|
|
|
|
"};\n"
|
|
|
|
"template <>\n"
|
|
|
|
"bool Foo::bar<9>() {\n"
|
|
|
|
" return true;\n"
|
|
|
|
"}\n"
|
|
|
|
"int global() {\n"
|
|
|
|
" int bar = 1;\n"
|
|
|
|
" return bar;\n"
|
|
|
|
"}";
|
|
|
|
checkUninitVar(code, "test.cpp");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2020-04-10 12:33:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uninitvar11() { // 9123
|
|
|
|
const char code[] = "bool get(int &var);\n"
|
|
|
|
"void foo () {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" x = get(x) && x;\n"
|
|
|
|
"}";
|
|
|
|
checkUninitVar(code, "test.cpp");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-11-13 21:34:27 +01:00
|
|
|
}
|
|
|
|
|
2021-05-10 19:48:07 +02:00
|
|
|
void uninitvar12() { // 10218
|
|
|
|
const char code[] = "void fp() {\n"
|
|
|
|
" std::stringstream ss;\n"
|
|
|
|
" for (int i; ss >> i;) {}\n"
|
|
|
|
"}";
|
|
|
|
checkUninitVar(code);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2021-07-06 14:24:07 +02:00
|
|
|
void uninitvar13() { // #9772 - FP
|
|
|
|
const char code[] = "int func(void)\n"
|
|
|
|
"{ int rez;\n"
|
|
|
|
" struct sccb* ccb;\n"
|
|
|
|
" \n"
|
|
|
|
" do\n"
|
|
|
|
" { if ((ccb = calloc(1, sizeof(*ccb))) == NULL)\n"
|
|
|
|
" { rez = 1;\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" rez = 0;\n"
|
|
|
|
" } while (0);\n"
|
|
|
|
" \n"
|
|
|
|
" if (rez != 0)\n"
|
|
|
|
" free(ccb);\n"
|
|
|
|
" \n"
|
|
|
|
" return rez;\n"
|
|
|
|
"}";
|
|
|
|
checkUninitVar(code);
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-01-22 13:51:43 +01:00
|
|
|
void uninitvar_unconditionalTry() {
|
|
|
|
// Unconditional scopes and try{} scopes
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" {\n"
|
|
|
|
" return i;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2015-01-22 13:51:43 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" try {\n"
|
|
|
|
" return i;\n"
|
|
|
|
" } catch(...) {}\n"
|
|
|
|
"}");
|
2015-01-22 13:51:43 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
|
2022-03-15 14:32:33 +01:00
|
|
|
|
|
|
|
checkUninitVar("void f(bool x) {\n"
|
|
|
|
" bool b;\n"
|
|
|
|
" {\n"
|
|
|
|
" auto g = []{};\n"
|
|
|
|
" b = x;\n"
|
|
|
|
" }\n"
|
|
|
|
" if (b) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f(bool x) {\n"
|
|
|
|
" bool b;\n"
|
|
|
|
" {\n"
|
|
|
|
" int i[2]{ 1, 2 };\n"
|
|
|
|
" b = x;\n"
|
|
|
|
" }\n"
|
|
|
|
" if (b) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f(bool x) {\n"
|
|
|
|
" bool b;\n"
|
|
|
|
" {\n"
|
|
|
|
" auto g = []{};\n"
|
|
|
|
" }\n"
|
|
|
|
" if (b) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: b\n", errout.str());
|
2015-01-22 13:51:43 +01:00
|
|
|
}
|
|
|
|
|
2015-01-30 19:16:25 +01:00
|
|
|
void uninitvar_funcptr() {
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.disable
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void getLibraryContainer() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" Reference< XStorageBasedLibraryContainer >(*Factory)(const Reference< XComponentContext >&, const Reference< XStorageBasedDocument >&)\n"
|
|
|
|
" = &DocumentDialogLibraryContainer::create;\n"
|
|
|
|
" rxContainer.set((*Factory)(m_aContext, xDocument));\n"
|
|
|
|
"}");
|
2015-01-30 19:16:25 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-02-10 19:00:01 +01:00
|
|
|
checkUninitVar("void foo() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" void* x;\n"
|
2019-02-10 19:00:01 +01:00
|
|
|
" int (*f)(int, int) = x;\n"
|
|
|
|
" dostuff((*f)(a,b));\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
2015-01-30 19:16:25 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void getLibraryContainer() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" Reference< XStorageBasedLibraryContainer >(*Factory)(const Reference< XComponentContext >&, const Reference< XStorageBasedDocument >&);\n"
|
|
|
|
" rxContainer.set((*Factory)(m_aContext, xDocument));\n"
|
|
|
|
"}");
|
2015-01-30 19:16:25 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: Factory\n", errout.str());
|
2021-05-13 20:21:02 +02:00
|
|
|
|
|
|
|
// extracttests.enable
|
2015-01-30 19:16:25 +01:00
|
|
|
}
|
2015-05-10 14:27:15 +02:00
|
|
|
|
2015-06-05 23:57:42 +02:00
|
|
|
void uninitvar_operator() { // Ticket #6463, #6680
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct Source { Source& operator>>(int& i) { i = 0; return *this; } };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"struct Sink { int v; };\n"
|
|
|
|
"Source foo;\n"
|
|
|
|
"void uninit() {\n"
|
|
|
|
" Sink s;\n"
|
|
|
|
" int n = 1 >> s.v;\n" // Not initialized
|
|
|
|
"};\n"
|
|
|
|
"void notUninit() {\n"
|
|
|
|
" Sink s1;\n"
|
|
|
|
" foo >> s1.v;\n" // Initialized by operator>>
|
|
|
|
" Sink s2;\n"
|
|
|
|
" int n;\n"
|
|
|
|
" foo >> s2.v >> n;\n" // Initialized by operator>>
|
|
|
|
"}");
|
2015-05-10 14:27:15 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized struct member: s.v\n", errout.str());
|
2015-06-05 23:57:42 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct Fred { int a; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void foo() {\n"
|
|
|
|
" Fred fred;\n"
|
|
|
|
" std::cin >> fred.a;\n"
|
|
|
|
"}");
|
2015-06-05 23:57:42 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-05-10 14:27:15 +02:00
|
|
|
}
|
|
|
|
|
2012-12-21 16:57:49 +01:00
|
|
|
// Handling of function calls
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar2_func() {
|
2021-10-15 22:50:29 +02:00
|
|
|
// #4716
|
|
|
|
checkUninitVar("void bar(const int a, const int * const b);\n"
|
|
|
|
"int foo(void) {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int *b = 0;\n"
|
|
|
|
" bar(a,b);\n" // <<
|
|
|
|
" return 0;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
|
2021-10-16 00:31:55 +02:00
|
|
|
|
2012-12-21 19:32:56 +01:00
|
|
|
// non-pointer variable
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(char);\n" // value => error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char c;\n"
|
|
|
|
" a(c);\n"
|
|
|
|
"}");
|
2013-11-16 12:02:21 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(char c);\n" // value => error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char c;\n"
|
|
|
|
" a(c);\n"
|
|
|
|
"}");
|
2012-12-20 19:45:30 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(const char c);\n" // const value => error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char c;\n"
|
|
|
|
" a(c);\n"
|
|
|
|
"}");
|
2012-12-20 19:45:30 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(char *c);\n" // address => no error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char c;\n"
|
|
|
|
" a(&c);\n"
|
|
|
|
"}");
|
2012-12-20 19:45:30 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-12-25 14:05:29 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(pstr s);\n" // address => no error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char c;\n"
|
|
|
|
" a(&c);\n"
|
|
|
|
"}");
|
2012-12-25 14:05:29 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-12-20 19:45:30 +01:00
|
|
|
|
2021-05-23 11:45:32 +02:00
|
|
|
checkUninitVar("void a(const char *c);\n" // const address => data is not changed
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char c;\n"
|
2021-05-23 11:45:32 +02:00
|
|
|
" a(&c);\n" // <- no warning
|
|
|
|
" c++;\n" // <- uninitialized variable
|
2015-07-23 20:53:50 +02:00
|
|
|
"}");
|
2021-05-23 11:45:32 +02:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: c\n", "", errout.str());
|
2012-12-20 19:45:30 +01:00
|
|
|
|
2012-12-21 19:32:56 +01:00
|
|
|
// pointer variable
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(char c);\n" // value => error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char *c;\n"
|
|
|
|
" a(*c);\n"
|
|
|
|
"}");
|
2012-12-21 19:32:56 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
2012-12-21 16:57:49 +01:00
|
|
|
|
2012-12-21 19:32:56 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(char *c);\n" // address => error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char *c;\n"
|
|
|
|
" a(c);\n"
|
|
|
|
"}");
|
2012-12-21 19:32:56 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("typedef struct { int a; int b; } AB;\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void a(AB *ab);\n"
|
|
|
|
"void b() {\n"
|
|
|
|
" AB *ab;\n"
|
|
|
|
" a(ab);\n"
|
|
|
|
"}");
|
2013-12-10 18:36:16 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(const char *c);\n" // const address => error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char *c;\n"
|
|
|
|
" a(c);\n"
|
|
|
|
"}");
|
2013-11-16 12:02:21 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(const char c[]);\n" // const address => error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char *c;\n"
|
|
|
|
" a(c);\n"
|
|
|
|
"}");
|
2012-12-21 19:32:56 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str());
|
2012-12-21 17:04:15 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(char **c);\n" // address of pointer => no error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char *c;\n"
|
|
|
|
" a(&c);\n"
|
|
|
|
"}");
|
2012-12-25 13:58:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(char *c);\n" // address of pointer (suspicious cast to pointer) => no error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" char *c;\n"
|
|
|
|
" a(&c);\n"
|
|
|
|
"}");
|
2012-12-21 19:32:56 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void a(const char **c);\n" // const address of pointer => no error
|
2015-07-23 20:53:50 +02:00
|
|
|
"void b() {\n"
|
|
|
|
" const char *c;\n"
|
|
|
|
" a(&c);\n"
|
|
|
|
"}");
|
2012-12-21 16:57:49 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-04-10 20:04:32 +02:00
|
|
|
|
2015-08-08 09:57:56 +02:00
|
|
|
// array
|
|
|
|
checkUninitVar("int calc(const int *p, int n);\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" int x[10];\n"
|
|
|
|
" calc(x,10);\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n",
|
|
|
|
"", errout.str());
|
|
|
|
|
2015-11-10 10:18:24 +01:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int x[10];\n"
|
|
|
|
" int &x0(*x);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-08-08 09:57:56 +02:00
|
|
|
// ....
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct ABC { int a; };\n" // struct initialization
|
2015-07-23 20:53:50 +02:00
|
|
|
"void clear(struct ABC &abc);\n"
|
|
|
|
"int f() {\n"
|
|
|
|
" struct ABC abc;\n"
|
|
|
|
" clear(abc);\n"
|
|
|
|
" return abc.a;\n"
|
|
|
|
"}");
|
2013-04-10 20:04:32 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-01-22 21:22:02 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void write_packet() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" time_t now0;\n"
|
|
|
|
" time(&now0);\n"
|
|
|
|
"}", "test.c");
|
2015-01-22 21:22:02 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void write_packet() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" time_t* now0;\n"
|
|
|
|
" time(now0);\n"
|
|
|
|
"}", "test.c");
|
2015-01-22 21:22:02 +01:00
|
|
|
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: now0\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void write_packet() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char now0;\n"
|
|
|
|
" strcmp(&now0, sth);\n"
|
|
|
|
"}", "test.c");
|
2021-05-23 14:57:43 +02:00
|
|
|
ASSERT_EQUALS("[test.c:3]: (error) Uninitialized variable: now0\n", errout.str());
|
2015-08-08 09:57:56 +02:00
|
|
|
|
|
|
|
// #2775 - uninitialized struct pointer in subfunction
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: struct Fred {int x;};
|
2015-08-08 09:57:56 +02:00
|
|
|
checkUninitVar("void a(struct Fred *fred) {\n"
|
|
|
|
" fred->x = 0;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void b() {\n"
|
|
|
|
" struct Fred *p;\n"
|
|
|
|
" a(p);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
|
|
|
// #2946 - FP array is initialized in subfunction
|
|
|
|
checkUninitVar("void a(char *buf) {\n"
|
|
|
|
" buf[0] = 0;\n"
|
|
|
|
"}\n"
|
|
|
|
"void b() {\n"
|
|
|
|
" char buf[10];\n"
|
|
|
|
" a(buf);\n"
|
|
|
|
" buf[1] = buf[0];\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2018-02-04 22:30:08 +01:00
|
|
|
|
|
|
|
// unknown macro
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" struct listnode *item;\n"
|
|
|
|
" list_for_each(item, &key_list) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-12-20 19:45:30 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar2_value() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (x) {\n"
|
|
|
|
" int y = -ENOMEM;\n" // assume constant ENOMEM is nonzero since it's negated
|
|
|
|
" if (y != 0) return;\n"
|
|
|
|
" i++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}", "test.cpp", false);
|
2012-12-24 19:11:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i, y;\n"
|
|
|
|
" if (x) {\n"
|
|
|
|
" y = -ENOMEM;\n" // assume constant ENOMEM is nonzero since it's negated
|
|
|
|
" if (y != 0) return;\n"
|
|
|
|
" i++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}", "test.cpp", false);
|
2012-12-24 19:11:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i, y;\n"
|
|
|
|
" if (x) y = -ENOMEM;\n" // assume constant ENOMEM is nonzero since it's negated
|
|
|
|
" else y = get_value(i);\n"
|
|
|
|
" if (y != 0) return;\n" // <- condition is always true if i is uninitialized
|
|
|
|
" i++;\n"
|
|
|
|
"}");
|
2012-12-24 19:11:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (x) i = 0;\n"
|
|
|
|
" if (!x || i>0) {}\n" // <- no error
|
|
|
|
"}");
|
2012-12-24 19:11:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (!x) { }\n"
|
|
|
|
" else i = 0;\n"
|
|
|
|
" if (x || i>0) {}\n"
|
|
|
|
"}");
|
2012-12-24 19:11:13 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: i\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (x) { }\n"
|
|
|
|
" else i = 0;\n"
|
|
|
|
" if (x || i>0) {}\n" // <- no error
|
|
|
|
"}");
|
2012-12-24 19:11:13 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2012-12-27 18:45:00 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int y;\n"
|
|
|
|
" if (x) y = do_something();\n"
|
|
|
|
" if (!x) return 0;\n"
|
|
|
|
" return y;\n"
|
|
|
|
"}");
|
2013-04-08 19:34:39 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: int y;
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int x) {\n" // FP with ?:
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (x)\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" a = y;\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" return x ? 2*a : 0;\n"
|
|
|
|
"}");
|
2013-07-10 16:44:35 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" if (x)\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" a = y;\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" return y ? 2*a : 3*a;\n"
|
|
|
|
"}");
|
2015-07-22 20:31:58 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
|
2013-07-10 16:44:35 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n" // Don't crash
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" dostuff(\"ab\" cd \"ef\", x?a:z);\n" // <- No AST is created for ?:
|
|
|
|
"}");
|
2015-07-22 22:17:12 +02:00
|
|
|
|
2012-12-27 18:45:00 +01:00
|
|
|
// Unknown => bail out..
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (a(x)) i = 0;\n"
|
|
|
|
" if (b(x)) return;\n"
|
|
|
|
" i++;\n" // <- no error if b(x) is always true when a(x) is false
|
|
|
|
"}");
|
2012-12-27 18:45:00 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-05-29 16:16:12 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (x) i = 0;\n"
|
|
|
|
" while (condition) {\n"
|
|
|
|
" if (x) i++;\n" // <- no error
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-05-29 16:16:12 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-06-02 18:26:03 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int x) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" if (x) i = 0;\n"
|
|
|
|
" while (condition) {\n"
|
|
|
|
" i++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-06-02 18:26:03 +02:00
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
2012-12-24 19:11:13 +01:00
|
|
|
}
|
2013-01-16 20:28:29 +01:00
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
#define valueFlowUninit(...) valueFlowUninit_(__FILE__, __LINE__, __VA_ARGS__)
|
2021-10-30 22:13:58 +02:00
|
|
|
void valueFlowUninit2_value()
|
|
|
|
{
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x) {\n"
|
|
|
|
" int y = -ENOMEM;\n" // assume constant ENOMEM is nonzero since it's negated
|
|
|
|
" if (y != 0) return;\n"
|
|
|
|
" i++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}",
|
|
|
|
"test.cpp");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" int i, y;\n"
|
|
|
|
" if (x) {\n"
|
|
|
|
" y = -ENOMEM;\n" // assume constant ENOMEM is nonzero since it's negated
|
|
|
|
" if (y != 0) return;\n"
|
|
|
|
" i++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}",
|
|
|
|
"test.cpp");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" int i, y;\n"
|
|
|
|
" if (x) y = -ENOMEM;\n" // assume constant ENOMEM is nonzero since it's negated
|
|
|
|
" else y = get_value(i);\n"
|
|
|
|
" if (y != 0) return;\n" // <- condition is always true if i is uninitialized
|
|
|
|
" i++;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f(int x) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (!x) i = 0;\n"
|
|
|
|
" if (!x || i>0) {}\n" // <- error
|
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Uninitialized variable: i\n", errout.str());
|
2021-10-30 22:13:58 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f(int x) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x) i = 0;\n"
|
|
|
|
" if (!x || i>0) {}\n" // <- no error
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f(int x) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (!x) { }\n"
|
|
|
|
" else i = 0;\n"
|
|
|
|
" if (x || i>0) {}\n"
|
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Uninitialized variable: i\n", errout.str());
|
2021-10-30 22:13:58 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f(int x) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x) { }\n"
|
|
|
|
" else i = 0;\n"
|
|
|
|
" if (x || i>0) {}\n" // <- no error
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("int f(int x) {\n"
|
|
|
|
" int y;\n"
|
|
|
|
" if (x) y = do_something();\n"
|
|
|
|
" if (!x) return 0;\n"
|
|
|
|
" return y;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// extracttests.start: int y;
|
|
|
|
valueFlowUninit("int f(int x) {\n" // FP with ?:
|
|
|
|
" int a;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" a = y;\n"
|
|
|
|
" return x ? 2*a : 0;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("int f(int x) {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" if (x)\n"
|
|
|
|
" a = y;\n"
|
|
|
|
" return y ? 2*a : 3*a;\n"
|
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Uninitialized variable: a\n", errout.str());
|
2021-10-30 22:13:58 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n" // Don't crash
|
|
|
|
" int a;\n"
|
|
|
|
" dostuff(\"ab\" cd \"ef\", x?a:z);\n" // <- No AST is created for ?:
|
|
|
|
"}");
|
|
|
|
|
|
|
|
// Unknown => bail out..
|
|
|
|
valueFlowUninit("void f(int x) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (a(x)) i = 0;\n"
|
|
|
|
" if (b(x)) return;\n"
|
|
|
|
" i++;\n" // <- no error if b(x) is always true when a(x) is false
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f(int x) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x) i = 0;\n"
|
|
|
|
" while (condition) {\n"
|
|
|
|
" if (x) i++;\n" // <- no error
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f(int x) {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" if (x) i = 0;\n"
|
|
|
|
" while (condition) {\n"
|
|
|
|
" i++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
2022-04-26 07:45:08 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f ( void ){\n" // #9313 - FN
|
|
|
|
" int *p;\n"
|
|
|
|
" int a[ 2 ] = { [ 0 ] = *p++, [ 1 ] = 1 };\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
2022-05-04 05:59:01 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f(int height) {\n"
|
|
|
|
" int a[11];\n"
|
|
|
|
" int *p = a;\n"
|
|
|
|
" int step = 2;\n"
|
|
|
|
" for (int i = 0; i < (height * step); i += step)\n"
|
|
|
|
" *p++ = 0;\n"
|
|
|
|
" for (int i = 0; i < height; i++)\n"
|
|
|
|
" if (a[i]) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-07-08 12:39:29 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f(void) {\n"
|
|
|
|
" char *c;\n"
|
|
|
|
" char x;\n"
|
|
|
|
" while (true) {\n"
|
|
|
|
" c = &x;\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" ++c;\n"
|
|
|
|
"}", "test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-10-30 22:13:58 +02:00
|
|
|
}
|
|
|
|
|
2019-12-19 20:18:25 +01:00
|
|
|
void uninitStructMember() { // struct members
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int a = ab.a;\n"
|
|
|
|
"}");
|
2013-02-15 18:13:47 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" AB ab;\n"
|
|
|
|
" int a = ab.a;\n"
|
|
|
|
"}");
|
2015-01-21 12:20:03 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = ab.a + 1;\n"
|
|
|
|
"}");
|
2013-06-27 16:53:15 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void do_something(const struct AB ab);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-17 23:55:13 +01:00
|
|
|
ASSERT_EQUALS("[test.c:6]: (error) Uninitialized struct member: ab.b\n", errout.str());
|
2013-01-16 20:28:29 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n" // #4760
|
2015-07-23 20:53:50 +02:00
|
|
|
"void do_something(int a);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" do_something(ab.a);\n"
|
|
|
|
"}\n", "test.c");
|
2019-03-09 11:30:45 +01:00
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
2015-06-19 18:21:46 +02:00
|
|
|
|
2018-08-15 18:04:36 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
|
|
|
"void do_something(const struct AB &ab) { a = ab.a; }\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" do_something(ab);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2018-08-15 18:04:36 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int a = ab.a;\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-19 10:09:58 +01:00
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
2019-08-24 14:43:35 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" AB ab1;\n"
|
|
|
|
" AB ab2 = { ab1.a, 0 };\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab1.a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" buf[ab.a] = 0;\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-19 10:09:58 +01:00
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 1;\n"
|
|
|
|
" x = ab;\n"
|
|
|
|
"}\n", "test.c");
|
2014-05-10 19:56:44 +02:00
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 1;\n"
|
|
|
|
" x = *(&ab);\n"
|
|
|
|
"}\n", "test.c");
|
2014-05-10 20:20:55 +02:00
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
|
|
|
|
|
2019-02-27 18:44:30 +01:00
|
|
|
checkUninitVar("void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int x;\n"
|
|
|
|
" ab.a = (addr)&x;\n"
|
|
|
|
" dostuff(&ab,0);\n"
|
|
|
|
"}\n", "test.c");
|
2021-05-17 09:22:19 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-02-27 18:44:30 +01:00
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" static void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->f();\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" static void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).f();\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" static int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->v;\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" static int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).v;\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->f();\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).f();\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->v;\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("struct Element {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).v;\n"
|
|
|
|
"}");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n" // pass struct member by address
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" assign(&ab.a, 0);\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-20 13:12:40 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-25 16:13:32 +02:00
|
|
|
checkUninitVar("struct Cstring { char *text; int size, alloc; };\n"
|
|
|
|
"int maybe();\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" Cstring res;\n"
|
|
|
|
" if ( ! maybe() ) return;\n" // <- fp goes away if this is removed
|
|
|
|
" ( ((res).text = (void*)0), ((res).size = (res).alloc = 0) );\n" // <- fp goes away if parentheses are removed
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-03-08 18:42:24 +01:00
|
|
|
{
|
|
|
|
const char argDirectionsTestXmlData[] = "<?xml version=\"1.0\"?>\n"
|
|
|
|
"<def>\n"
|
|
|
|
" <function name=\"uninitvar_funcArgInTest\">\n"
|
|
|
|
" <arg nr=\"1\" direction=\"in\"/>\n"
|
|
|
|
" </function>\n"
|
|
|
|
" <function name=\"uninitvar_funcArgOutTest\">\n"
|
|
|
|
" <arg nr=\"1\" direction=\"out\"/>\n"
|
|
|
|
" </function>\n"
|
|
|
|
"</def>";
|
|
|
|
|
|
|
|
ASSERT_EQUALS(true, settings.library.loadxmldata(argDirectionsTestXmlData, sizeof(argDirectionsTestXmlData) / sizeof(argDirectionsTestXmlData[0])));
|
|
|
|
|
|
|
|
checkUninitVar("struct AB { int a; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" uninitvar_funcArgInTest(&ab);\n"
|
|
|
|
" x = ab;\n"
|
|
|
|
"}\n", "test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("struct AB { int a; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" uninitvar_funcArgOutTest(&ab);\n"
|
|
|
|
" x = ab;\n"
|
|
|
|
"}\n", "test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void do_something(const struct AB ab);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" ab.b = 0;\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-16 20:28:29 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-01-17 17:01:04 +01:00
|
|
|
|
2014-09-15 15:58:21 +02:00
|
|
|
{
|
2015-11-22 16:20:46 +01:00
|
|
|
checkUninitVar("struct AB { char a[10]; };\n"
|
|
|
|
"void f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct AB ab;\n"
|
|
|
|
" strcpy(ab.a, STR);\n"
|
|
|
|
"}\n", "test.c");
|
2014-09-15 15:58:21 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-02-27 17:58:25 +01:00
|
|
|
checkUninitVar("struct AB { unsigned char a[10]; };\n" // #8999 - cast
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" strcpy((char *)ab.a, STR);\n"
|
|
|
|
"}\n", "test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-11-22 16:20:46 +01:00
|
|
|
checkUninitVar("struct AB { char a[10]; };\n"
|
|
|
|
"void f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct AB ab;\n"
|
|
|
|
" strcpy(x, ab.a);\n"
|
|
|
|
"}\n", "test.c");
|
2019-03-09 11:30:45 +01:00
|
|
|
TODO_ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab.a\n", "", errout.str());
|
2014-09-15 15:58:21 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" dosomething(ab.a);\n"
|
|
|
|
"}\n", "test.c");
|
2019-03-09 11:30:45 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2014-09-15 15:58:21 +02:00
|
|
|
}
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void do_something(const struct AB ab);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab = getAB();\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-17 17:01:04 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-01-17 21:04:22 +01:00
|
|
|
|
2015-11-22 16:20:46 +01:00
|
|
|
{
|
|
|
|
// #6769 - calling method that might assign struct members
|
|
|
|
checkUninitVar("struct AB { int a; int b; void set(); };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.set();\n"
|
|
|
|
" x = ab;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2015-11-22 16:20:46 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("struct AB { int a; int get() const; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.get();\n"
|
|
|
|
" x = ab;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2015-11-22 16:20:46 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("struct AB { int a; void dostuff() {} };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.dostuff();\n"
|
|
|
|
" x = ab;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2015-11-22 16:20:46 +01:00
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; struct { int b; int c; } s; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void do_something(const struct AB ab);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 1;\n"
|
|
|
|
" ab.s.b = 2;\n"
|
|
|
|
" ab.s.c = 3;\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-17 21:04:22 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct conf {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char x;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void do_something(struct conf ant_conf);\n"
|
|
|
|
"\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct conf c;\n"
|
|
|
|
" initdata(&c);\n"
|
|
|
|
" do_something(c);\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-17 21:04:22 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-01-18 21:26:28 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct PIXEL {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" union {\n"
|
|
|
|
" struct { unsigned char red,green,blue,alpha; };\n"
|
|
|
|
" unsigned int color;\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"unsigned char f() {\n"
|
|
|
|
" struct PIXEL p1;\n"
|
|
|
|
" p1.color = 255;\n"
|
|
|
|
" return p1.red;\n"
|
|
|
|
"}");
|
2013-09-30 06:35:31 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"int f() {\n"
|
|
|
|
" struct AB *ab;\n"
|
|
|
|
" for (i = 1; i < 10; i++) {\n"
|
|
|
|
" if (condition && (ab = getab()) != NULL) {\n"
|
|
|
|
" a = ab->a;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-12-04 20:32:20 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"int f(int x) {\n"
|
|
|
|
" struct AB *ab;\n"
|
|
|
|
" if (x == 0) {\n"
|
|
|
|
" ab = getab();\n"
|
|
|
|
" }\n"
|
|
|
|
" if (x == 0 && (ab != NULL || ab->a == 0)) { }\n"
|
|
|
|
"}");
|
2013-12-04 20:32:20 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct A { int *x; };\n" // declarationId is 0 for "delete"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void foo(void *info, void*p);\n"
|
|
|
|
"void bar(void) {\n"
|
|
|
|
" struct A *delete = 0;\n"
|
|
|
|
" foo( info, NULL );\n"
|
|
|
|
"}");
|
2013-12-04 20:32:20 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2014-03-20 06:48:17 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct ABC { int a; int b; int c; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void foo(int x, const struct ABC *abc);\n"
|
|
|
|
"void bar(void) {\n"
|
|
|
|
" struct ABC abc;\n"
|
|
|
|
" foo(123, &abc);\n"
|
|
|
|
" return abc.b;\n"
|
|
|
|
"}");
|
2022-01-13 19:52:02 +01:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: abc.a\n"
|
|
|
|
"[test.cpp:5]: (error) Uninitialized struct member: abc.b\n"
|
|
|
|
"[test.cpp:5]: (error) Uninitialized struct member: abc.c\n",
|
|
|
|
"[test.cpp:6]: (error) Uninitialized struct member: abc.b\n",
|
|
|
|
errout.str());
|
2013-12-04 20:32:20 +01:00
|
|
|
|
2019-06-02 20:19:53 +02:00
|
|
|
checkUninitVar("struct ABC { int a; int b; int c; };\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" struct ABC abc;\n"
|
|
|
|
" dostuff((uint32_t *)&abc.a);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-17 11:50:31 +02:00
|
|
|
checkUninitVar("void f(void) {\n"
|
|
|
|
" struct tm t;\n"
|
|
|
|
" t.tm_year = 123;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2013-01-19 10:09:58 +01:00
|
|
|
// return
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" return ab.b;\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-20 17:54:32 +01:00
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
|
2013-01-19 10:09:58 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" return ab.a;\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-19 10:09:58 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-10 18:38:44 +02:00
|
|
|
checkUninitVar("struct S { int a; int b; };\n" // #8299
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct S s;\n"
|
|
|
|
" s.a = 0;\n"
|
|
|
|
" return s;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: s.b\n", errout.str());
|
|
|
|
|
2021-05-10 19:24:03 +02:00
|
|
|
checkUninitVar("struct S { int a; int b; };\n" // #9810
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct S s;\n"
|
|
|
|
" return s.a ? 1 : 2;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: s.a\n", errout.str());
|
|
|
|
|
2013-01-18 21:26:28 +01:00
|
|
|
// checkIfForWhileHead
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct FRED {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" int b;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct FRED fred;\n"
|
|
|
|
" fred.a = do_something();\n"
|
|
|
|
" if (fred.a == 0) { }\n"
|
|
|
|
"}\n", "test.c");
|
2013-01-18 21:26:28 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct FRED {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int a;\n"
|
|
|
|
" int b;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct FRED fred;\n"
|
|
|
|
" fred.a = do_something();\n"
|
|
|
|
" if (fred.b == 0) { }\n"
|
|
|
|
"}\n", "test.c", false);
|
2013-01-19 12:48:56 +01:00
|
|
|
ASSERT_EQUALS("[test.c:9]: (error) Uninitialized struct member: fred.b\n", errout.str());
|
2013-02-05 17:01:46 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct Fred { int a; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f() {\n"
|
|
|
|
" struct Fred fred;\n"
|
|
|
|
" if (fred.a==1) {}\n"
|
|
|
|
"}", "test.c");
|
2013-05-02 20:34:15 +02:00
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized struct member: fred.a\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct S { int n; int m; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct S s;\n"
|
|
|
|
" for (s.n = 0; s.n <= 10; s.n++) { }\n"
|
|
|
|
"}", "test.c");
|
2013-02-05 17:01:46 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-02-23 15:57:58 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void test2() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct { char type; } s_d;\n"
|
|
|
|
" if (foo(&s_d.type)){}\n"
|
|
|
|
"}");
|
2013-02-23 15:57:58 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-03-20 16:59:45 +01:00
|
|
|
|
2013-04-10 20:46:44 +02:00
|
|
|
// for
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (x) { clear(ab); z = ab.a; }\n"
|
|
|
|
"}");
|
2013-04-10 20:46:44 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (x) { ab.a = ab.a + 1; }\n"
|
|
|
|
"}");
|
2013-06-25 18:40:41 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
|
|
|
|
2017-06-30 13:41:19 +02:00
|
|
|
checkUninitVar("struct AB { int a; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (x) { init(&ab); z = ab.a; }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2013-03-20 16:59:45 +01:00
|
|
|
// address of member
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a[10]; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int *p = ab.a;\n"
|
|
|
|
"}");
|
2013-03-20 16:59:45 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2014-10-16 21:57:43 +02:00
|
|
|
|
2019-12-19 20:18:25 +01:00
|
|
|
// Reference
|
|
|
|
checkUninitVar("struct A { int x; };\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" struct A a;\n"
|
|
|
|
" int& x = a.x;\n"
|
|
|
|
" x = 0;\n"
|
|
|
|
" return a.x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2014-10-16 21:57:43 +02:00
|
|
|
// non static data-member initialization
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a=1; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int a = ab.a;\n"
|
|
|
|
" int b = ab.b;\n"
|
|
|
|
"}");
|
2014-10-16 21:57:43 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
|
2015-01-21 13:10:38 +01:00
|
|
|
|
|
|
|
// STL class member
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct A {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" std::map<int, int> m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}");
|
2015-01-21 13:10:38 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Unknown type (C++)
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct A {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" C m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}", "test.cpp");
|
2015-01-21 13:10:38 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Unknown type (C)
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct A {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" C m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}", "test.c");
|
2015-01-21 13:10:38 +01:00
|
|
|
ASSERT_EQUALS("[test.c:7]: (error) Uninitialized struct member: a.m\n", errout.str());
|
|
|
|
|
|
|
|
// Type with constructor
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("class C { C(); }\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"struct A {\n"
|
|
|
|
" C m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}");
|
2015-01-21 13:10:38 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-04-25 22:22:35 +02:00
|
|
|
|
|
|
|
// #11002
|
|
|
|
checkUninitVar("struct S { char *p; int len; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" S s;\n"
|
|
|
|
" s.p = nullptr;\n"
|
|
|
|
" char* q = (s).p;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-01-16 20:28:29 +01:00
|
|
|
}
|
2013-01-20 18:40:24 +01:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar2_while() {
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: int a;
|
|
|
|
|
2013-01-24 19:41:15 +01:00
|
|
|
// for, while
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" while (a) {\n"
|
|
|
|
" x = x + 1;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-06-25 18:40:41 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" do {\n"
|
|
|
|
" x = x + 1;\n"
|
|
|
|
" } while (a);\n"
|
|
|
|
"}");
|
2013-06-25 18:40:41 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
2013-02-12 15:59:23 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" for (int x = x; x < 10; x++) {}\n"
|
|
|
|
"}");
|
2013-12-23 12:27:00 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: struct Element{Element*Next();};
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" for (Element *ptr3 = ptr3->Next(); ptr3; ptr3 = ptr3->Next()) {}\n"
|
|
|
|
"}");
|
2013-12-23 12:27:00 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: ptr3\n", errout.str());
|
2014-09-12 08:19:00 +02:00
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: int a;
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" while (a) {\n"
|
|
|
|
" init(&x);\n"
|
|
|
|
" x++;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-06-27 18:28:00 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" while (a) {\n"
|
|
|
|
" if (b) x++;\n"
|
|
|
|
" else x = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-01-24 19:41:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" for (int i = 0; i < 10; i += x) {\n"
|
|
|
|
" x = y;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-01-24 19:41:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" for (int i = 0; i < 10; i += x) { }\n"
|
|
|
|
"}");
|
2013-01-24 19:41:15 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" for (i=0;i<9;++i)\n"
|
|
|
|
" if (foo()) return i;\n"
|
|
|
|
" return 9;\n"
|
|
|
|
"}");
|
2013-01-24 19:41:15 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" do {} while (!getvalue(&i));\n"
|
|
|
|
" i++;\n"
|
|
|
|
"}");
|
2013-12-13 13:27:01 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" while (a()) {\n" // <- condition must always be true or there will be problem
|
|
|
|
" if (b()) {\n"
|
|
|
|
" x = 1;\n"
|
|
|
|
" break;"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" return x;\n"
|
|
|
|
"}");
|
2013-01-20 18:40:24 +01:00
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
2013-01-24 19:41:15 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" while (a()) {\n"
|
|
|
|
" if (b() && (x=1)) {\n"
|
|
|
|
" return x;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
" return 0;\n"
|
|
|
|
"}");
|
2013-09-23 06:07:01 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: void do_something(int);
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" for (;;) {\n"
|
|
|
|
" int a = x+1;\n"
|
|
|
|
" do_something(a);\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-01-24 19:41:15 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB {int a; int b;};\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (true) {\n"
|
|
|
|
" int a = 1+ab.a;\n"
|
|
|
|
" do_something(a);\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n", "test.c");
|
2019-03-09 11:30:45 +01:00
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
2013-02-11 18:31:14 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(int i) {\n" // #4569 fp
|
2015-07-23 20:53:50 +02:00
|
|
|
" float *buffer;\n"
|
|
|
|
" if(i>10) buffer = f;\n"
|
|
|
|
" if(i>10) {\n"
|
|
|
|
" for (int i=0;i<10;i++)\n"
|
|
|
|
" buffer[i] = 0;\n" // <- fp
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-02-11 18:31:14 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-02-12 15:59:23 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(){\n" // #4519 - fp: inline assembler in loop
|
2015-07-23 20:53:50 +02:00
|
|
|
" int x;\n"
|
|
|
|
" for (int i = 0; i < 10; i++) {\n"
|
|
|
|
" asm(\"foo\");\n"
|
|
|
|
" if (x & 0xf1) { }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-02-12 15:59:23 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-03-22 08:10:46 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("static void f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct ABC *abc;\n"
|
|
|
|
" for (i = 0; i < 10; i++)\n"
|
|
|
|
" x += sizeof(*abc);\n"
|
|
|
|
"}");
|
2013-03-22 08:10:46 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-06-26 17:02:57 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(void) {\n" // #4879
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" while (x) {\n"
|
|
|
|
" for (i = 0; i < 5; i++)\n"
|
|
|
|
" a[i] = b[i];\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2013-06-26 17:02:57 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2014-04-10 15:59:37 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(void) {\n" // #5658
|
2015-07-23 20:53:50 +02:00
|
|
|
" struct Foo *foo;\n"
|
|
|
|
" while (true) {\n"
|
|
|
|
" foo = malloc(sizeof(*foo));\n"
|
|
|
|
" foo->x = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2014-04-10 15:59:37 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2014-04-21 10:32:29 +02:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f(void) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" int i;\n"
|
|
|
|
" while (x) {\n"
|
|
|
|
" for (i=0,y=i;;){}\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
2014-04-21 10:32:29 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2016-08-04 14:39:54 +02:00
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2018-01-02 23:19:33 +01:00
|
|
|
" char *p = (char *)malloc(256);\n"
|
|
|
|
" while(*p && *p == '_')\n"
|
|
|
|
" p++;\n"
|
|
|
|
"}");
|
2021-05-17 09:22:19 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: *p\n", errout.str());
|
2018-01-02 23:19:33 +01:00
|
|
|
|
2016-08-04 14:39:54 +02:00
|
|
|
// #6646 - init in for loop
|
|
|
|
checkUninitVar("void f() {\n" // No FP
|
|
|
|
" for (int i;;i++)\n"
|
|
|
|
" dostuff(&i);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: int a;
|
2016-08-04 14:39:54 +02:00
|
|
|
checkUninitVar("void f() {\n" // No FN
|
|
|
|
" for (int i;;i++)\n"
|
|
|
|
" a=i;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: i\n", errout.str());
|
2022-04-27 12:43:21 +02:00
|
|
|
|
|
|
|
checkUninitVar("namespace N {\n" // #7377
|
|
|
|
" template<typename T>\n"
|
|
|
|
" class C {};\n"
|
|
|
|
" using V = class C<void>;\n"
|
|
|
|
"}\n"
|
|
|
|
"int f() {\n"
|
|
|
|
" int r = 0;\n"
|
|
|
|
" for (int x; x < 4; x++)\n"
|
|
|
|
" r += x;\n"
|
|
|
|
" return r;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: x\n", errout.str());
|
2013-01-20 18:40:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar2_4494() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("namespace N1 {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" class Fred {\n"
|
|
|
|
" public:\n"
|
|
|
|
" static void f1(char *p) { *p = 0; }\n"
|
|
|
|
" };\n"
|
|
|
|
" void fa(void) { char *p; Fred::f1(p); }\n"
|
|
|
|
" void fb(void) { char *p; Fred::f2(p); }\n"
|
|
|
|
" void fc(void) { char *p; ::N1::Fred::f1(p); }\n"
|
|
|
|
" void fd(void) { char *p; ::N1::Fred::f2(p); }\n"
|
|
|
|
"}\n"
|
|
|
|
"namespace N2 {\n"
|
|
|
|
" static void f1(char *p) { *p = 0; }\n"
|
|
|
|
" void fa(void) { char *p; f1(p); }\n"
|
|
|
|
" void fb(void) { char *p; f2(p); }\n"
|
|
|
|
" void fc(void) { char *p; N1::Fred::f1(p); }\n"
|
|
|
|
" void fd(void) { char *p; N1::Fred::f2(p); }\n"
|
|
|
|
" void fe(void) { char *p; ::N1::Fred::f1(p); }\n"
|
|
|
|
" void ff(void) { char *p; ::N1::Fred::f2(p); }\n"
|
|
|
|
" void fg(void) { char *p; Foo::f1(p); }\n"
|
|
|
|
" void fh(void) { char *p; Foo::f2(p); }\n"
|
|
|
|
"}");
|
2013-01-22 06:55:25 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: p\n"
|
|
|
|
"[test.cpp:8]: (error) Uninitialized variable: p\n"
|
|
|
|
"[test.cpp:13]: (error) Uninitialized variable: p\n"
|
|
|
|
"[test.cpp:15]: (error) Uninitialized variable: p\n"
|
|
|
|
"[test.cpp:17]: (error) Uninitialized variable: p\n", errout.str());
|
2013-01-23 16:53:55 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("class Fred {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"public:\n"
|
|
|
|
" void f1(char *p) { *p = 0; }\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred fred;\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" char *p;\n"
|
|
|
|
" fred.f1(p);\n"
|
|
|
|
"}");
|
2013-01-23 16:53:55 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: p\n", errout.str());
|
2013-01-25 06:49:04 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("class Fred {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"public:\n"
|
|
|
|
" class Wilma {\n"
|
|
|
|
" public:\n"
|
|
|
|
" class Barney {\n"
|
|
|
|
" public:\n"
|
|
|
|
" class Betty {\n"
|
|
|
|
" public:\n"
|
|
|
|
" void f1(char *p) { *p = 0; }\n"
|
|
|
|
" };\n"
|
|
|
|
" Betty betty;\n"
|
|
|
|
" };\n"
|
|
|
|
" Barney barney;\n"
|
|
|
|
" };\n"
|
|
|
|
" Wilma wilma;\n"
|
|
|
|
"};\n"
|
|
|
|
"Fred fred;\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" char *p;\n"
|
|
|
|
" fred.wilma.barney.betty.f1(p);\n"
|
|
|
|
"}");
|
2013-01-25 06:49:04 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:20]: (error) Uninitialized variable: p\n", errout.str());
|
2013-01-22 06:55:25 +01:00
|
|
|
}
|
2013-11-06 17:53:09 +01:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void uninitvar2_malloc() {
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f() {\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" int *p = (int*)malloc(40);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" return *p;\n"
|
|
|
|
"}");
|
2013-12-04 20:32:20 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int *p = (int*)malloc(40);\n"
|
|
|
|
" int var = *p;\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"}");
|
2013-12-04 20:32:20 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"int f() {\n"
|
2021-05-13 20:21:02 +02:00
|
|
|
" struct AB *ab = (AB*)malloc(sizeof(struct AB));\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" return ab->a;\n"
|
|
|
|
"}");
|
2014-09-14 10:29:12 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: ab\n"
|
|
|
|
"[test.cpp:4]: (error) Uninitialized struct member: ab.a\n",
|
|
|
|
errout.str());
|
2013-12-09 15:58:42 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct t_udf_file { int dir_left; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct t_udf_file *newf;\n"
|
|
|
|
" newf = malloc(sizeof(*newf));\n"
|
2021-05-21 17:10:49 +02:00
|
|
|
" if (!newf) {};\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"}");
|
2013-12-12 09:17:41 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char *s = malloc(100);\n"
|
|
|
|
" if (s != NULL) { }\n"
|
|
|
|
"}");
|
2013-12-12 11:44:07 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" p || assert_failed();\n"
|
|
|
|
"}");
|
2013-12-24 12:48:17 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" x = p;\n"
|
|
|
|
"}");
|
2013-12-25 14:00:27 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-21 17:10:49 +02:00
|
|
|
checkUninitVar("int* f() {\n"
|
|
|
|
" int *p = (int*)malloc(40);\n"
|
|
|
|
" return p;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2013-12-09 15:58:42 +01:00
|
|
|
// function parameter (treat it as initialized until malloc is used)
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("int f(int *p) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" if (*p == 1) {}\n" // no error
|
2021-05-13 20:21:02 +02:00
|
|
|
" p = (int*)malloc(256);\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" return *p;\n" // error
|
|
|
|
"}");
|
2013-12-09 15:58:42 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Memory is allocated but not initialized: p\n", errout.str());
|
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"int f(struct AB *ab) {\n"
|
|
|
|
" if (ab->a == 1) {}\n" // no error
|
2021-05-13 20:21:02 +02:00
|
|
|
" ab = (AB*)malloc(sizeof(struct AB));\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" return ab->a;\n" // error
|
|
|
|
"}");
|
2013-12-09 15:58:42 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
2013-12-11 05:54:42 +01:00
|
|
|
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
"void do_something(struct AB *ab);\n" // unknown function
|
2021-05-13 20:21:02 +02:00
|
|
|
"void f() {\n"
|
|
|
|
" struct AB *ab = (AB*)malloc(sizeof(struct AB));\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" do_something(ab);\n"
|
|
|
|
"}");
|
2013-12-12 08:44:00 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2013-12-12 09:17:41 +01:00
|
|
|
// analysis failed. varid 0.
|
2015-07-23 20:37:09 +02:00
|
|
|
checkUninitVar("void *vlc_custom_create (vlc_object_t *parent, size_t length, const char *typename) {\n"
|
2015-07-23 20:53:50 +02:00
|
|
|
" assert (length >= sizeof (vlc_object_t));\n"
|
|
|
|
"}");
|
2013-12-11 05:54:42 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2013-12-04 20:32:20 +01:00
|
|
|
}
|
|
|
|
|
2015-07-24 06:06:31 +02:00
|
|
|
void uninitvar_ternaryexpression() { // #4683
|
|
|
|
checkUninitVar("struct B { int asd; };\n"
|
|
|
|
"int f() {\n"
|
|
|
|
" int a=0;\n"
|
|
|
|
" struct B *b;\n"
|
|
|
|
" if (x) {\n"
|
|
|
|
" a = 1;\n"
|
|
|
|
" b = p;\n"
|
|
|
|
" }\n"
|
|
|
|
" return a ? b->asd : 0;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2021-05-17 13:31:16 +02:00
|
|
|
void uninitvar_rangeBasedFor() {
|
|
|
|
checkUninitVar("void function(Entry& entry) {\n" // #7078
|
2015-10-28 16:37:46 +01:00
|
|
|
" for (auto* expr : entry.exprs) {\n"
|
|
|
|
" expr->operate();\n"
|
|
|
|
" expr->operate();\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-17 13:31:16 +02:00
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int *item;\n"
|
|
|
|
" for (item: itemList) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-19 21:12:11 +02:00
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int buf[10];\n"
|
|
|
|
" for (int &i: buf) { i = 0; }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2015-10-28 16:37:46 +01:00
|
|
|
}
|
|
|
|
|
2018-09-22 16:52:34 +02:00
|
|
|
void uninitvar_static() { // #8734
|
|
|
|
checkUninitVar("struct X { "
|
|
|
|
" typedef struct { int p; } P_t; "
|
|
|
|
" static int arr[]; "
|
|
|
|
"}; "
|
|
|
|
"int X::arr[] = {42}; "
|
|
|
|
"void f() { "
|
|
|
|
" std::vector<X::P_t> result; "
|
|
|
|
" X::P_t P; "
|
|
|
|
" P.p = 0; "
|
|
|
|
" result.push_back(P); "
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2021-05-22 11:04:42 +02:00
|
|
|
void checkExpr() {
|
|
|
|
checkUninitVar("struct AB { int a; int b; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB *ab = (struct AB*)calloc(1, sizeof(*ab));\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2015-07-25 19:02:20 +02:00
|
|
|
void trac_4871() { // #4871
|
|
|
|
checkUninitVar("void pickup(int a) {\n"
|
|
|
|
"bool using_planner_action;\n"
|
|
|
|
"if (a) {\n"
|
|
|
|
" using_planner_action = false;\n"
|
|
|
|
"}\n"
|
|
|
|
"else {\n"
|
|
|
|
" try\n"
|
|
|
|
" {}\n"
|
|
|
|
" catch (std::exception &ex) {\n"
|
|
|
|
" return;\n"
|
|
|
|
" }\n"
|
|
|
|
" using_planner_action = true;\n"
|
|
|
|
"}\n"
|
|
|
|
"if (using_planner_action) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void syntax_error() { // Ticket #5073
|
2015-09-23 10:33:55 +02:00
|
|
|
const char code[] = "struct flex_array {};\n"
|
|
|
|
"struct cgroup_taskset {};\n"
|
|
|
|
"void cgroup_attach_task() {\n"
|
|
|
|
" struct flex_array *group;\n"
|
|
|
|
" struct cgroup_taskset tset = { };\n"
|
|
|
|
" do { } while_each_thread(leader, tsk);\n"
|
|
|
|
"}";
|
|
|
|
ASSERT_THROW(checkUninitVar(code), InternalError);
|
2013-11-06 17:53:09 +01:00
|
|
|
}
|
2014-03-19 21:08:48 +01:00
|
|
|
|
2017-06-01 22:21:02 +02:00
|
|
|
void trac_5970() { // Ticket #5970
|
2016-05-14 22:36:54 +02:00
|
|
|
checkUninitVar("void DES_ede3_ofb64_encrypt() {\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
" DES_cblock d;\n"
|
|
|
|
" char *dp;\n"
|
|
|
|
" dp=(char *)d;\n"
|
|
|
|
" init(dp);\n"
|
2016-05-14 22:36:54 +02:00
|
|
|
"}", "test.c");
|
2021-05-16 22:27:04 +02:00
|
|
|
// FP Unknown type ASSERT_EQUALS("", errout.str());
|
2016-05-14 22:36:54 +02:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
void valueFlowUninit_(const char* file, int line, const char code[], const char fname[] = "test.cpp")
|
2021-10-21 14:44:48 +02:00
|
|
|
{
|
2017-08-23 22:17:49 +02:00
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
settings.debugwarnings = false;
|
2021-02-24 22:00:06 +01:00
|
|
|
settings.certainty.disable(Certainty::experimental);
|
2017-08-23 22:17:49 +02:00
|
|
|
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
2021-11-29 07:34:39 +01:00
|
|
|
ASSERT_LOC(tokenizer.tokenize(istr, fname), file, line);
|
2017-08-23 22:17:49 +02:00
|
|
|
|
|
|
|
// Check for redundant code..
|
|
|
|
CheckUninitVar checkuninitvar(&tokenizer, &settings, this);
|
2021-11-29 07:34:39 +01:00
|
|
|
(checkuninitvar.valueFlowUninit)();
|
2017-08-23 22:17:49 +02:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
#define ctu(code) ctu_(__FILE__, __LINE__, code)
|
|
|
|
void valueFlowUninitTest() {
|
2021-10-31 07:14:00 +01:00
|
|
|
// #9735 - FN
|
2021-11-06 19:06:07 +01:00
|
|
|
valueFlowUninit("typedef struct\n"
|
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" unsigned int flag : 1;\n" // bit filed gets never initialized
|
|
|
|
"} status;\n"
|
|
|
|
"bool foo(const status * const s)\n"
|
|
|
|
"{\n"
|
|
|
|
" return s->flag;\n" // << uninitvar
|
|
|
|
"}\n"
|
|
|
|
"void bar(const status * const s)\n"
|
|
|
|
"{\n"
|
|
|
|
" if( foo(s) == 1) {;}\n"
|
|
|
|
"}\n"
|
|
|
|
"void f(void)\n"
|
|
|
|
"{\n"
|
|
|
|
" status s;\n"
|
|
|
|
" s.x = 42;\n"
|
|
|
|
" bar(&s);\n"
|
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (warning) Uninitialized variable: s->flag\n",
|
2021-11-06 19:06:07 +01:00
|
|
|
errout.str());
|
2021-10-31 07:14:00 +01:00
|
|
|
|
2021-10-20 20:51:59 +02:00
|
|
|
// Ticket #2207 - False negative
|
|
|
|
valueFlowUninit("void foo() {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" b = c - a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void foo() {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" b = a - c;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
// Ticket #6455 - some compilers allow const variables to be uninitialized
|
|
|
|
// extracttests.disable
|
|
|
|
valueFlowUninit("void foo() {\n"
|
|
|
|
" const int a;\n"
|
|
|
|
" b = c - a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
// extracttests.enable
|
|
|
|
|
|
|
|
valueFlowUninit("void foo() {\n"
|
|
|
|
" int *p;\n"
|
|
|
|
" realloc(p,10);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void foo() {\n" // #5240
|
|
|
|
" char *p = malloc(100);\n"
|
|
|
|
" char *tmp = realloc(p,1000);\n"
|
|
|
|
" if (!tmp) free(p);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void foo() {\n"
|
|
|
|
" int *p = NULL;\n"
|
|
|
|
" realloc(p,10);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2017-08-23 22:17:49 +02:00
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" switch (x) {}\n"
|
|
|
|
"}");
|
2017-04-23 18:05:14 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
2017-04-28 21:09:56 +02:00
|
|
|
|
2017-08-23 22:17:49 +02:00
|
|
|
valueFlowUninit("int f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" init(x);\n"
|
|
|
|
" return x;\n" // TODO: inconclusive ?
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n" // #8172
|
|
|
|
" char **x;\n"
|
|
|
|
" if (2 < sizeof(*x)) {}\n"
|
|
|
|
"}");
|
2017-04-28 21:09:56 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-24 08:01:55 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void foo() {\n" // #5259 - False negative
|
|
|
|
" int a;\n"
|
|
|
|
" int x[] = {a,2};\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2019-08-30 08:41:17 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int *y = &x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int *x;\n"
|
|
|
|
" int *&y = x;\n"
|
2021-05-21 07:28:34 +02:00
|
|
|
" y = nullptr;\n"
|
2019-08-30 08:41:17 +02:00
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int x = xyz::x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" extern int a;\n"
|
|
|
|
" a++;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("static void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int x, y;\n"
|
|
|
|
" x = (y = 10);\n"
|
|
|
|
" int z = y * 2;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("static void foo() {\n"
|
|
|
|
" int x, y;\n"
|
|
|
|
" x = ((y) = 10);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("static void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" Foo p;\n"
|
|
|
|
" p.abcd();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("static void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" Foo p;\n"
|
|
|
|
" int x = p.abcd();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-17 11:50:31 +02:00
|
|
|
// struct
|
2021-05-17 10:31:02 +02:00
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" AB ab;\n"
|
|
|
|
" AB *p = &ab;\n"
|
|
|
|
" p->a = 1;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-21 10:42:58 +02:00
|
|
|
valueFlowUninit("struct S {\n"
|
|
|
|
" S& rIo;\n"
|
|
|
|
" S(S&);\n"
|
|
|
|
" void Write();\n"
|
|
|
|
"};\n"
|
2021-05-22 09:44:18 +02:00
|
|
|
"void foo(bool b, struct S &io) {\n"
|
2021-05-21 10:42:58 +02:00
|
|
|
" S* p;\n"
|
|
|
|
" if (b)\n"
|
2021-05-22 09:44:18 +02:00
|
|
|
" p = new S(io);\n"
|
2021-05-21 10:42:58 +02:00
|
|
|
" p->Write();\n"
|
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:10]: (warning) Uninitialized variable: p.rIo\n", errout.str());
|
2021-05-21 10:42:58 +02:00
|
|
|
|
2019-08-30 08:41:17 +02:00
|
|
|
// Unknown types
|
|
|
|
{
|
|
|
|
valueFlowUninit("void a()\n"
|
|
|
|
"{\n"
|
|
|
|
" A ret;\n"
|
|
|
|
" return ret;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// #3916 - avoid false positive
|
|
|
|
valueFlowUninit("void f(float x) {\n"
|
|
|
|
" union lf { long l; float f; } u_lf;\n"
|
|
|
|
" float hx = (u_lf.f = (x), u_lf.l);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
valueFlowUninit("void a()\n"
|
|
|
|
"{\n"
|
|
|
|
" int x[10];\n"
|
|
|
|
" int *y = x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void a()\n"
|
|
|
|
"{\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int *y = &x;\n"
|
|
|
|
" *y = 0;\n"
|
|
|
|
" x++;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void a()\n"
|
|
|
|
"{\n"
|
|
|
|
" char x[10], y[10];\n"
|
|
|
|
" char *z = x;\n"
|
|
|
|
" memset(z, 0, sizeof(x));\n"
|
|
|
|
" memcpy(y, x, sizeof(x));\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Handling >> and <<
|
|
|
|
{
|
|
|
|
valueFlowUninit("int a() {\n"
|
|
|
|
" int ret;\n"
|
|
|
|
" std::cin >> ret;\n"
|
|
|
|
" ret++;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f(int b) {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" std::cin >> b >> a;\n"
|
|
|
|
" return a;"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void foo() {\n" // #3707
|
|
|
|
" Node node;\n"
|
|
|
|
" int x;\n"
|
|
|
|
" node[\"abcd\"] >> x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-23 18:43:34 +02:00
|
|
|
valueFlowUninit("int a(FArchive &arc) {\n" // #3060 (initialization through operator<<)
|
2019-08-30 08:41:17 +02:00
|
|
|
" int *p;\n"
|
2021-05-24 13:17:50 +02:00
|
|
|
" arc << p;\n" // <- TODO initialization?
|
|
|
|
" return *p;\n"
|
2019-08-30 08:41:17 +02:00
|
|
|
"}");
|
2021-10-04 07:50:23 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
|
2019-08-30 08:41:17 +02:00
|
|
|
|
|
|
|
// #4320
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" a << 1;\n"
|
|
|
|
" return a;\n"
|
|
|
|
"}");
|
2021-10-04 07:50:23 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2019-08-30 08:41:17 +02:00
|
|
|
|
2020-06-06 15:24:01 +02:00
|
|
|
// #9750
|
|
|
|
valueFlowUninit("struct S {\n"
|
|
|
|
" int one;\n"
|
|
|
|
" int two;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void test(std::istringstream& in) {\n"
|
|
|
|
" S p;\n"
|
|
|
|
" in >> p.one >> p.two;\n"
|
2019-11-11 15:00:32 +01:00
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-30 08:41:17 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 15:24:01 +02:00
|
|
|
valueFlowUninit("struct S { int x; };\n" // #9417
|
|
|
|
"void f() {\n"
|
|
|
|
" S s;\n"
|
|
|
|
" return s(1);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-08-30 08:41:17 +02:00
|
|
|
valueFlowUninit("void a() {\n" // asm
|
|
|
|
" int x;\n"
|
|
|
|
" asm();\n"
|
|
|
|
" x++;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void a()\n"
|
|
|
|
"{\n"
|
|
|
|
" int x[10];\n"
|
|
|
|
" struct xyz xyz1 = { .x = x };\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" char *buf = malloc(100);\n"
|
|
|
|
" struct ABC *abc = buf;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("class Fred {\n"
|
|
|
|
"public:\n"
|
|
|
|
" FILE *f;\n"
|
|
|
|
" ~Fred();\n"
|
|
|
|
"}\n"
|
|
|
|
"Fred::~Fred()\n"
|
|
|
|
"{\n"
|
|
|
|
" fclose(f);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int c;\n"
|
|
|
|
" ab(sizeof(xyz), &c);\n"
|
|
|
|
" if (c);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f()\n"
|
|
|
|
"{\n"
|
|
|
|
" int c;\n"
|
|
|
|
" a = (f2(&c));\n"
|
|
|
|
" c++;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// goto/setjmp/longjmp..
|
|
|
|
valueFlowUninit("void foo(int x)\n"
|
|
|
|
"{\n"
|
|
|
|
" long b;\n"
|
|
|
|
" if (g()) {\n"
|
|
|
|
" b =2;\n"
|
|
|
|
" goto found;\n"
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return;\n"
|
|
|
|
"\n"
|
|
|
|
"found:\n"
|
|
|
|
" int a = b;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("int foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" jmp_buf env;\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int val = setjmp(env);\n"
|
|
|
|
" if(val)\n"
|
|
|
|
" return a;\n"
|
|
|
|
" a = 1;\n"
|
|
|
|
" longjmp(env, 1);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-25 20:37:22 +02:00
|
|
|
// range for..
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" X *item;\n"
|
|
|
|
" for (item: itemList) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-26 04:52:55 +02:00
|
|
|
valueFlowUninit("X f() {\n"
|
|
|
|
" if (!itemList.empty()) {\n"
|
|
|
|
" X* item;\n"
|
|
|
|
" for(item: itemList) {}\n"
|
|
|
|
" return *item;\n"
|
|
|
|
" }\n"
|
|
|
|
" return X{};\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-08-30 08:41:17 +02:00
|
|
|
// macro_for..
|
|
|
|
valueFlowUninit("int foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int retval;\n"
|
|
|
|
" if (condition) {\n"
|
|
|
|
" for12(1,2) { }\n"
|
|
|
|
" retval = 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" else\n"
|
|
|
|
" retval = 2;\n"
|
|
|
|
" return retval;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-23 18:43:34 +02:00
|
|
|
valueFlowUninit("void foo(struct qb_list_head *list) {\n"
|
|
|
|
" struct qb_list_head *iter;\n"
|
|
|
|
" qb_list_for_each(iter, list) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-05-24 15:54:09 +02:00
|
|
|
valueFlowUninit("void json_parse_nat_type_flags(json_t *root) {\n"
|
|
|
|
" int index;\n"
|
|
|
|
" json_array_foreach(root, index, value) {}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-08-30 08:41:17 +02:00
|
|
|
valueFlowUninit("int foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" goto exit;\n"
|
|
|
|
" i++;\n"
|
|
|
|
"exit:\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("int foo() {\n"
|
|
|
|
" int x,y=0;\n"
|
|
|
|
"again:\n"
|
|
|
|
" if (y) return x;\n"
|
|
|
|
" x = a;\n"
|
|
|
|
" y = 1;\n"
|
|
|
|
" goto again;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// #4040 - False positive
|
|
|
|
valueFlowUninit("int f(int x) {\n"
|
|
|
|
" int iter;\n"
|
|
|
|
" {\n"
|
|
|
|
" union\n"
|
|
|
|
" {\n"
|
|
|
|
" int asInt;\n"
|
|
|
|
" double asDouble;\n"
|
|
|
|
" };\n"
|
|
|
|
"\n"
|
|
|
|
" iter = x;\n"
|
|
|
|
" }\n"
|
|
|
|
" return 1 + iter;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// C++11 style initialization
|
|
|
|
valueFlowUninit("int f() {\n"
|
|
|
|
" int i = 0;\n"
|
|
|
|
" int j{ i };\n"
|
|
|
|
" return j;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Ticket #5646
|
|
|
|
valueFlowUninit("float foo() {\n"
|
|
|
|
" float source[2] = {3.1, 3.1};\n"
|
|
|
|
" float (*sink)[2] = &source;\n"
|
|
|
|
" return (*sink)[0];\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-09-09 17:20:03 +02:00
|
|
|
|
|
|
|
// Ticket #8755
|
|
|
|
valueFlowUninit("void f(int b) {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" if (b == 10)\n"
|
|
|
|
" a = 1;\n"
|
|
|
|
" if (b == 13)\n"
|
|
|
|
" a = 1;\n"
|
|
|
|
" if (b == 'x') {}\n"
|
|
|
|
" if (a) {}\n"
|
|
|
|
"}");
|
2020-02-16 16:02:22 +01:00
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: a\n", "", errout.str());
|
2019-10-30 12:12:02 +01:00
|
|
|
|
2019-11-13 12:46:54 +01:00
|
|
|
valueFlowUninit("void h() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" int* v = &i;\n"
|
|
|
|
" sscanf(\"0\", \"%d\", v);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-11-13 12:46:54 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-10-30 12:12:02 +01:00
|
|
|
valueFlowUninit("void test(int p) {\n"
|
|
|
|
" int f;\n"
|
|
|
|
" if (p > 0)\n"
|
|
|
|
" f = 0;\n"
|
|
|
|
" if (p > 1)\n"
|
|
|
|
" f += 1;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-10-30 12:12:02 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2020-08-26 07:02:15 +02:00
|
|
|
|
|
|
|
valueFlowUninit("unsigned char get();\n"
|
|
|
|
"char f() {\n"
|
|
|
|
" unsigned char c;\n"
|
|
|
|
" do {\n"
|
|
|
|
" c = get();\n"
|
|
|
|
" } while (isalpha(c) == 0);\n"
|
|
|
|
" return static_cast<char>(c);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-01-18 10:12:07 +01:00
|
|
|
|
|
|
|
valueFlowUninit("void f(int x)\n"
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" char value;\n"
|
|
|
|
" for(i = 0; i < 1; i++) {\n"
|
|
|
|
" if(x > 1)\n"
|
|
|
|
" value = 0;\n"
|
|
|
|
" }\n"
|
|
|
|
" printf(\"\", value);\n"
|
|
|
|
"}\n");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:9]: (warning) Uninitialized variable: value\n", errout.str());
|
2021-01-18 10:12:07 +01:00
|
|
|
|
|
|
|
valueFlowUninit("void f(int x)\n"
|
|
|
|
"{\n"
|
|
|
|
" int i;\n"
|
|
|
|
" char value;\n"
|
|
|
|
" for(i = 0; i < 1; i++) {\n"
|
|
|
|
" if(x > 1)\n"
|
|
|
|
" value = 0;\n"
|
|
|
|
" else\n"
|
|
|
|
" value = 1;\n"
|
|
|
|
" }\n"
|
|
|
|
" printf(\"\", value);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-20 13:54:00 +02:00
|
|
|
|
|
|
|
// function pointers
|
|
|
|
valueFlowUninit("int f (const struct FileFuncDefs *ffd) {\n" // #10279
|
|
|
|
" int c;\n"
|
|
|
|
" (*ffd->zread)(&c, 1);\n"
|
|
|
|
" return c;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("int foo(unsigned int code) {\n" // #10279
|
|
|
|
" int res;\n\n"
|
|
|
|
" (* (utility_table[code])) (&res);\n"
|
|
|
|
" return (res);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-05-23 07:58:19 +02:00
|
|
|
|
|
|
|
valueFlowUninit("struct Archive {\n"
|
|
|
|
" bool isNull;\n"
|
|
|
|
" friend void operator&(const Archive &, bool &isNull);\n"
|
|
|
|
"};\n"
|
|
|
|
"void load(Archive& ar) {\n"
|
|
|
|
" bool isNull;\n"
|
|
|
|
" ar & isNull;\n"
|
|
|
|
" if (!isNull) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-06-19 13:58:18 +02:00
|
|
|
|
|
|
|
// #10119
|
|
|
|
valueFlowUninit("struct Foo {\n"
|
|
|
|
" int i{};\n"
|
|
|
|
" static const float cf;\n"
|
|
|
|
"};\n"
|
|
|
|
"const float Foo::cf = 0.1f;\n"
|
|
|
|
"int bar() {\n"
|
|
|
|
" Foo f;\n"
|
|
|
|
" return f.i;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-07-01 22:08:00 +02:00
|
|
|
|
2021-07-03 09:12:26 +02:00
|
|
|
// #10326
|
|
|
|
valueFlowUninit("void foo() {\n"
|
|
|
|
" int cnt;\n"
|
|
|
|
" do {\n"
|
|
|
|
" cnt = 32 ;\n"
|
|
|
|
" }\n"
|
|
|
|
" while ( 0 ) ;\n"
|
|
|
|
" if (cnt != 0) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-07-01 22:08:00 +02:00
|
|
|
// #10327 - avoid extra warnings for uninitialized variable
|
|
|
|
valueFlowUninit("void dowork( int me ) {\n"
|
|
|
|
" if ( me == 0 ) {}\n" // <- not uninitialized
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"int main() {\n"
|
|
|
|
" int me;\n"
|
|
|
|
" dowork(me);\n" // <- me is uninitialized
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: me\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("int foo() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int a = x;\n" // <- x is uninitialized
|
|
|
|
" return a;\n" // <- a has been initialized
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: x\n", errout.str());
|
2021-09-18 22:23:05 +02:00
|
|
|
|
|
|
|
// #10468
|
|
|
|
valueFlowUninit("uint32_t foo(uint32_t in) {\n"
|
|
|
|
" uint32_t out, mask = 0x7F;\n"
|
|
|
|
" while (mask ^ 0x7FFFFFFF)\n"
|
|
|
|
" out = in & ~mask;\n"
|
|
|
|
" return out;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-10-30 22:13:58 +02:00
|
|
|
|
|
|
|
// #6597
|
|
|
|
valueFlowUninit("int f(int b) {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" if (!b)\n"
|
|
|
|
" a = 1;\n"
|
|
|
|
" if (b)\n"
|
|
|
|
" return a;\n"
|
|
|
|
" else\n"
|
|
|
|
" return -1;\n"
|
|
|
|
"}\n");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (warning) Uninitialized variable: a\n", errout.str());
|
2021-12-17 21:51:47 +01:00
|
|
|
|
|
|
|
// #9772
|
|
|
|
valueFlowUninit("int func(void) {\n"
|
|
|
|
" int rez;\n"
|
|
|
|
" struct sccb* ccb;\n"
|
|
|
|
" do {\n"
|
|
|
|
" if ((ccb = calloc(1, sizeof(*ccb))) == NULL) {\n"
|
|
|
|
" rez = 1;\n"
|
|
|
|
" break;\n"
|
|
|
|
" }\n"
|
|
|
|
" rez = 0;\n"
|
|
|
|
" } while (0);\n"
|
|
|
|
" if (rez != 0)\n"
|
|
|
|
" free(ccb);\n"
|
|
|
|
" return rez;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2021-12-23 08:22:41 +01:00
|
|
|
|
|
|
|
// #10553
|
|
|
|
valueFlowUninit("struct CharDataOnly {\n"
|
|
|
|
" char data[100];\n"
|
|
|
|
"};\n"
|
|
|
|
"CharDataOnly f() {\n"
|
|
|
|
" CharDataOnly testData;\n"
|
|
|
|
" strcpy(testData.data, \"string smaller than size\");\n"
|
|
|
|
" return testData;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-03-30 15:49:45 +02:00
|
|
|
|
|
|
|
// #10089
|
|
|
|
valueFlowUninit("typedef union {\n"
|
|
|
|
" struct { int x; };\n"
|
|
|
|
" int v[1];\n"
|
|
|
|
"} U;\n"
|
|
|
|
"void init(int* d) {\n"
|
|
|
|
" *d = 42;\n"
|
|
|
|
"}\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" U u;\n"
|
|
|
|
" init(u.v);\n"
|
|
|
|
" printf(\"%d\\n\", u.x);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// #10280
|
|
|
|
valueFlowUninit("union U {\n"
|
|
|
|
" char c[2];\n"
|
|
|
|
" uint16_t u16;\n"
|
|
|
|
"};\n"
|
|
|
|
"uint16_t f(std::istream& is) {\n"
|
|
|
|
" U u;\n"
|
|
|
|
" is.read(u.c, 2);\n"
|
|
|
|
" return u.u16;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-04-11 07:23:44 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" char src, dest;\n"
|
|
|
|
" std::memcpy(&dest, &src, 1);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: &src\n", errout.str());
|
2022-04-21 19:29:38 +02:00
|
|
|
|
|
|
|
// #10988
|
|
|
|
valueFlowUninit("void f(const void* ptr, bool* result) {\n"
|
|
|
|
" int dummy;\n"
|
|
|
|
" *result = (&dummy < ptr);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" int x;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" A* p = &a;\n"
|
|
|
|
" p->x = 1;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" int x;\n"
|
|
|
|
"};\n"
|
|
|
|
"void g(const int&);\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" g(a.x);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: a.x\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" int x;\n"
|
|
|
|
"};\n"
|
|
|
|
"void g(const int&);\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" A* p = &a;\n"
|
|
|
|
" g(p->x);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:8]: (error) Uninitialized variable: p->x\n", errout.str());
|
2022-04-24 09:56:58 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" a++;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
2022-04-28 10:48:37 +02:00
|
|
|
|
|
|
|
// #11006
|
|
|
|
valueFlowUninit("int g(int);\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" int received[NSIG];\n"
|
|
|
|
" for (int sig = 0; sig < NSIG; sig++)\n"
|
|
|
|
" received[sig] = g(sig);\n"
|
|
|
|
" for (int sig = 0; sig < NSIG; sig++)\n"
|
|
|
|
" if (received[sig]) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-06-14 09:54:45 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void increment(int& i) { ++i; }\n" // #6475
|
|
|
|
"int f() {\n"
|
|
|
|
" int n;\n"
|
|
|
|
" increment(n);\n"
|
|
|
|
" return n;\n"
|
|
|
|
"}\n");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (warning) Uninitialized variable: i\n", errout.str());
|
2017-04-23 18:05:14 +02:00
|
|
|
}
|
|
|
|
|
2021-10-03 22:15:35 +02:00
|
|
|
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
|
|
|
|
valueFlowUninit("struct wcsstruct {\n"
|
|
|
|
" int *wcsprm;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void copy_wcs(wcsstruct *wcsin) {\n"
|
|
|
|
" wcsstruct *x;\n"
|
|
|
|
" memcpy(wcsin, x, sizeof(wcsstruct));\n" // <- warning
|
|
|
|
" x->wcsprm = NULL;\n" // <- no warning
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct wcsstruct {\n"
|
|
|
|
" int *wcsprm;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void copy_wcs(wcsstruct *wcsin) {\n"
|
|
|
|
" wcsstruct *x;\n"
|
|
|
|
" sizeof(x);\n"
|
|
|
|
" x->wcsprm = NULL;\n" // <- Warn
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct wcsstruct {\n"
|
|
|
|
" int *wcsprm;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void init_wcs(wcsstruct *x) { if (x->wcsprm != NULL); }\n" // <- no warning
|
|
|
|
"\n"
|
|
|
|
"void copy_wcs() {\n"
|
|
|
|
" wcsstruct *x;\n"
|
|
|
|
" x->wcsprm = NULL;\n" // <- warn here
|
|
|
|
" init_wcs(x);\n" // <- no warning
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: x\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
void uninitvar_ipa() {
|
|
|
|
// #8825
|
|
|
|
valueFlowUninit("typedef struct {\n"
|
|
|
|
" int flags;\n"
|
|
|
|
"} someType_t;\n"
|
|
|
|
"void bar(const someType_t * const p) {\n"
|
|
|
|
" if( (p->flags & 0xF000) == 0xF000){}\n"
|
|
|
|
"}\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" someType_t gVar;\n"
|
|
|
|
" bar(&gVar);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5]: (warning) Uninitialized variable: p->flags\n", errout.str());
|
2019-08-15 10:44:55 +02:00
|
|
|
|
2021-02-20 12:58:42 +01:00
|
|
|
valueFlowUninit("typedef struct\n"
|
2019-08-15 10:44:55 +02:00
|
|
|
"{\n"
|
|
|
|
" int flags[3];\n"
|
|
|
|
"} someType_t;\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" someType_t gVar;\n"
|
|
|
|
" if(gVar.flags[1] == 42){}\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2021-10-21 14:44:48 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: gVar.flags\n", errout.str());
|
2019-08-15 10:44:55 +02:00
|
|
|
|
2021-05-23 12:06:23 +02:00
|
|
|
valueFlowUninit("void foo() {\n" // #10293
|
|
|
|
" union {\n"
|
|
|
|
" struct hdr cm;\n"
|
|
|
|
" char control[123];\n"
|
|
|
|
" } u;\n"
|
|
|
|
" char *x = u.control;\n" // <- no error
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-08-23 06:23:21 +02:00
|
|
|
valueFlowUninit("struct pc_data {\n"
|
|
|
|
" struct {\n"
|
|
|
|
" char * strefa;\n"
|
|
|
|
" } wampiryzm;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct pc_data *pcdata;\n"
|
|
|
|
" if ( *pcdata->wampiryzm.strefa == '\\0' ) { }\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-23 06:23:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: pcdata\n", errout.str());
|
|
|
|
|
2019-09-03 06:46:34 +02:00
|
|
|
// # 9293
|
|
|
|
valueFlowUninit("struct S {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int y;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct S s1;\n"
|
|
|
|
" int * x = &s1.x;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
" struct S s2 = {*x, 0};\n"
|
|
|
|
"}");
|
2021-05-21 07:28:34 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:9]: (error) Uninitialized variable: *x\n", errout.str());
|
2019-09-03 06:46:34 +02:00
|
|
|
|
|
|
|
valueFlowUninit("struct S {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" int y;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct S s1;\n"
|
|
|
|
" struct S s2;\n"
|
|
|
|
" int * x = &s1.x;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
" s2.x = *x;\n"
|
|
|
|
"}");
|
2021-05-21 07:28:34 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:10]: (error) Uninitialized variable: *x\n", errout.str());
|
2019-09-03 06:46:34 +02:00
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
valueFlowUninit("void f(bool * x) {\n"
|
|
|
|
" *x = false;\n"
|
|
|
|
"}\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" bool b;\n"
|
|
|
|
" f(&b);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-15 10:44:55 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-08-17 07:36:41 +02:00
|
|
|
valueFlowUninit("void f(bool * x) {\n"
|
|
|
|
" if (x != nullptr)\n"
|
|
|
|
" x = 1;\n"
|
|
|
|
"}\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" bool x;\n"
|
|
|
|
" f(&x);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-17 07:36:41 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" bool b;\n"
|
|
|
|
" bool * x = &b;\n"
|
|
|
|
" if (x != nullptr)\n"
|
|
|
|
" x = 1;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-17 07:36:41 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-08-15 10:44:55 +02:00
|
|
|
valueFlowUninit("struct A { bool b; };"
|
|
|
|
"void f(A * x) {\n"
|
|
|
|
" x->b = false;\n"
|
|
|
|
"}\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" A b;\n"
|
|
|
|
" f(&b);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2021-05-21 07:28:34 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-15 10:44:55 +02:00
|
|
|
|
|
|
|
valueFlowUninit("std::string f() {\n"
|
|
|
|
" std::ostringstream ostr;\n"
|
|
|
|
" ostr << \"\";\n"
|
|
|
|
" return ostr.str();\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-15 10:44:55 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-19 09:09:07 +02:00
|
|
|
// #9281
|
|
|
|
valueFlowUninit("struct s {\n"
|
|
|
|
" char a[20];\n"
|
|
|
|
"};\n"
|
|
|
|
"void c(struct s *sarg) {\n"
|
|
|
|
" sarg->a[0] = '\\0';\n"
|
|
|
|
"}\n"
|
|
|
|
"void b(struct s *sarg) {\n"
|
|
|
|
" c(sarg);\n"
|
|
|
|
"}\n"
|
|
|
|
"void a() {\n"
|
|
|
|
" struct s s1;\n"
|
|
|
|
" b(&s1);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-19 09:09:07 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-24 11:27:47 +02:00
|
|
|
|
|
|
|
// # 9290
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" double x;\n"
|
|
|
|
"};\n"
|
|
|
|
"double b() {\n"
|
|
|
|
" A * c;\n"
|
|
|
|
" c->x = 42;\n"
|
|
|
|
" return c->x;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2021-10-03 18:47:21 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n",
|
2020-02-13 16:27:06 +01:00
|
|
|
errout.str());
|
2019-08-24 11:27:47 +02:00
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" double x;\n"
|
|
|
|
"};\n"
|
|
|
|
"double b() {\n"
|
|
|
|
" A c;\n"
|
|
|
|
" c.x = 42;\n"
|
2019-08-26 20:45:14 +02:00
|
|
|
" return c.x;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-24 11:27:47 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" double x;\n"
|
|
|
|
"};\n"
|
|
|
|
"double d(A * e) {\n"
|
|
|
|
" e->x = 42;\n"
|
|
|
|
" return e->x;\n"
|
|
|
|
"}\n"
|
|
|
|
"double b() {\n"
|
|
|
|
" A c;\n"
|
|
|
|
" return d(&c);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2021-05-21 07:28:34 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-29 08:38:50 +02:00
|
|
|
|
|
|
|
// # 9302
|
|
|
|
valueFlowUninit("struct VZ {\n"
|
|
|
|
" double typ;\n"
|
|
|
|
"};\n"
|
|
|
|
"void read() {\n"
|
|
|
|
" struct VZ vz;\n"
|
|
|
|
" struct VZ* pvz = &vz;\n"
|
|
|
|
" vz.typ = 42;\n"
|
|
|
|
" if (pvz->typ == 0)\n"
|
|
|
|
" return;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-29 08:38:50 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-30 18:32:45 +02:00
|
|
|
|
|
|
|
// # 9305
|
|
|
|
valueFlowUninit("struct kf {\n"
|
|
|
|
" double x;\n"
|
|
|
|
"};\n"
|
|
|
|
"void set(kf* k) {\n"
|
|
|
|
" k->x = 0;\n"
|
|
|
|
"}\n"
|
|
|
|
"void cal() {\n"
|
|
|
|
" KF b;\n"
|
|
|
|
" KF* pb = &b;\n"
|
|
|
|
" set( pb);\n"
|
|
|
|
" if (pb->x)\n"
|
|
|
|
" return;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-30 18:32:45 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-09-10 19:40:08 +02:00
|
|
|
|
|
|
|
// # 9348
|
|
|
|
valueFlowUninit("void f(int *a) {\n"
|
|
|
|
" int b = 0;\n"
|
|
|
|
" memcpy(a, &b, sizeof(b));\n"
|
|
|
|
"}\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" f(&i);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-09-10 19:40:08 +02:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2020-03-01 16:46:20 +01:00
|
|
|
|
|
|
|
// # 9631
|
|
|
|
valueFlowUninit("static void g(bool * result, int num, int num2, size_t * buflen) {\n"
|
|
|
|
" if (*result && *buflen >= 5) {}\n"
|
|
|
|
"}\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" size_t bytesCopied;\n"
|
|
|
|
" bool copied_all = true;\n"
|
|
|
|
" g(&copied_all, 5, 6, &bytesCopied);\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (warning) Uninitialized variable: *buflen\n", errout.str());
|
2021-05-21 07:28:34 +02:00
|
|
|
|
2021-11-07 06:51:19 +01:00
|
|
|
// # 9953
|
|
|
|
valueFlowUninit("uint32_t f(uint8_t *mem) {\n"
|
|
|
|
" uint32_t u32;\n"
|
|
|
|
" uint8_t *buf = (uint8_t *)(&u32);\n"
|
|
|
|
" buf[0] = mem[0];\n"
|
|
|
|
" return(*(uint32_t *)buf);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-15 10:44:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-21 14:44:48 +02:00
|
|
|
void valueFlowUninitStructMembers()
|
|
|
|
{
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 1;\n"
|
|
|
|
" if (ab.b == 2) {}\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: ab.b\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void do_something(const struct AB &ab) { a = ab.a; }\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2021-10-30 07:43:37 +02:00
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void do_something(const struct AB &ab) { a = ab.b; }\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}");
|
2022-06-28 22:52:35 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (warning) Uninitialized variable: ab.b\n", errout.str());
|
2021-10-30 07:43:37 +02:00
|
|
|
|
2021-10-21 14:44:48 +02:00
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int a = ab.a;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab.a\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" AB ab1;\n"
|
|
|
|
" AB ab2 = { ab1.a, 0 };\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ab1.a\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" buf[ab.a] = 0;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab.a\n", errout.str());
|
|
|
|
|
2021-10-30 07:43:37 +02:00
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 1;\n"
|
|
|
|
" x = ab;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized variable: ab.b\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 1;\n"
|
|
|
|
" x = *(&ab);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
2021-11-07 06:51:19 +01:00
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized variable: *(&ab).b\n", errout.str());
|
2021-10-30 07:43:37 +02:00
|
|
|
|
|
|
|
valueFlowUninit("void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int x;\n"
|
|
|
|
" ab.a = (void*)&x;\n"
|
|
|
|
" dostuff(&ab,0);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" static void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->f();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" static void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).f();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" static int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->v;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" static int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).v;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->f();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" void f() { }\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).f();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; element->v;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Element {\n"
|
|
|
|
" int v;\n"
|
|
|
|
"};\n"
|
|
|
|
"void test() {\n"
|
|
|
|
" Element *element; (*element).v;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: element\n", errout.str());
|
|
|
|
|
2021-10-21 14:44:48 +02:00
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n" // pass struct member by address
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" assign(&ab.a, 0);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit(
|
|
|
|
"struct Cstring { char *text; int size, alloc; };\n"
|
|
|
|
"int maybe();\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" Cstring res;\n"
|
|
|
|
" if ( ! maybe() ) return;\n" // <- fp goes away if this is removed
|
|
|
|
" ( ((res).text = (void*)0), ((res).size = (res).alloc = 0) );\n" // <- fp goes away if parentheses are removed
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void do_something(const struct AB ab);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" ab.b = 0;\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
{
|
|
|
|
valueFlowUninit("struct AB { char a[10]; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" strcpy(ab.a, STR);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { unsigned char a[10]; };\n" // #8999 - cast
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" strcpy((char *)ab.a, STR);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { char a[10]; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" strcpy(x, ab.a);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
TODO_ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: ab.a\n", "", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" dosomething(ab.a);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void do_something(const struct AB ab);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab = getAB();\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
{
|
|
|
|
// #6769 - calling method that might assign struct members
|
|
|
|
valueFlowUninit("struct AB { int a; int b; void set(); };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.set();\n"
|
|
|
|
" x = ab;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int get() const; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.get();\n"
|
|
|
|
" x = ab;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ab\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; void dostuff() {} };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.dostuff();\n"
|
|
|
|
" x = ab;\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("error", "", errout.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; struct { int b; int c; } s; };\n"
|
|
|
|
"void do_something(const struct AB ab);\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 1;\n"
|
|
|
|
" ab.s.b = 2;\n"
|
|
|
|
" ab.s.c = 3;\n"
|
|
|
|
" do_something(ab);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct conf {\n"
|
|
|
|
" char x;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void do_something(struct conf ant_conf);\n"
|
|
|
|
"\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct conf c;\n"
|
|
|
|
" initdata(&c);\n"
|
|
|
|
" do_something(c);\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct PIXEL {\n"
|
|
|
|
" union {\n"
|
|
|
|
" struct { unsigned char red,green,blue,alpha; };\n"
|
|
|
|
" unsigned int color;\n"
|
|
|
|
" };\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"unsigned char f() {\n"
|
|
|
|
" struct PIXEL p1;\n"
|
|
|
|
" p1.color = 255;\n"
|
|
|
|
" return p1.red;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"int f() {\n"
|
|
|
|
" struct AB *ab;\n"
|
|
|
|
" for (i = 1; i < 10; i++) {\n"
|
|
|
|
" if (condition && (ab = getab()) != NULL) {\n"
|
|
|
|
" a = ab->a;\n"
|
|
|
|
" }\n"
|
|
|
|
" }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"int f(int x) {\n"
|
|
|
|
" struct AB *ab;\n"
|
|
|
|
" if (x == 0) {\n"
|
|
|
|
" ab = getab();\n"
|
|
|
|
" }\n"
|
|
|
|
" if (x == 0 && (ab != NULL || ab->a == 0)) { }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct A { int *x; };\n" // declarationId is 0 for "delete"
|
|
|
|
"void foo(void *info, void*p);\n"
|
|
|
|
"void bar(void) {\n"
|
|
|
|
" struct A *delete = 0;\n"
|
|
|
|
" foo( info, NULL );\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct ABC { int a; int b; int c; };\n"
|
|
|
|
"void foo(int x, const struct ABC *abc);\n"
|
|
|
|
"void bar(void) {\n"
|
|
|
|
" struct ABC abc;\n"
|
|
|
|
" foo(123, &abc);\n"
|
|
|
|
" return abc.b;\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: abc.a\n"
|
|
|
|
"[test.cpp:5]: (error) Uninitialized variable: abc.b\n"
|
|
|
|
"[test.cpp:5]: (error) Uninitialized variable: abc.c\n",
|
|
|
|
"",
|
|
|
|
errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct ABC { int a; int b; int c; };\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" struct ABC abc;\n"
|
|
|
|
" dostuff((uint32_t *)&abc.a);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f(void) {\n"
|
|
|
|
" struct tm t;\n"
|
|
|
|
" t.tm_year = 123;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// return
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" return ab.b;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:5]: (error) Uninitialized variable: ab.b\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" ab.a = 0;\n"
|
|
|
|
" return ab.a;\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct S { int a; int b; };\n" // #8299
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct S s;\n"
|
|
|
|
" s.a = 0;\n"
|
|
|
|
" return s;\n"
|
|
|
|
"}\n");
|
2021-10-30 07:43:37 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: s.b\n", errout.str());
|
2021-10-21 14:44:48 +02:00
|
|
|
|
|
|
|
valueFlowUninit("struct S { int a; int b; };\n" // #9810
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct S s;\n"
|
|
|
|
" return s.a ? 1 : 2;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s.a\n", errout.str());
|
|
|
|
|
|
|
|
// checkIfForWhileHead
|
|
|
|
valueFlowUninit("struct FRED {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int b;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct FRED fred;\n"
|
|
|
|
" fred.a = do_something();\n"
|
|
|
|
" if (fred.a == 0) { }\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct FRED {\n"
|
|
|
|
" int a;\n"
|
|
|
|
" int b;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct FRED fred;\n"
|
|
|
|
" fred.a = do_something();\n"
|
|
|
|
" if (fred.b == 0) { }\n"
|
|
|
|
"}\n",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:9]: (error) Uninitialized variable: fred.b\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct Fred { int a; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct Fred fred;\n"
|
|
|
|
" if (fred.a==1) {}\n"
|
|
|
|
"}",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:4]: (error) Uninitialized variable: fred.a\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct S { int n; int m; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct S s;\n"
|
|
|
|
" for (s.n = 0; s.n <= 10; s.n++) { }\n"
|
|
|
|
"}",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void test2() {\n"
|
|
|
|
" struct { char type; } s_d;\n"
|
|
|
|
" if (foo(&s_d.type)){}\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// for
|
|
|
|
valueFlowUninit("struct AB { int a; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (x) { clear(ab); z = ab.a; }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (x) { ab.a = ab.a + 1; }\n"
|
|
|
|
"}");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ab.a\n", "", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct AB { int a; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" while (x) { init(&ab); z = ab.a; }\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// address of member
|
|
|
|
valueFlowUninit("struct AB { int a[10]; int b; };\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int *p = ab.a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Reference
|
|
|
|
valueFlowUninit("struct A { int x; };\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" struct A a;\n"
|
|
|
|
" int& x = a.x;\n"
|
|
|
|
" x = 0;\n"
|
|
|
|
" return a.x;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// non static data-member initialization
|
|
|
|
valueFlowUninit("struct AB { int a=1; int b; };\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" struct AB ab;\n"
|
|
|
|
" int a = ab.a;\n"
|
|
|
|
" int b = ab.b;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: ab.a\n", errout.str());
|
|
|
|
|
|
|
|
// STL class member
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" std::map<int, int> m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Unknown type (C++)
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" C m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}",
|
|
|
|
"test.cpp");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
// Unknown type (C)
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" C m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}",
|
|
|
|
"test.c");
|
|
|
|
ASSERT_EQUALS("[test.c:7]: (error) Uninitialized variable: a.m\n", errout.str());
|
|
|
|
|
|
|
|
// Type with constructor
|
|
|
|
valueFlowUninit("class C { C(); }\n"
|
|
|
|
"struct A {\n"
|
|
|
|
" C m;\n"
|
|
|
|
" int i;\n"
|
|
|
|
"};\n"
|
|
|
|
"void foo() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x = a.m;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-01-08 10:47:28 +01:00
|
|
|
|
|
|
|
valueFlowUninit("struct S {\n"
|
|
|
|
" int t[1];\n"
|
|
|
|
"};\n"
|
|
|
|
"int f(const S* ps) {\n"
|
|
|
|
" return ps->t[0];\n"
|
|
|
|
"}\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" S s;\n"
|
|
|
|
" s.t[0] = 1;\n"
|
|
|
|
" f(&s);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct X {\n"
|
|
|
|
" int a, b;\n"
|
|
|
|
"};\n"
|
|
|
|
"struct S {\n"
|
|
|
|
" X t;\n"
|
|
|
|
"};\n"
|
|
|
|
"int f(const S* ps) {\n"
|
|
|
|
" return ps->t.a;\n"
|
|
|
|
"}\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" S s;\n"
|
|
|
|
" s.t.a = 1;\n"
|
|
|
|
" f(&s);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-01-13 19:52:02 +01:00
|
|
|
|
|
|
|
valueFlowUninit("typedef struct { int a; int b; int c; } ABC;\n" // #5777
|
|
|
|
"void setabc(int x, const ABC* const abc) {\n"
|
|
|
|
" sum = abc->a + abc->b + abc->c;\n"
|
|
|
|
"}\n"
|
|
|
|
"void f(void) {\n"
|
|
|
|
" ABC abc;\n"
|
|
|
|
" abc.a = 1;\n"
|
|
|
|
" setabc(123, &abc);\n"
|
|
|
|
"}\n");
|
|
|
|
TODO_ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->b\n"
|
|
|
|
"[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->c\n",
|
2022-06-28 22:52:35 +02:00
|
|
|
"[test.cpp:8] -> [test.cpp:3]: (warning) Uninitialized variable: abc->b\n",
|
2022-01-13 19:52:02 +01:00
|
|
|
errout.str());
|
2022-06-24 13:55:04 +02:00
|
|
|
|
|
|
|
valueFlowUninit("struct S { int* p; };\n" // #10463
|
|
|
|
"void f(S* in) {\n"
|
|
|
|
" S* s;\n"
|
|
|
|
" memcpy(in, s, sizeof(S));\n"
|
|
|
|
" s->p = NULL;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: s\n",
|
|
|
|
errout.str());
|
2021-10-21 14:44:48 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 07:48:54 +02:00
|
|
|
void uninitvar_memberfunction() {
|
|
|
|
// # 8715
|
|
|
|
valueFlowUninit("struct C {\n"
|
|
|
|
" int x();\n"
|
|
|
|
"};\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" C *c;\n"
|
|
|
|
" if (c->x() == 4) {}\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
2019-08-16 07:48:54 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: c\n", errout.str());
|
2021-05-21 14:30:47 +02:00
|
|
|
|
2021-05-21 07:28:34 +02:00
|
|
|
valueFlowUninit("struct A { \n"
|
|
|
|
" int i; \n"
|
|
|
|
" void f();\n"
|
|
|
|
"};\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" a.f();\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-08-16 07:48:54 +02:00
|
|
|
}
|
|
|
|
|
Fix crash in CheckUninitVar (#2129)
http://cppcheck.osuosl.org:8000/ycmd
```
2019-08-30 23:21
ftp://ftp.se.debian.org/debian/pool/main/y/ycmd/ycmd_0+20181101+git600f54d.orig.tar.gz
cppcheck-options: -j1 --library=posix --library=gnu --library=qt --library=python --library=googletest --library=boost -D__GNUC__ --check-library --inconclusive --enable=style,information --platform=unix64 --template=daca2 -rp=temp temp
platform: Linux-4.15.0-58-generic-x86_64-with-Ubuntu-18.04-bionic
python: 2.7.15+
client-version: 1.1.31
cppcheck: head 1.88
count: Crash! 83
elapsed-time: -11.0 61.2
head results:
Checking temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc: __GNUC__=1...
Program received signal SIGSEGV, Segmentation fault.
CheckUninitVar::valueFlowUninit (this=this@entry=0x7fffffffb350) at build/checkuninitvar.cpp:2376
2376 if (!tok->variable())
#0 CheckUninitVar::valueFlowUninit (this=this@entry=0x7fffffffb350) at build/checkuninitvar.cpp:2376
#1 0x00005555556901ac in CheckUninitVar::runChecks (this=<optimized out>, tokenizer=0x7fffffffbcb0, settings=0x7fffffffcd10, errorLogger=<optimized out>) at lib/checkuninitvar.h:68
#2 0x00005555556a2210 in CppCheck::checkNormalTokens (this=this@entry=0x7fffffffcaf0, tokenizer=...) at build/cppcheck.cpp:730
#3 0x00005555556a6ee2 in CppCheck::checkFile (this=this@entry=0x7fffffffcaf0, filename="temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc", cfgname="", fileStream=...) at build/cppcheck.cpp:540
#4 0x00005555556aad4c in CppCheck::check (this=this@entry=0x7fffffffcaf0, path="temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc") at build/cppcheck.cpp:195
#5 0x00005555557ef167 in CppCheckExecutor::check_internal (this=this@entry=0x7fffffffd880, cppcheck=..., argv=argv@entry=0x7fffffffdc08) at cli/cppcheckexecutor.cpp:884
#6 0x00005555557efa9a in CppCheckExecutor::check (this=0x7fffffffd880, argc=17, argv=0x7fffffffdc08) at cli/cppcheckexecutor.cpp:198
#7 0x00005555555b1d1b in main (argc=17, argv=0x7fffffffdc08) at cli/main.cpp:95
```
2019-09-01 14:51:40 +02:00
|
|
|
void uninitvar_nonmember() {
|
2019-09-02 06:59:07 +02:00
|
|
|
valueFlowUninit("struct Foo {\n"
|
|
|
|
" int bar;\n"
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
|
|
|
"int main() {\n"
|
|
|
|
" Foo* foo;\n"
|
2021-05-22 09:44:18 +02:00
|
|
|
" foo->bar = 3;\n"
|
2021-02-20 12:58:42 +01:00
|
|
|
"}");
|
Fix crash in CheckUninitVar (#2129)
http://cppcheck.osuosl.org:8000/ycmd
```
2019-08-30 23:21
ftp://ftp.se.debian.org/debian/pool/main/y/ycmd/ycmd_0+20181101+git600f54d.orig.tar.gz
cppcheck-options: -j1 --library=posix --library=gnu --library=qt --library=python --library=googletest --library=boost -D__GNUC__ --check-library --inconclusive --enable=style,information --platform=unix64 --template=daca2 -rp=temp temp
platform: Linux-4.15.0-58-generic-x86_64-with-Ubuntu-18.04-bionic
python: 2.7.15+
client-version: 1.1.31
cppcheck: head 1.88
count: Crash! 83
elapsed-time: -11.0 61.2
head results:
Checking temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc: __GNUC__=1...
Program received signal SIGSEGV, Segmentation fault.
CheckUninitVar::valueFlowUninit (this=this@entry=0x7fffffffb350) at build/checkuninitvar.cpp:2376
2376 if (!tok->variable())
#0 CheckUninitVar::valueFlowUninit (this=this@entry=0x7fffffffb350) at build/checkuninitvar.cpp:2376
#1 0x00005555556901ac in CheckUninitVar::runChecks (this=<optimized out>, tokenizer=0x7fffffffbcb0, settings=0x7fffffffcd10, errorLogger=<optimized out>) at lib/checkuninitvar.h:68
#2 0x00005555556a2210 in CppCheck::checkNormalTokens (this=this@entry=0x7fffffffcaf0, tokenizer=...) at build/cppcheck.cpp:730
#3 0x00005555556a6ee2 in CppCheck::checkFile (this=this@entry=0x7fffffffcaf0, filename="temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc", cfgname="", fileStream=...) at build/cppcheck.cpp:540
#4 0x00005555556aad4c in CppCheck::check (this=this@entry=0x7fffffffcaf0, path="temp/ycmd-0+20181101+git600f54d/ycmd/tests/clang/testdata/completion_fixit.cc") at build/cppcheck.cpp:195
#5 0x00005555557ef167 in CppCheckExecutor::check_internal (this=this@entry=0x7fffffffd880, cppcheck=..., argv=argv@entry=0x7fffffffdc08) at cli/cppcheckexecutor.cpp:884
#6 0x00005555557efa9a in CppCheckExecutor::check (this=0x7fffffffd880, argc=17, argv=0x7fffffffdc08) at cli/cppcheckexecutor.cpp:198
#7 0x00005555555b1d1b in main (argc=17, argv=0x7fffffffdc08) at cli/main.cpp:95
```
2019-09-01 14:51:40 +02:00
|
|
|
ASSERT_EQUALS("[test.cpp:7]: (error) Uninitialized variable: foo\n", errout.str());
|
|
|
|
}
|
|
|
|
|
2016-08-02 14:27:51 +02:00
|
|
|
void isVariableUsageDeref() {
|
|
|
|
// *p
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char a[10];\n"
|
|
|
|
" char c = *a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
2021-05-13 20:21:02 +02:00
|
|
|
// extracttests.start: extern const int SIZE;
|
2016-08-02 14:27:51 +02:00
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char a[SIZE+10];\n"
|
|
|
|
" char c = *a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" char a[10];\n"
|
|
|
|
" *a += 10;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
|
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" int a[10][10];\n"
|
|
|
|
" dostuff(*a);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2019-07-24 18:20:23 +02:00
|
|
|
|
|
|
|
checkUninitVar("void f() {\n"
|
|
|
|
" void (*fp[1]) (void) = {function1};\n"
|
|
|
|
" (*fp[0])();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2016-08-02 14:27:51 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 06:35:44 +01:00
|
|
|
void isVariableUsageDerefValueflow()
|
|
|
|
{
|
|
|
|
// *p
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" char a[10];\n"
|
|
|
|
" char c = *a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: *a\n", errout.str());
|
|
|
|
|
|
|
|
// extracttests.start: extern const int SIZE;
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" char a[SIZE+10];\n"
|
|
|
|
" char c = *a;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: *a\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" char a[10];\n"
|
|
|
|
" *a += 10;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: *a\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" int a[10][10];\n"
|
|
|
|
" dostuff(*a);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("void f() {\n"
|
|
|
|
" void (*fp[1]) (void) = {function1};\n"
|
|
|
|
" (*fp[0])();\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-03-27 10:01:58 +02:00
|
|
|
|
2022-03-31 21:24:20 +02:00
|
|
|
valueFlowUninit("template <typename T, int value> T Get() {return value;}\n"
|
|
|
|
"char f() {\n"
|
|
|
|
" char buf[10];\n"
|
|
|
|
" for(int i = 0; i < Get<int,10>() ; ++i) \n"
|
|
|
|
" buf[i] = 0;\n"
|
|
|
|
" return buf[0];\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2022-03-27 10:01:58 +02:00
|
|
|
valueFlowUninit("static void Foo(double* p) {\n"
|
|
|
|
" p[0] = 0;\n"
|
|
|
|
" p[1] = 0;\n"
|
|
|
|
" p[2] = 0;\n"
|
|
|
|
" p[3] = 0;\n"
|
|
|
|
"}\n"
|
|
|
|
"double f() {\n"
|
|
|
|
" double L[2][2];\n"
|
|
|
|
" Foo(*L);\n"
|
|
|
|
" return L[0][0];\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-03-24 06:35:44 +01:00
|
|
|
}
|
|
|
|
|
2022-01-02 08:14:10 +01:00
|
|
|
void uninitvar_memberaccess() {
|
|
|
|
valueFlowUninit("struct foo{char *bar;};\n"
|
|
|
|
"void f(unsigned long long *p) {\n"
|
|
|
|
" foo a;\n"
|
|
|
|
" ((&a)->bar) = reinterpret_cast<char*>(*p);\n"
|
|
|
|
" if ((&a)->bar) ;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct foo{char *bar;};\n"
|
|
|
|
"void f(unsigned long long *p) {\n"
|
|
|
|
" foo a;\n"
|
|
|
|
" ((&(a))->bar) = reinterpret_cast<char*>(*p);\n"
|
|
|
|
" if ((&a)->bar) ;\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-01-25 12:14:20 +01:00
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n" // #10200
|
|
|
|
" struct B {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" };\n"
|
|
|
|
" int j;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f(std::vector<A::B>& x) {\n"
|
|
|
|
" A::B b;\n"
|
|
|
|
" b.i = 123;\n"
|
|
|
|
" x.push_back(b);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" struct B {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" };\n"
|
|
|
|
" int j;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f(std::vector<A::B>& x) {\n"
|
|
|
|
" A::B b;\n"
|
|
|
|
" x.push_back(b);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: b\n", errout.str());
|
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" struct B {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" };\n"
|
|
|
|
" int j;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f(std::vector<A>&x) {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" a.j = 123;\n"
|
|
|
|
" x.push_back(a);\n"
|
|
|
|
"}\n");
|
|
|
|
|
|
|
|
valueFlowUninit("struct A {\n"
|
|
|
|
" struct B {\n"
|
|
|
|
" int i;\n"
|
|
|
|
" };\n"
|
|
|
|
" int j;\n"
|
|
|
|
"};\n"
|
|
|
|
"void f(std::vector<A>& x) {\n"
|
|
|
|
" A a;\n"
|
|
|
|
" x.push_back(a);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:9]: (error) Uninitialized variable: a\n", errout.str());
|
2022-05-05 06:54:27 +02:00
|
|
|
|
|
|
|
valueFlowUninit("struct S { struct T { int* p; } t[2]; };\n" // #11018
|
|
|
|
"void f() {\n"
|
|
|
|
" S s;\n"
|
|
|
|
" *&s.t[0].p = 0;\n"
|
|
|
|
"}\n"
|
|
|
|
"void g() {\n"
|
|
|
|
" S s;\n"
|
|
|
|
" ((*&(*&s.t[0].p))) = 0;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-05-10 18:25:01 +02:00
|
|
|
|
|
|
|
valueFlowUninit("struct S { int i; };\n" // #6323
|
|
|
|
"void f() {\n"
|
|
|
|
" struct S s;\n"
|
|
|
|
" int x = -3;\n"
|
|
|
|
" int y = x < (1, s.i);\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: s.i\n", errout.str());
|
2022-01-02 08:14:10 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
void ctu_(const char* file, int line, const char code[]) {
|
2018-12-18 07:56:33 +01:00
|
|
|
// Clear the error buffer..
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
2021-11-29 07:34:39 +01:00
|
|
|
ASSERT_LOC(tokenizer.tokenize(istr, "test.cpp"), file, line);
|
2018-12-25 21:11:23 +01:00
|
|
|
|
|
|
|
CTU::FileInfo *ctu = CTU::getFileInfo(&tokenizer);
|
2018-12-18 07:56:33 +01:00
|
|
|
|
|
|
|
// Check code..
|
|
|
|
std::list<Check::FileInfo*> fileInfo;
|
|
|
|
CheckUninitVar check(&tokenizer, &settings, this);
|
|
|
|
fileInfo.push_back(check.getFileInfo(&tokenizer, &settings));
|
2018-12-25 21:11:23 +01:00
|
|
|
check.analyseWholeProgram(ctu, fileInfo, settings, *this);
|
2018-12-18 07:56:33 +01:00
|
|
|
while (!fileInfo.empty()) {
|
|
|
|
delete fileInfo.back();
|
|
|
|
fileInfo.pop_back();
|
|
|
|
}
|
2018-12-25 21:11:23 +01:00
|
|
|
delete ctu;
|
2018-12-18 07:56:33 +01:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:34:39 +01:00
|
|
|
void ctuTest() {
|
2018-12-18 07:56:33 +01:00
|
|
|
ctu("void f(int *p) {\n"
|
|
|
|
" a = *p;\n"
|
|
|
|
"}\n"
|
|
|
|
"int main() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" f(&x);\n"
|
|
|
|
"}");
|
2018-12-25 21:11:23 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (error) Using argument p that points at uninitialized variable x\n", errout.str());
|
2018-12-18 07:56:33 +01:00
|
|
|
|
|
|
|
ctu("void use(int *p) { a = *p + 3; }\n"
|
|
|
|
"void call(int x, int *p) { x++; use(p); }\n"
|
|
|
|
"int main() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" call(4,&x);\n"
|
|
|
|
"}");
|
2018-12-30 16:23:25 +01:00
|
|
|
ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:2] -> [test.cpp:1]: (error) Using argument p that points at uninitialized variable x\n", errout.str());
|
2018-12-18 07:56:33 +01:00
|
|
|
|
|
|
|
ctu("void dostuff(int *x, int *y) {\n"
|
|
|
|
" if (!var)\n"
|
|
|
|
" return -1;\n" // <- early return
|
|
|
|
" *x = *y;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" dostuff(a, &x);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
ctu("void dostuff(int *x, int *y) {\n"
|
|
|
|
" if (cond)\n"
|
|
|
|
" *y = -1;\n" // <- conditionally written
|
|
|
|
" *x = *y;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
|
|
|
"void f() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" dostuff(a, &x);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
2019-09-12 13:29:52 +02:00
|
|
|
ctu("void f(int *p) {\n"
|
|
|
|
" a = sizeof(*p);\n"
|
|
|
|
"}\n"
|
|
|
|
"int main() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" f(&x);\n"
|
|
|
|
"}");
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
2020-02-04 18:56:01 +01:00
|
|
|
|
|
|
|
ctu("void f(int *v) {\n"
|
|
|
|
" std::cin >> *v;\n"
|
|
|
|
"}\n"
|
|
|
|
"int main() {\n"
|
|
|
|
" int x;\n"
|
|
|
|
" f(&x);\n"
|
|
|
|
"}");
|
2022-01-13 19:52:02 +01:00
|
|
|
ASSERT_EQUALS("", errout.str());
|
2022-01-17 20:35:30 +01:00
|
|
|
|
|
|
|
ctu("void increment(int& i) { ++i; }\n" // #6475
|
|
|
|
"int f() {\n"
|
|
|
|
" int n;\n"
|
|
|
|
" increment(n);\n"
|
|
|
|
" return n;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Using argument i that points at uninitialized variable n\n", errout.str());
|
|
|
|
|
|
|
|
ctu("void increment(int* i) { ++(*i); }\n"
|
|
|
|
"int f() {\n"
|
|
|
|
" int n;\n"
|
|
|
|
" increment(&n);\n"
|
|
|
|
" return n;\n"
|
|
|
|
"}\n");
|
|
|
|
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Using argument i that points at uninitialized variable n\n", errout.str());
|
2018-12-18 07:56:33 +01:00
|
|
|
}
|
2010-10-31 12:31:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestUninitVar)
|