2010-10-31 11:51:25 +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 11:51:25 +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/>.
*/
2020-04-13 13:44:48 +02:00
# include "check.h"
2010-10-31 11:51:25 +01:00
# include "checknullpointer.h"
2020-04-13 13:44:48 +02:00
# include "ctu.h"
2022-01-27 19:03:20 +01:00
# include "errortypes.h"
2017-05-27 04:33:47 +02:00
# include "library.h"
# include "settings.h"
2010-10-31 11:51:25 +01:00
# include "testsuite.h"
2017-05-27 04:33:47 +02:00
# include "token.h"
# include "tokenize.h"
2022-01-27 19:03:20 +01:00
# include <iosfwd>
2017-05-27 04:33:47 +02:00
# include <list>
# include <map>
# include <string>
2022-01-27 19:03:20 +01:00
# include <unordered_map>
# include <utility>
2017-05-27 04:33:47 +02:00
# include <vector>
2010-10-31 11:51:25 +01:00
2022-01-27 19:03:20 +01:00
# include <simplecpp.h>
2011-10-13 20:53:06 +02:00
class TestNullPointer : public TestFixture {
2010-10-31 11:51:25 +01:00
public :
2021-08-07 20:51:18 +02:00
TestNullPointer ( ) : TestFixture ( " TestNullPointer " ) { }
2010-10-31 11:51:25 +01:00
private :
2014-03-24 09:59:05 +01:00
Settings settings ;
2010-10-31 11:51:25 +01:00
2022-02-10 23:02:24 +01:00
void run ( ) override {
2019-03-17 07:37:38 +01:00
LOAD_LIB_2 ( settings . library , " std.cfg " ) ;
2021-02-24 22:00:06 +01:00
settings . severity . enable ( Severity : : warning ) ;
2014-03-24 09:59:05 +01:00
2011-10-09 15:09:02 +02:00
TEST_CASE ( nullpointerAfterLoop ) ;
2010-10-31 11:51:25 +01:00
TEST_CASE ( nullpointer1 ) ;
TEST_CASE ( nullpointer2 ) ;
2010-11-04 18:18:19 +01:00
TEST_CASE ( structDerefAndCheck ) ; // dereferencing struct and then checking if it's null
TEST_CASE ( pointerDerefAndCheck ) ;
2010-10-31 11:51:25 +01:00
TEST_CASE ( nullpointer5 ) ; // References should not be checked
2010-12-26 13:38:16 +01:00
TEST_CASE ( nullpointerExecutionPaths ) ;
TEST_CASE ( nullpointerExecutionPathsLoop ) ;
2010-10-31 11:51:25 +01:00
TEST_CASE ( nullpointer7 ) ;
TEST_CASE ( nullpointer9 ) ;
2011-05-28 05:30:19 +02:00
TEST_CASE ( nullpointer10 ) ;
2011-06-04 04:16:08 +02:00
TEST_CASE ( nullpointer11 ) ; // ticket #2812
2011-10-16 12:54:58 +02:00
TEST_CASE ( nullpointer12 ) ; // ticket #2470
2012-01-28 08:06:03 +01:00
TEST_CASE ( nullpointer15 ) ; // #3560 (fp: return p ? f(*p) : f(0))
2012-02-13 19:46:45 +01:00
TEST_CASE ( nullpointer16 ) ; // #3591
2012-03-07 20:31:23 +01:00
TEST_CASE ( nullpointer17 ) ; // #3567
2012-04-01 15:56:48 +02:00
TEST_CASE ( nullpointer18 ) ; // #1927
2012-05-16 09:56:39 +02:00
TEST_CASE ( nullpointer19 ) ; // #3811
2012-05-31 18:41:00 +02:00
TEST_CASE ( nullpointer20 ) ; // #3807 (fp: return p ? (p->x() || p->y()) : z)
2012-09-23 09:20:16 +02:00
TEST_CASE ( nullpointer21 ) ; // #4038 (fp: if (x) p=q; else return;)
2013-03-19 08:47:05 +01:00
TEST_CASE ( nullpointer23 ) ; // #4665 (false positive)
2013-10-20 13:37:36 +02:00
TEST_CASE ( nullpointer24 ) ; // #5082 fp: chained assignment
2014-03-26 12:17:27 +01:00
TEST_CASE ( nullpointer25 ) ; // #5061
2014-03-27 19:16:30 +01:00
TEST_CASE ( nullpointer26 ) ; // #3589
2019-05-16 21:11:04 +02:00
TEST_CASE ( nullpointer27 ) ; // #6568
2015-08-15 00:31:26 +02:00
TEST_CASE ( nullpointer28 ) ; // #6491
2015-08-15 01:00:36 +02:00
TEST_CASE ( nullpointer30 ) ; // #6392
2018-04-09 16:13:17 +02:00
TEST_CASE ( nullpointer31 ) ; // #8482
2018-11-03 15:53:24 +01:00
TEST_CASE ( nullpointer32 ) ; // #8460
2019-08-08 07:46:47 +02:00
TEST_CASE ( nullpointer33 ) ;
TEST_CASE ( nullpointer34 ) ;
2019-08-11 15:39:37 +02:00
TEST_CASE ( nullpointer35 ) ;
2019-09-01 09:58:53 +02:00
TEST_CASE ( nullpointer36 ) ; // #9264
2019-09-20 15:06:37 +02:00
TEST_CASE ( nullpointer37 ) ; // #9315
TEST_CASE ( nullpointer38 ) ;
2019-09-30 21:04:43 +02:00
TEST_CASE ( nullpointer39 ) ; // #2153
TEST_CASE ( nullpointer40 ) ;
TEST_CASE ( nullpointer41 ) ;
TEST_CASE ( nullpointer42 ) ;
2019-11-08 08:11:41 +01:00
TEST_CASE ( nullpointer43 ) ; // #9404
2019-11-04 07:02:25 +01:00
TEST_CASE ( nullpointer44 ) ; // #9395, #9423
2019-11-08 08:11:41 +01:00
TEST_CASE ( nullpointer45 ) ;
2019-12-01 12:16:31 +01:00
TEST_CASE ( nullpointer46 ) ; // #9441
TEST_CASE ( nullpointer47 ) ; // #6850
2019-12-03 18:30:52 +01:00
TEST_CASE ( nullpointer48 ) ; // #9196
2020-01-05 16:25:33 +01:00
TEST_CASE ( nullpointer49 ) ; // #7804
TEST_CASE ( nullpointer50 ) ; // #6462
2020-02-13 16:27:06 +01:00
TEST_CASE ( nullpointer51 ) ;
2020-02-16 16:02:22 +01:00
TEST_CASE ( nullpointer52 ) ;
2020-03-01 16:46:20 +01:00
TEST_CASE ( nullpointer53 ) ; // #8005
2020-05-19 07:53:54 +02:00
TEST_CASE ( nullpointer54 ) ; // #9573
2020-05-21 08:47:48 +02:00
TEST_CASE ( nullpointer55 ) ; // #8144
2020-05-28 07:41:47 +02:00
TEST_CASE ( nullpointer56 ) ; // #9701
2020-06-13 10:26:54 +02:00
TEST_CASE ( nullpointer57 ) ; // #9751
2020-07-20 06:25:35 +02:00
TEST_CASE ( nullpointer58 ) ; // #9807
2020-09-14 09:17:29 +02:00
TEST_CASE ( nullpointer59 ) ; // #9897
2020-09-23 07:45:03 +02:00
TEST_CASE ( nullpointer60 ) ; // #9842
2020-11-10 16:00:55 +01:00
TEST_CASE ( nullpointer61 ) ;
TEST_CASE ( nullpointer62 ) ;
TEST_CASE ( nullpointer63 ) ;
TEST_CASE ( nullpointer64 ) ;
2020-11-20 09:36:09 +01:00
TEST_CASE ( nullpointer65 ) ; // #9980
2020-12-14 11:13:47 +01:00
TEST_CASE ( nullpointer66 ) ; // #10024
2020-12-27 10:14:46 +01:00
TEST_CASE ( nullpointer67 ) ; // #10062
2021-01-08 22:55:04 +01:00
TEST_CASE ( nullpointer68 ) ;
2021-08-22 20:17:18 +02:00
TEST_CASE ( nullpointer69 ) ; // #8143
2021-01-25 17:24:36 +01:00
TEST_CASE ( nullpointer70 ) ;
2021-02-17 12:09:11 +01:00
TEST_CASE ( nullpointer71 ) ; // #10178
2021-06-29 19:42:47 +02:00
TEST_CASE ( nullpointer72 ) ; // #10215
TEST_CASE ( nullpointer73 ) ; // #10321
2021-07-03 09:12:26 +02:00
TEST_CASE ( nullpointer74 ) ;
2021-07-18 07:46:31 +02:00
TEST_CASE ( nullpointer75 ) ;
2021-08-22 20:17:18 +02:00
TEST_CASE ( nullpointer76 ) ; // #10408
2021-08-21 18:52:11 +02:00
TEST_CASE ( nullpointer77 ) ;
2021-08-22 20:17:18 +02:00
TEST_CASE ( nullpointer78 ) ; // #7802
2021-08-25 20:33:41 +02:00
TEST_CASE ( nullpointer79 ) ; // #10400
2021-08-29 15:39:41 +02:00
TEST_CASE ( nullpointer80 ) ; // #10410
2021-09-04 08:45:55 +02:00
TEST_CASE ( nullpointer81 ) ; // #8724
2021-09-21 19:25:41 +02:00
TEST_CASE ( nullpointer82 ) ; // #10331
2021-10-05 08:29:23 +02:00
TEST_CASE ( nullpointer83 ) ; // #9870
2021-10-06 08:39:58 +02:00
TEST_CASE ( nullpointer84 ) ; // #9873
2021-10-15 10:57:40 +02:00
TEST_CASE ( nullpointer85 ) ; // #10210
2021-11-25 22:34:00 +01:00
TEST_CASE ( nullpointer86 ) ;
2021-12-02 17:28:31 +01:00
TEST_CASE ( nullpointer87 ) ; // #9291
TEST_CASE ( nullpointer88 ) ; // #9949
2021-12-06 20:06:48 +01:00
TEST_CASE ( nullpointer89 ) ; // #10640
2021-12-07 08:50:29 +01:00
TEST_CASE ( nullpointer90 ) ; // #6098
2022-01-04 21:19:45 +01:00
TEST_CASE ( nullpointer91 ) ; // #10678
2022-04-07 06:48:13 +02:00
TEST_CASE ( nullpointer92 ) ;
2022-04-10 08:10:11 +02:00
TEST_CASE ( nullpointer93 ) ; // #3929
2015-01-05 14:54:24 +01:00
TEST_CASE ( nullpointer_addressOf ) ; // address of
2014-03-28 10:06:24 +01:00
TEST_CASE ( nullpointerSwitch ) ; // #2626
2013-04-03 10:27:08 +02:00
TEST_CASE ( nullpointer_cast ) ; // #4692
2012-04-30 12:36:41 +02:00
TEST_CASE ( nullpointer_castToVoid ) ; // #3771
2019-08-05 16:26:32 +02:00
TEST_CASE ( nullpointer_subfunction ) ;
2011-10-12 22:11:27 +02:00
TEST_CASE ( pointerCheckAndDeRef ) ; // check if pointer is null and then dereference it
TEST_CASE ( nullConstantDereference ) ; // Dereference NULL constant
TEST_CASE ( gcc_statement_expression ) ; // Don't crash
2011-06-26 21:46:33 +02:00
TEST_CASE ( snprintf_with_zero_size ) ;
TEST_CASE ( snprintf_with_non_zero_size ) ;
2011-10-12 22:11:27 +02:00
TEST_CASE ( printf_with_invalid_va_argument ) ;
TEST_CASE ( scanf_with_invalid_va_argument ) ;
2011-10-23 23:36:57 +02:00
TEST_CASE ( nullpointer_in_return ) ;
2011-11-04 09:49:49 +01:00
TEST_CASE ( nullpointer_in_typeid ) ;
2011-11-07 21:10:21 +01:00
TEST_CASE ( nullpointer_in_for_loop ) ;
2011-11-13 22:35:13 +01:00
TEST_CASE ( nullpointerDelete ) ;
2019-09-20 15:06:37 +02:00
TEST_CASE ( nullpointerSubFunction ) ;
2011-11-23 21:19:54 +01:00
TEST_CASE ( nullpointerExit ) ;
2012-01-25 15:16:22 +01:00
TEST_CASE ( nullpointerStdString ) ;
2012-03-16 17:24:03 +01:00
TEST_CASE ( nullpointerStdStream ) ;
2019-04-26 11:30:09 +02:00
TEST_CASE ( nullpointerSmartPointer ) ;
2011-12-25 17:01:45 +01:00
TEST_CASE ( functioncall ) ;
2013-07-15 21:57:58 +02:00
TEST_CASE ( functioncalllibrary ) ; // use Library to parse function call
2012-11-21 08:56:17 +01:00
TEST_CASE ( functioncallDefaultArguments ) ;
2013-10-12 10:50:59 +02:00
TEST_CASE ( nullpointer_internal_error ) ; // #5080
2015-07-31 21:15:43 +02:00
TEST_CASE ( ticket6505 ) ;
2016-10-09 15:15:29 +02:00
TEST_CASE ( subtract ) ;
2018-04-03 21:32:37 +02:00
TEST_CASE ( addNull ) ;
2019-12-15 16:36:46 +01:00
TEST_CASE ( isPointerDeRefFunctionDecl ) ;
2018-12-18 07:56:33 +01:00
2021-11-29 07:34:39 +01:00
TEST_CASE ( ctuTest ) ;
2010-10-31 11:51:25 +01:00
}
2021-11-29 07:34:39 +01:00
# define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
void check_ ( const char * file , int line , const char code [ ] , bool inconclusive = false , const char filename [ ] = " test.cpp " ) {
2010-10-31 11:51:25 +01:00
// Clear the error buffer..
errout . str ( " " ) ;
2021-02-24 22:00:06 +01:00
settings . certainty . setEnabled ( Certainty : : inconclusive , inconclusive ) ;
2014-01-12 19:27:26 +01:00
2010-12-01 18:00:55 +01:00
// Tokenize..
Tokenizer tokenizer ( & settings , this ) ;
std : : istringstream istr ( code ) ;
2021-11-29 07:34:39 +01:00
ASSERT_LOC ( tokenizer . tokenize ( istr , filename ) , file , line ) ;
2010-12-01 18:00:55 +01:00
2017-05-15 20:05:11 +02:00
// Check for null pointer dereferences..
2016-10-09 15:15:29 +02:00
CheckNullPointer checkNullPointer ;
checkNullPointer . runChecks ( & tokenizer , & settings , this ) ;
2010-10-31 11:51:25 +01:00
}
2017-05-18 21:52:31 +02:00
void checkP ( const char code [ ] ) {
// Clear the error buffer..
errout . str ( " " ) ;
2021-02-24 22:00:06 +01:00
settings . certainty . setEnabled ( Certainty : : inconclusive , false ) ;
2017-05-18 21:52:31 +02:00
// Raw tokens..
2018-04-09 09:41:24 +02:00
std : : vector < std : : string > files ( 1 , " test.cpp " ) ;
2017-05-18 21:52:31 +02:00
std : : istringstream istr ( code ) ;
const simplecpp : : TokenList tokens1 ( istr , files , files [ 0 ] ) ;
// Preprocess..
simplecpp : : TokenList tokens2 ( files ) ;
std : : map < std : : string , simplecpp : : TokenList * > filedata ;
simplecpp : : preprocess ( tokens2 , tokens1 , files , filedata , simplecpp : : DUI ( ) ) ;
// Tokenizer..
Tokenizer tokenizer ( & settings , this ) ;
2020-05-16 18:44:17 +02:00
tokenizer . createTokens ( std : : move ( tokens2 ) ) ;
2017-05-18 21:52:31 +02:00
tokenizer . simplifyTokens1 ( " " ) ;
// Check for null pointer dereferences..
CheckNullPointer checkNullPointer ;
checkNullPointer . runChecks ( & tokenizer , & settings , this ) ;
}
2010-10-31 11:51:25 +01:00
2014-11-20 14:20:09 +01:00
void nullpointerAfterLoop ( ) {
2021-05-13 20:21:02 +02:00
// extracttests.start: struct Token { const Token *next() const; std::string str() const; };
check ( " void foo(const Token *tok) \n "
2010-10-31 11:51:25 +01:00
" { \n "
" while (tok); \n "
" tok = tok->next(); \n "
2014-09-04 17:52:14 +02:00
" } " , true ) ;
2020-05-21 08:47:48 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'tok' is redundant or there is possible null pointer dereference: tok. \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2011-03-28 19:27:07 +02:00
// #2681
2011-10-18 19:34:14 +02:00
{
const char code [ ] = " void foo(const Token *tok) \n "
" { \n "
" while (tok && tok->str() == \" = \" ) \n "
" tok = tok->next(); \n "
" \n "
" if (tok->str() != \" ; \" ) \n "
" ; \n "
" } \n " ;
2020-05-21 08:47:48 +02:00
check ( code ) ;
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:6]: (warning) Either the condition 'tok' is redundant or there is possible null pointer dereference: tok. \n " , errout . str ( ) ) ;
2011-10-18 19:34:14 +02:00
}
2011-03-28 19:27:07 +02:00
2010-10-31 11:51:25 +01:00
check ( " void foo() \n "
" { \n "
" for (const Token *tok = tokens; tok; tok = tok->next()) \n "
" { \n "
" while (tok && tok->str() != \" ; \" ) \n "
" tok = tok->next(); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2020-05-21 09:12:19 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:3]: (warning) Either the condition 'while' is redundant or there is possible null pointer dereference: tok. \n " , " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
check ( " void foo(Token &tok) \n "
" { \n "
" for (int i = 0; i < tok.size(); i++ ) \n "
" { \n "
" while (!tok) \n "
" char c = tok.read(); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" for (const Token *tok = tokens; tok; tok = tok->next()) \n "
" { \n "
" while (tok && tok->str() != \" ; \" ) \n "
" tok = tok->next(); \n "
" if( !tok ) break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" for (const Token *tok = tokens; tok; tok = tok ? tok->next() : NULL) \n "
" { \n "
" while (tok && tok->str() != \" ; \" ) \n "
" tok = tok->next(); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(A*a) \n "
" { \n "
" switch (a->b()) { \n "
" case 1: \n "
" while( a ){ \n "
" a = a->next; \n "
" } \n "
" break; \n "
" case 2: \n "
" a->b(); \n "
" break; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-09 15:09:02 +02:00
// dereference in outer scope..
2014-06-22 10:02:14 +02:00
check ( " void foo(int x, const Token *tok) { \n "
" if (x == 123) { \n "
" while (tok) tok = tok->next(); \n "
" } \n "
" tok->str(); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-06-29 18:55:59 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:5]: (warning) Either the condition 'tok' is redundant or there is possible null pointer dereference: tok. \n " , errout . str ( ) ) ;
2011-10-23 23:36:57 +02:00
check ( " int foo(const Token *tok) \n "
" { \n "
" while (tok){;} \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int foo(const Token *tok) \n "
" { \n "
" while (tok){;} \n "
" char a[2] = {0,0}; \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2020-05-21 08:47:48 +02:00
check ( " struct b { \n "
" b * c; \n "
" int i; \n "
" } \n "
" void a(b * e) { \n "
" for (b *d = e;d; d = d->c) \n "
" while (d && d->i == 0) \n "
" d = d->c; \n "
" if (!d) throw; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-05-21 08:47:48 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2020-09-03 07:17:36 +02:00
check ( " struct b { \n "
" b * c; \n "
" int i; \n "
" }; \n "
" void f(b* e1, b* e2) { \n "
" for (const b* d = e1; d != e2; d = d->c) { \n "
" if (d && d->i != 0) {} \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:7] -> [test.cpp:6]: (warning) Either the condition 'd' is redundant or there is possible null pointer dereference: d. \n " , errout . str ( ) ) ;
2011-10-09 15:09:02 +02:00
}
2014-11-20 14:20:09 +01:00
void nullpointer1 ( ) {
2010-10-31 11:51:25 +01:00
// ticket #1923 - no false positive when using else if
check ( " void f(A *a) \n "
" { \n "
" if (a->x == 1) \n "
" { \n "
" a = a->next; \n "
" } \n "
" else if (a->x == 2) { } \n "
" if (a) { } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// ticket #2134 - sizeof doesn't dereference
check ( " void f() { \n "
" int c = 1; \n "
" int *list = NULL; \n "
" sizeof(*list); \n "
" if (!list) \n "
" ; \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-29 20:19:31 +01:00
// ticket #2245 - sizeof doesn't dereference
check ( " void f(Bar *p) { \n "
" if (!p) { \n "
" int sz = sizeof(p->x); \n "
" } \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-11-29 20:19:31 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer2 ( ) {
2010-10-31 11:51:25 +01:00
// Null pointer dereference can only happen with pointers
check ( " void foo() \n "
" { \n "
" Fred fred; \n "
" while (fred); \n "
" fred.hello(); \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// Dereferencing a struct and then checking if it is null
// This is checked by this function:
// CheckOther::nullPointerStructByDeRefAndChec
2014-11-20 14:20:09 +01:00
void structDerefAndCheck ( ) {
2021-05-13 20:21:02 +02:00
// extracttests.start: struct ABC { int a; int b; int x; };
2010-10-31 11:51:25 +01:00
// errors..
check ( " void foo(struct ABC *abc) \n "
" { \n "
" int a = abc->a; \n "
" if (!abc) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition '!abc' is redundant or there is possible null pointer dereference: abc. \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2011-02-19 21:01:38 +01:00
check ( " void foo(struct ABC *abc) { \n "
2010-10-31 11:51:25 +01:00
" bar(abc->a); \n "
2014-05-22 19:48:00 +02:00
" bar(x, abc->a); \n "
" bar(x, y, abc->a); \n "
2010-10-31 11:51:25 +01:00
" if (!abc) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:2]: (warning) Either the condition '!abc' is redundant or there is possible null pointer dereference: abc. \n "
" [test.cpp:5] -> [test.cpp:3]: (warning) Either the condition '!abc' is redundant or there is possible null pointer dereference: abc. \n "
" [test.cpp:5] -> [test.cpp:4]: (warning) Either the condition '!abc' is redundant or there is possible null pointer dereference: abc. \n " , errout . str ( ) ) ;
2011-02-19 21:01:38 +01:00
2011-02-20 19:45:34 +01:00
check ( " void foo(ABC *abc) { \n "
" if (abc->a == 3) { \n "
" return; \n "
" } \n "
" if (abc) {} \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:5] -> [test.cpp:2]: (warning) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc. \n " ,
errout . str ( ) ) ;
2011-02-20 19:45:34 +01:00
2011-08-01 21:57:23 +02:00
check ( " void f(ABC *abc) { \n "
" if (abc->x == 0) { \n "
" return; \n "
" } \n "
" if (!abc); \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:5] -> [test.cpp:2]: (warning) Either the condition '!abc' is redundant or there is possible null pointer dereference: abc. \n " , errout . str ( ) ) ;
2011-08-01 21:57:23 +02:00
2011-02-19 21:01:38 +01:00
// TODO: False negative if member of member is dereferenced
check ( " void foo(ABC *abc) { \n "
" abc->next->a = 0; \n "
" if (abc->next) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:2]: (warning) Possible null pointer dereference: abc - otherwise it is redundant to check it against null. \n " , " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2011-02-19 21:10:31 +01:00
check ( " void foo(ABC *abc) { \n "
" abc->a = 0; \n "
" if (abc && abc->b == 0) \n "
" ; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc. \n " ,
errout . str ( ) ) ;
2011-02-19 21:10:31 +01:00
2010-10-31 11:51:25 +01:00
// ok dereferencing in a condition
check ( " void foo(struct ABC *abc) \n "
" { \n "
" if (abc && abc->a); \n "
" if (!abc) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-02 06:11:55 +01:00
check ( " void f(struct ABC *abc) { \n "
" int x = abc && a(abc->x); \n "
" if (abc) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
// ok to use a linked list..
check ( " void foo(struct ABC *abc) \n "
" { \n "
" abc = abc->next; \n "
" if (!abc) \n "
" ; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-08 19:30:14 +01:00
check ( " void f(struct ABC *abc) { \n "
" abc = (ABC *)(abc->_next); \n "
" if (abc) { } "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2011-12-08 19:30:14 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
// reassign struct..
check ( " void foo(struct ABC *abc) \n "
" { \n "
" int a = abc->a; \n "
" abc = abc->next; \n "
" if (!abc) \n "
" ; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(struct ABC *abc) \n "
" { \n "
" int a = abc->a; \n "
" f(&abc); \n "
" if (!abc) \n "
" ; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// goto..
check ( " void foo(struct ABC *abc) \n "
" { \n "
" int a; \n "
" if (!abc) \n "
" goto out; "
" a = abc->a; \n "
" return; \n "
" out: \n "
" if (!abc) \n "
" ; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// loops..
check ( " void foo(struct ABC *abc) \n "
" { \n "
" int a = abc->a; "
" do \n "
" { \n "
" if (abc) \n "
" abc = abc->next; \n "
" --a; \n "
" } \n "
" while (a > 0); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() \n "
" { \n "
" for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) \n "
" { \n "
" while (tok && tok->str() != \" { \" ) \n "
" tok = tok->next(); \n "
" if (!tok) \n "
" return; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// dynamic_cast..
check ( " void foo(ABC *abc) \n "
" { \n "
" int a = abc->a; \n "
" if (!dynamic_cast<DEF *>(abc)) \n "
" ; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-12 15:02:06 +01:00
// #2641 - global pointer, function call
check ( " ABC *abc; \n "
" void f() { \n "
" abc->a = 0; \n "
" do_stuff(); \n "
" if (abc) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-12 17:42:58 +01:00
2011-03-12 20:57:19 +01:00
check ( " Fred *fred; \n "
" void f() { \n "
" fred->foo(); \n "
" if (fred) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-12 17:42:58 +01:00
// #2641 - local pointer, function call
check ( " void f() { \n "
2017-04-24 18:27:16 +02:00
" ABC *abc = abc1; \n "
2011-03-12 17:42:58 +01:00
" abc->a = 0; \n "
" do_stuff(); \n "
" if (abc) { } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:5] -> [test.cpp:3]: (warning) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc. \n " ,
errout . str ( ) ) ;
2011-03-12 18:27:19 +01:00
// #2641 - local pointer, function call
check ( " void f(ABC *abc) { \n "
" abc->a = 0; \n "
" do_stuff(); \n "
" if (abc) { } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:4] -> [test.cpp:2]: (warning) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc. \n " ,
errout . str ( ) ) ;
2011-03-28 17:28:21 +02:00
// #2691 - switch/break
check ( " void f(ABC *abc) { \n "
" switch ( x ) { \n "
" case 14: \n "
" sprintf(buf, \" %d \" , abc->a); \n "
" break; \n "
" case 15: \n "
" if ( abc ) {} \n "
" break; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-07 21:08:21 +02:00
// #3128
check ( " void f(ABC *abc) { \n "
" x(!abc || y(abc->a)); \n "
" if (abc) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-30 17:59:38 +01:00
2013-10-22 10:47:48 +02:00
check ( " void f(ABC *abc) { \n "
" x(def || !abc || y(def, abc->a)); \n "
" if (abc) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(ABC *abc) { \n "
" x(abc && y(def, abc->a)); \n "
" if (abc) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(ABC *abc) { \n "
" x(def && abc && y(def, abc->a)); \n "
" if (abc) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-30 17:59:38 +01:00
// #3228 - calling function with null object
{
const char code [ ] = " void f(Fred *fred) { \n "
" fred->x(); \n "
" if (fred) { } \n "
" } " ;
check ( code ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'fred' is redundant or there is possible null pointer dereference: fred. \n " ,
errout . str ( ) ) ;
2011-10-30 17:59:38 +01:00
}
2011-12-18 13:33:23 +01:00
2014-01-03 18:53:37 +01:00
// #3425 - false positives when there are macros
2017-05-18 21:52:31 +02:00
checkP ( " #define IF if \n "
" void f(struct FRED *fred) { \n "
" fred->x = 0; \n "
" IF(!fred){} \n "
" } " ) ;
2011-12-18 13:33:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-10-20 20:57:16 +02:00
check ( " void foo() { \n "
" BUFFER *buffer = get_buffer(); \n "
" if (!buffer) \n "
" uv_fatal_error(); \n "
" buffer->x = 11; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
}
// Dereferencing a pointer and then checking if it is null
2014-11-20 14:20:09 +01:00
void pointerDerefAndCheck ( ) {
2021-05-13 20:21:02 +02:00
// extracttests.start: void bar(int);
2010-10-31 11:51:25 +01:00
// errors..
check ( " void foo(int *p) \n "
" { \n "
" *p = 0; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2011-08-02 11:20:09 +02:00
check ( " void foo(int *p) \n "
" { \n "
" *p = 0; \n "
" if (p) { } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2011-08-02 11:20:09 +02:00
2011-08-02 17:04:07 +02:00
check ( " void foo(int *p) \n "
" { \n "
" *p = 0; \n "
" if (p || q) { } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2011-08-02 17:04:07 +02:00
2010-10-31 11:51:25 +01:00
check ( " void foo(int *p) \n "
" { \n "
" bar(*p); \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2011-07-31 14:07:35 +02:00
check ( " void foo(char *p) \n "
" { \n "
" strcpy(p, \" abc \" ); \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-07-31 14:07:35 +02:00
2011-08-02 17:20:13 +02:00
check ( " void foo(char *p) \n "
" { \n "
" if (*p == 0) { } \n "
" if (!p) { } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-08-02 17:20:13 +02:00
2010-10-31 11:51:25 +01:00
// no error
check ( " void foo() \n "
" { \n "
" int *p; \n "
" f(&p); \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int **p = f(); \n "
" if (!p) \n "
" ; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int *p) \n "
" { \n "
" if (x) \n "
" p = 0; \n "
" else \n "
" *p = 0; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int x) \n "
" { \n "
" int a = 2 * x; "
" if (x == 0) \n "
" ; \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(int *p) \n "
" { \n "
" int var1 = p ? *p : 0; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2020-11-10 16:00:55 +01:00
check ( " void foo(int *p, bool x) \n "
2011-10-02 19:27:18 +02:00
" { \n "
" int var1 = x ? *p : 5; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-06-04 17:20:21 +02:00
ASSERT_EQUALS (
2020-11-10 16:00:55 +01:00
" [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2011-10-02 19:27:18 +02:00
2013-05-03 16:18:44 +02:00
// while
check ( " void f(int *p) { \n "
" *p = 0; \n "
" while (p) { p = 0; } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p) { \n "
" *p = 0; \n "
" while (p) { } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2013-05-03 16:18:44 +02:00
2011-10-02 19:27:18 +02:00
// Ticket #3125
check ( " void foo(ABC *p) \n "
" { \n "
" int var1 = p ? (p->a) : 0; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-10-02 19:27:18 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo(ABC *p) \n "
" { \n "
" int var1 = p ? (1 + p->a) : 0; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-10-02 19:27:18 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-01-21 19:38:59 +01:00
check ( " void f() { \n "
" int * a=0; \n "
" if (!a) {}; \n "
" int c = a ? 0 : 1; \n "
2015-07-25 14:17:55 +02:00
" } \n " , true ) ;
2013-01-21 19:38:59 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// #3686
check ( " void f() { \n "
" int * a=0; \n "
" if (!a) {}; \n "
" int c = a ? b : b+1; \n "
2015-07-25 14:17:55 +02:00
" } \n " , true ) ;
2013-01-21 19:38:59 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int * a=0; \n "
" if (!a) {}; \n "
" int c = (a) ? b : b+1; \n "
2015-07-25 14:17:55 +02:00
" } \n " , true ) ;
2013-01-21 19:38:59 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
check ( " void foo(P *p) \n "
" { \n "
" while (p) \n "
" if (p->check()) \n "
" break; \n "
" else \n "
" p = p->next(); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 19:48:58 +01:00
check ( " void f(Document *doc) { \n "
" int x = doc && doc->x; \n "
" if (!doc) { \n "
" return; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 18:18:19 +01:00
2011-10-07 21:08:21 +02:00
// #3128 - false positive
check ( " void f(int *p) { \n "
" assert(!p || (*p<=6)); \n "
" if (p) { *p = 0; } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p) { \n "
" assert(p && (*p<=6)); \n "
" if (p) { *p = 0; } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-08 12:58:10 +02:00
check ( " void f(int *p) { \n "
" *p = 12; \n "
" assert(p && (*p<=6)); \n "
" if (p) { *p = 0; } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:2]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2011-10-08 12:58:10 +02:00
2010-11-04 18:18:19 +01:00
check ( " void foo(x *p) \n "
" { \n "
" p = p->next; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-11-04 18:18:19 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-12-17 21:09:12 +01:00
check ( " void foo(x *p) \n "
" { \n "
" p = bar(p->next); \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-12-17 21:09:12 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-08 17:16:52 +01:00
2011-06-21 18:45:30 +02:00
check ( " void foo(x *p) \n "
" { \n "
" p = aa->bar(p->next); \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-06-21 18:45:30 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-05-01 07:06:14 +02:00
check ( " void foo(x *p) \n "
" { \n "
" p = *p2 = p->next; \n "
" if (!p) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2012-05-01 07:06:14 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-08 17:16:52 +01:00
check ( " void foo(struct ABC *abc) \n "
" { \n "
" abc = abc ? abc->next : 0; \n "
" if (!abc) \n "
" ; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-08 17:16:52 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-10 19:57:26 +01:00
2013-01-29 17:12:14 +01:00
check ( " void f(struct ABC *abc) { \n " // #4523
" abc = (*abc).next; \n "
" if (abc) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-01-30 16:52:12 +01:00
check ( " void f(struct ABC *abc) { \n " // #4523
" abc = (*abc->ptr); \n "
" if (abc) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-29 18:45:32 +02:00
check ( " int f(Item *item) { \n "
" x = item ? ab(item->x) : 0; \n "
" if (item) { } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-03-29 18:45:32 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f(Item *item) { \n "
" item->x = 0; \n "
" a = b ? c : d; \n "
" if (item) { } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:4] -> [test.cpp:2]: (warning) Either the condition 'item' is redundant or there is possible null pointer dereference: item. \n " ,
errout . str ( ) ) ;
2011-03-29 18:45:32 +02:00
2011-01-10 19:57:26 +01:00
check ( " BOOL GotoFlyAnchor() \n " // #2243
" { \n "
" const SwFrm* pFrm = GetCurrFrm(); \n "
" do { \n "
" pFrm = pFrm->GetUpper(); \n "
" } while( pFrm && !pFrm->IsFlyFrm() ); \n "
" \n "
" if( !pFrm ) \n "
" return FALSE; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-10 19:57:26 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-15 12:09:36 +01:00
// Ticket #2463
2012-12-06 19:19:22 +01:00
check ( " struct A \n "
2011-01-15 12:09:36 +01:00
" { \n "
" B* W; \n "
" \n "
" void f() { \n "
" switch (InData) { \n "
" case 2: \n "
" if (!W) return; \n "
" W->foo(); \n "
" break; \n "
" case 3: \n "
" f(); \n "
" if (!W) return; \n "
" break; \n "
" } \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-15 12:09:36 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-31 17:30:27 +01:00
// #2525 - sizeof
check ( " void f() { \n "
" int *test = NULL; \n "
" int c = sizeof(test[0]); \n "
" if (!test) \n "
" ; \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2011-01-31 17:30:27 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-20 09:47:55 +02:00
2013-11-02 23:56:10 +01:00
check ( " void f(type* p) { \n " // #4983
" x(sizeof p[0]); \n "
" if (!p) \n "
" ; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-11-02 23:56:10 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-08-20 09:47:55 +02:00
// #3023 - checked deref
check ( " void f(struct ABC *abc) { \n "
" WARN_ON(!abc || abc->x == 0); \n "
" if (!abc) { } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-08-20 09:47:55 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(struct ABC *abc) { \n "
" WARN_ON(!abc || abc->x == 7); \n "
" if (!abc) { } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-08-20 09:47:55 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-18 13:33:23 +01:00
2014-01-03 18:53:37 +01:00
// #3425 - false positives when there are macros
2017-05-18 21:52:31 +02:00
checkP ( " #define IF if \n "
" void f(int *p) { \n "
" *p = 0; \n "
" IF(!p){} \n "
" } " ) ;
2011-12-18 13:33:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-07-23 10:05:55 +02:00
check ( " void f() { \n " // #3914 - false positive
" int *p; \n "
" ((p=ret()) && (x=*p)); \n "
" if (p); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer5 ( ) {
2010-10-31 11:51:25 +01:00
// errors..
check ( " void foo(A &a) \n "
" { \n "
" char c = a.c(); \n "
" if (!a) \n "
" return; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
// Execution paths..
2014-11-20 14:20:09 +01:00
void nullpointerExecutionPaths ( ) {
2010-10-31 11:51:25 +01:00
// errors..
check ( " static void foo() \n "
" { \n "
" Foo *p = 0; \n "
2013-04-15 19:00:15 +02:00
" if (a == 1) { \n "
2010-10-31 11:51:25 +01:00
" p = new FooBar; \n "
2013-04-15 19:00:15 +02:00
" } else { if (a == 2) { \n "
" p = new FooCar; } } \n "
2010-10-31 11:51:25 +01:00
" p->abcd(); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-30 12:54:19 +01:00
TODO_ASSERT_EQUALS ( " [test.cpp:8]: (error) Possible null pointer dereference: p \n " ,
" " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2013-04-15 19:00:15 +02:00
check ( " static void foo() { \n "
2021-05-13 20:21:02 +02:00
" int &r = *(int*)0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-05-13 20:21:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference: (int*)0 \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2013-04-15 19:00:15 +02:00
check ( " static void foo(int x) { \n "
2021-05-13 20:21:02 +02:00
" int y = 5 + *(int*)0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-05-13 20:21:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference: (int*)0 \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2011-10-18 19:34:14 +02:00
{
2014-01-22 20:16:31 +01:00
const char code [ ] = " static void foo() { \n "
2011-10-18 19:34:14 +02:00
" Foo<int> *abc = 0; \n "
" abc->a(); \n "
" } \n " ;
2020-05-21 08:47:48 +02:00
check ( code ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: abc \n " , errout . str ( ) ) ;
2011-10-18 19:34:14 +02:00
}
2010-10-31 11:51:25 +01:00
2013-04-15 19:00:15 +02:00
check ( " static void foo() { \n "
2021-05-13 20:21:02 +02:00
" std::cout << *(int*)0; "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-05-13 20:21:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference: (int*)0 \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
check ( " void f() \n "
" { \n "
" char *c = 0; \n "
" { \n "
" delete c; \n "
" } \n "
" c[0] = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:7]: (error) Null pointer dereference: c \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2013-04-15 19:00:15 +02:00
check ( " static void foo() { \n "
2021-05-13 20:21:02 +02:00
" if (3 > *(int*)0); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-05-13 20:21:02 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference: (int*)0 \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
// no false positive..
check ( " static void foo() \n "
" { \n "
" Foo *p = 0; \n "
" p = new Foo; \n "
" p->abcd(); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void foo() \n "
" { \n "
" int sz = sizeof((*(struct dummy *)0).x); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void get_offset(long &offset) \n "
" { \n "
" mystruct * temp; temp = 0; \n "
" offset = (long)(&(temp->z)); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// Ticket #1893 - try/catch inside else
check ( " int *test(int *Z) \n "
" { \n "
" int *Q=NULL; \n "
" if (Z) { \n "
" Q = Z; \n "
" } \n "
" else { \n "
" Z = new int; \n "
" try { \n "
" } catch(...) { \n "
" } \n "
" Q = Z; \n "
" } \n "
" *Q=1; \n "
" return Q; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int *test(int *Z) \n "
" { \n "
" int *Q=NULL; \n "
" if (Z) { \n "
" Q = Z; \n "
" } \n "
" else { \n "
" try { \n "
" } catch(...) { \n "
" } \n "
" } \n "
" *Q=1; \n "
" return Q; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:12]: (warning) Possible null pointer dereference: Q \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
// Ticket #2052 (false positive for 'else continue;')
check ( " void f() { \n "
" for (int x = 0; x < 5; ++x) { "
" int *p = 0; \n "
" if (a(x)) p=b(x); \n "
" else continue; \n "
" *p = 0; \n "
" } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// function pointer..
check ( " void foo() \n "
" { \n "
" void (*f)(); \n "
" f = 0; \n "
" f(); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (error) Null pointer dereference: f \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2022-04-26 17:25:56 +02:00
check ( " int* g(); \n " // #11007
" int* f() { \n "
" static int* (*fun)() = 0; \n "
" if (!fun) \n "
" fun = g; \n "
" return fun(); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
// loops..
check ( " void f() { \n "
" int *p = 0; \n "
" for (int i = 0; i < 10; ++i) { \n "
" int x = *p + 1; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
check ( " void f(int a) { \n "
" const char *p = 0; \n "
" if (a) { \n "
2011-02-05 12:53:28 +01:00
" p = \" abcd \" ; \n "
2010-10-31 11:51:25 +01:00
" } \n "
" for (int i = 0; i < 3; i++) { \n "
" if (a && (p[i] == '1')); \n "
" } \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-29 20:30:23 +01:00
// ticket #2251: taking the address of member
check ( " void f() { \n "
" Fred *fred = 0; \n "
" int x = &fred->x; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-11-29 20:30:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-18 19:34:14 +02:00
2020-05-21 08:47:48 +02:00
// ticket #3220: dereferencing a null pointer is UB
2011-10-18 19:34:14 +02:00
check ( " void f() { \n "
" Fred *fred = NULL; \n "
" fred->do_something(); \n "
" } " ) ;
2020-05-21 08:47:48 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: fred \n " , errout . str ( ) ) ;
2012-02-01 20:38:47 +01:00
// ticket #3570 - parsing of conditions
{
check ( " void f() { \n "
" int *p = NULL; \n "
" if (x) \n "
" p = q; \n "
" if (p && *p) { } \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2012-02-01 20:38:47 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int *p = NULL; \n "
" if (x) \n "
" p = q; \n "
" if (!p || *p) { } \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2012-02-01 20:38:47 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" int *p = NULL; \n "
" if (x) \n "
" p = q; \n "
" if (p || *p) { } \n "
" } " ) ;
2021-08-11 08:37:37 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Possible null pointer dereference: p \n " , errout . str ( ) ) ;
2012-02-01 20:38:47 +01:00
}
2019-02-26 17:14:45 +01:00
// ticket #8831 - FP triggered by if/return/else sequence
{
check ( " void f(int *p, int *q) { \n "
" if (p == NULL) \n "
" return; \n "
" else if (q == NULL) \n "
" return; \n "
" *q = 0; \n "
" } \n "
" \n "
" void g() { \n "
" f(NULL, NULL); \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-12-20 13:12:19 +01:00
check ( " void f() { \n " // #5979
" int* const crash = 0; \n "
" *crash = 0; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: crash \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
}
2010-12-26 13:38:16 +01:00
// Ticket #2350
2014-11-20 14:20:09 +01:00
void nullpointerExecutionPathsLoop ( ) {
2010-12-26 13:38:16 +01:00
// No false positive:
check ( " void foo() { \n "
" int n; \n "
2014-01-22 20:16:31 +01:00
" int *argv32 = p; \n "
2010-12-26 13:38:16 +01:00
" if (x) { \n "
" n = 0; \n "
" argv32 = 0; \n "
" } \n "
" \n "
" for (int i = 0; i < n; i++) { \n "
" argv32[i] = 0; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-12-26 13:38:16 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// No false negative:
check ( " void foo() { \n "
" int n; \n "
" int *argv32; \n "
" if (x) { \n "
" n = 10; \n "
" argv32 = 0; \n "
" } \n "
" \n "
" for (int i = 0; i < n; i++) { \n "
" argv32[i] = 0; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2021-06-09 09:20:43 +02:00
ASSERT_EQUALS ( " [test.cpp:10]: (warning) Possible null pointer dereference: argv32 \n " , errout . str ( ) ) ;
2011-02-19 20:19:46 +01:00
// #2231 - error if assignment in loop is not used
2021-05-13 20:21:02 +02:00
// extracttests.start: int y[20];
2011-02-19 20:19:46 +01:00
check ( " void f() { \n "
" char *p = 0; \n "
" \n "
" for (int x = 0; x < 3; ++x) { \n "
" if (y[x] == 0) { \n "
2021-05-13 20:21:02 +02:00
" p = (char *)malloc(10); \n "
2011-02-19 20:19:46 +01:00
" break; \n "
" } \n "
" } \n "
" \n "
" *p = 0; \n "
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:11]: (warning) Possible null pointer dereference: p \n " , errout . str ( ) ) ;
2010-12-26 13:38:16 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer7 ( ) {
2010-10-31 11:51:25 +01:00
check ( " void foo() \n "
" { \n "
" wxLongLong x = 0; \n "
" int y = x.GetValue(); \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer9 ( ) { //#ticket 1778
2010-10-31 11:51:25 +01:00
check ( " void foo() \n "
" { \n "
" std::string * x = 0; \n "
" *x = \" test \" ; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: x \n " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer10 ( ) {
2021-05-13 20:21:02 +02:00
// extracttests.start: struct my_type { int x; };
2011-05-28 05:30:19 +02:00
check ( " void foo() \n "
" { \n "
" struct my_type* p = 0; \n "
" p->x = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
2011-06-04 04:16:08 +02:00
}
2014-11-20 14:20:09 +01:00
void nullpointer11 ( ) { // ticket #2812
2021-05-13 20:21:02 +02:00
// extracttests.start: struct my_type { int x; };
2011-05-28 05:30:19 +02:00
check ( " int foo() \n "
" { \n "
" struct my_type* p; \n "
2012-12-06 19:19:22 +01:00
" p = 0; \n "
2011-05-28 05:30:19 +02:00
" return p->x; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
2011-05-28 05:30:19 +02:00
}
2014-11-20 14:20:09 +01:00
void nullpointer12 ( ) { // ticket #2470, #4035
2012-08-25 12:00:25 +02:00
const char code [ ] = " int foo() \n "
" { \n "
" int* i = nullptr; \n "
" return *i; \n "
" } \n " ;
2015-07-25 14:17:55 +02:00
check ( code , false , " test.cpp " ) ; // C++ file => nullptr means NULL
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: i \n " , errout . str ( ) ) ;
2011-10-16 12:54:58 +02:00
2015-07-25 14:17:55 +02:00
check ( code , false , " test.c " ) ; // C file => nullptr does not mean NULL
2011-10-16 12:54:58 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer15 ( ) { // #3560
2012-01-28 08:06:03 +01:00
check ( " void f() { \n "
" char *p = 0; \n "
" if (x) p = \" abcd \" ; \n "
" return p ? f(*p) : f(0); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer16 ( ) { // #3591
2012-02-13 19:46:45 +01:00
check ( " void foo() { \n "
" int *p = 0; \n "
" bar(&p); \n "
" *p = 0; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2012-02-13 19:46:45 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer17 ( ) { // #3567
2012-03-07 20:31:23 +01:00
check ( " int foo() { \n "
" int *p = 0; \n "
2013-04-15 19:00:15 +02:00
" if (x) { return 0; } \n "
" return !p || *p; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2012-03-07 20:31:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int foo() { \n "
" int *p = 0; \n "
2013-04-15 19:00:15 +02:00
" if (x) { return 0; } \n "
2012-03-07 20:31:23 +01:00
" return p && *p; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2012-03-07 20:31:23 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer18 ( ) { // #1927
2012-04-01 15:54:41 +02:00
check ( " void f () \n "
" { \n "
" int i=0; \n "
2012-04-01 15:56:48 +02:00
" char *str=NULL; \n "
" while (str[i]) \n "
" { \n "
" i++; \n "
" }; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (error) Null pointer dereference: str \n " , errout . str ( ) ) ;
2012-04-01 15:54:41 +02:00
}
2014-11-20 14:20:09 +01:00
void nullpointer19 ( ) { // #3811
2012-05-16 09:56:39 +02:00
check ( " int foo() { \n "
" perror(0); \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer20 ( ) { // #3807
2012-05-31 18:41:00 +02:00
check ( " void f(int x) { \n "
" struct xy *p = 0; \n "
" if (x) p = q; \n "
" if (p ? p->x || p->y : 0) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int x) { \n " // false negative
" struct xy *p = 0; \n "
" if (x) p = q; \n "
" if (y ? p->x : p->y) { } \n "
" } " ) ;
2019-10-20 15:20:05 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:4]: (warning) Possible null pointer dereference: p \n " , " " , errout . str ( ) ) ;
2012-05-31 18:41:00 +02:00
}
2014-11-20 14:20:09 +01:00
void nullpointer21 ( ) { // #4038 - fp: if (x) p=q; else return;
2012-09-23 09:20:16 +02:00
check ( " void f(int x) { \n "
" int *p = 0; \n "
" if (x) p = q; \n "
" else return; \n "
" *p = 0; \n " // <- p is not NULL
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer23 ( ) { // #4665
2013-03-19 08:47:05 +01:00
check ( " void f(){ \n "
" char *c = NULL; \n "
" char cBuf[10]; \n "
" sprintf(cBuf, \" %s \" , c ? c : \" 0 \" ); \n "
" } " ) ;
2013-11-04 23:09:15 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-03-19 08:47:05 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer24 ( ) { // #5083 - fp: chained assignment
2013-10-20 13:37:36 +02:00
check ( " void f(){ \n "
" char *c = NULL; \n "
" x = c = new char[10]; \n "
" *c = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer25 ( ) { // #5061
2014-03-26 12:17:27 +01:00
check ( " void f(int *data, int i) \n "
" { \n "
" int *array = NULL; \n "
" if (data == 1 && array[i] == 0) \n "
" std::cout << \" test \" ; \n "
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: array \n " , errout . str ( ) ) ;
2014-03-26 12:17:27 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer26 ( ) { // #3589
2014-03-27 19:16:30 +01:00
check ( " double foo() { \n "
" sk *t1 = foo(); \n "
" sk *t2 = foo(); \n "
" if ((!t1) && (!t2)) \n "
" return 0.0; \n "
" if (t1 && (!t2)) \n "
" return t1->Inter(); \n "
" if (t2->GetT() == t) \n "
" return t2->Inter(); \n "
" if (t2 && (!t1)) \n "
" return 0.0; \n "
" return 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-05-16 21:11:04 +02:00
void nullpointer27 ( ) { // #6568
check ( " template<class Type> \n "
" class Foo { \n "
" Foo<Type>& operator = ( Type* ); \n "
" }; \n "
" template<class Type> \n "
" Foo<Type>& Foo<Type>::operator = ( Type* pointer_ ) { \n "
" pointer_=NULL; \n "
" *pointer_=0; \n "
" return *this; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (error) Null pointer dereference: pointer_ \n " , errout . str ( ) ) ;
}
2015-08-15 00:31:26 +02:00
void nullpointer28 ( ) { // #6491
check ( " typedef struct { int value; } S; \n "
2021-02-20 12:58:42 +01:00
" int f(const S *s) { \n "
" int i = s ? s->value + 1 \n "
" : s->value - 1; // <-- null ptr dereference \n "
2015-08-15 00:31:26 +02:00
" return i; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2022-02-25 05:53:51 +01:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 's' is redundant or there is possible null pointer dereference: s. \n " ,
errout . str ( ) ) ;
2015-08-15 00:31:26 +02:00
}
2015-08-15 01:00:36 +02:00
void nullpointer30 ( ) { // #6392
check ( " void f(std::vector<std::string> *values) \n "
" { \n "
" values->clear(); \n "
2021-02-20 12:58:42 +01:00
" if (values) \n "
2015-08-15 01:00:36 +02:00
" { \n "
" for (int i = 0; i < values->size(); ++i) \n "
" { \n "
" values->push_back( \" test \" ); \n "
" } \n "
" } \n "
" } \n " , true ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:4] -> [test.cpp:3]: (warning) Either the condition 'values' is redundant or there is possible null pointer dereference: values. \n " ,
errout . str ( ) ) ;
2015-08-15 01:00:36 +02:00
}
2018-04-09 16:13:17 +02:00
void nullpointer31 ( ) { // #8482
check ( " struct F \n "
" { \n "
" int x; \n "
" }; \n "
2021-02-20 12:58:42 +01:00
" \n "
2018-04-09 16:13:17 +02:00
" static void foo(F* f) \n "
" { \n "
" if( f ) {} \n "
" else { return; } \n "
" (void)f->x; \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2021-08-08 10:55:10 +02:00
check ( " typedef struct \n "
" { \n "
" int x; \n "
" } F; \n "
" \n "
" static void foo(F* f) \n "
" { \n "
" if( !f || f->x == 0 ) \n "
" { \n "
" if( !f ) \n "
" return; \n "
" } \n "
" \n "
" (void)f->x; \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-04-09 16:13:17 +02:00
}
2018-11-03 15:53:24 +01:00
void nullpointer32 ( ) { // #8460
check ( " int f(int * ptr) { \n "
" if(ptr) \n "
" { return 0;} \n "
" else{ \n "
" int *p1 = ptr; \n "
" return *p1; \n "
" } \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:6]: (warning) Either the condition 'ptr' is redundant or there is possible null pointer dereference: p1. \n " , errout . str ( ) ) ;
}
2019-08-08 07:46:47 +02:00
void nullpointer33 ( ) {
check ( " void f(int * x) { \n "
" if (x != nullptr) \n "
" *x = 2; \n "
" else \n "
" *x = 3; \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:5]: (warning) Either the condition 'x!=nullptr' is redundant or there is possible null pointer dereference: x. \n " , errout . str ( ) ) ;
}
void nullpointer34 ( ) {
check ( " void g() { \n "
2021-08-07 20:51:18 +02:00
" throw " " ; \n "
2019-08-08 07:46:47 +02:00
" } \n "
" bool f(int * x) { \n "
" if (x) *x += 1; \n "
" if (!x) g(); \n "
" return *x; \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-08-11 15:39:37 +02:00
void nullpointer35 ( ) {
check ( " bool f(int*); \n "
" void g(int* x) { \n "
" if (f(x)) { \n "
" *x = 1; \n "
" } \n "
" } \n "
" void h() { \n "
" g(0); \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2020-01-05 16:25:33 +01:00
check ( " bool f(int*); \n "
" void g(int* x) { \n "
" bool b = f(x); \n "
" if (b) { \n "
" *x = 1; \n "
" } \n "
" } \n "
" void h() { \n "
" g(0); \n "
" } \n " ,
true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-08-11 15:39:37 +02:00
}
2019-09-01 09:58:53 +02:00
void nullpointer36 ( ) {
check ( " char* f(char* s) { \n "
" char* start = s; \n "
" if (!s) \n "
" return (s); \n "
" while (isspace(*start)) \n "
" start++; \n "
" return (start); \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-09-20 15:07:27 +02:00
void nullpointer37 ( ) {
2019-09-20 15:06:37 +02:00
check ( " void f(int value, char *string) { \n "
" char *ptr1 = NULL, *ptr2 = NULL; \n "
" unsigned long count = 0; \n "
" if(!string) \n "
" return; \n "
" ptr1 = string; \n "
" ptr2 = strrchr(string, 'a'); \n "
" if(ptr2 == NULL) \n "
" return; \n "
" while(ptr1 < ptr2) { \n "
" count++; \n "
" ptr1++; \n "
" } \n "
" } \n " ,
true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-09-20 15:07:27 +02:00
void nullpointer38 ( ) {
2019-09-20 15:06:37 +02:00
check ( " void f(int * x) { \n "
" std::vector<int*> v; \n "
" if (x) { \n "
" v.push_back(x); \n "
" *x; \n "
" } \n "
" } \n " ,
true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-10-01 08:39:08 +02:00
void nullpointer39 ( ) {
2019-09-30 21:04:43 +02:00
check ( " struct A { int * x; }; \n "
" void f(struct A *a) { \n "
" if (a->x == NULL) {} \n "
" *(a->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-30 21:04:43 +02:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->x==NULL' is redundant or there is possible null pointer dereference: a->x. \n " ,
errout . str ( ) ) ;
}
2019-10-01 08:39:08 +02:00
void nullpointer40 ( ) {
2019-09-30 21:04:43 +02:00
check ( " struct A { std::unique_ptr<int> x; }; \n "
" void f(struct A *a) { \n "
" if (a->x == nullptr) {} \n "
" *(a->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-30 21:04:43 +02:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->x==nullptr' is redundant or there is possible null pointer dereference: a->x. \n " ,
errout . str ( ) ) ;
}
2019-10-01 08:39:08 +02:00
void nullpointer41 ( ) {
2019-09-30 21:04:43 +02:00
check ( " struct A { int * g() const; }; \n "
" void f(struct A *a) { \n "
" if (a->g() == nullptr) {} \n "
" *(a->g()); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-30 21:04:43 +02:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->g()==nullptr' is redundant or there is possible null pointer dereference: a->g(). \n " ,
errout . str ( ) ) ;
check ( " struct A { int * g(); }; \n "
" void f(struct A *a) { \n "
" if (a->g() == nullptr) {} \n "
" *(a->g()); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-30 21:04:43 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-10-01 08:39:08 +02:00
void nullpointer42 ( ) {
2019-09-30 21:04:43 +02:00
check ( " struct A { std::unique_ptr<int> g() const; }; \n "
" void f(struct A *a) { \n "
" if (a->g() == nullptr) {} \n "
" *(a->g()); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-30 21:04:43 +02:00
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'a->g()==nullptr' is redundant or there is possible null pointer dereference: a->g(). \n " ,
errout . str ( ) ) ;
}
2019-11-08 08:11:41 +01:00
void nullpointer43 ( ) {
check ( " struct A { int* x; }; \n "
" void f(A* a) { \n "
" int * x = a->x; \n "
" if (x) { \n "
" (void)*a->x; \n "
" } \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-11-08 08:11:41 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-11-04 07:02:25 +01:00
void nullpointer44 ( ) {
// #9395
check ( " int foo( ) { \n "
" const B* b = getB(); \n "
" const double w = ( nullptr != b) ? 42. : 0.0; \n "
" if ( w == 0.0 ) \n "
" return 0; \n "
" return b->get(); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-11-04 07:02:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// #9423
check ( " extern F* GetF(); \n "
" extern L* GetL(); \n "
" void Foo() { \n "
" const F* const fPtr = GetF(); \n "
" const bool fPtrOk = fPtr != NULL; \n "
" assert(fPtrOk); \n "
" if (!fPtrOk) \n "
" return; \n "
" L* const lPtr = fPtr->l; \n "
" const bool lPtrOk = lPtr != NULL; \n "
" assert(lPtrOk); \n "
" if (!lPtrOk) \n "
" return; \n "
" lPtr->Clear(); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-11-04 07:02:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-11-08 08:11:41 +01:00
}
void nullpointer45 ( ) {
check ( " struct a { \n "
" a *b() const; \n "
" }; \n "
" void g() { throw 0; } \n "
" a h(a * c) { \n "
" if (c && c->b()) {} \n "
" if (!c) \n "
" g(); \n "
" if (!c->b()) \n "
" g(); \n "
" a d = *c->b(); \n "
" return d; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-11-08 08:11:41 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct a { \n "
" a *b() const; \n "
" }; \n "
" void e() { throw 0; } \n "
" a f() { \n "
" a *c = 0; \n "
" if (0 && c->b()) {} \n "
" if (!c) \n "
" e(); \n "
" if (!c->b()) \n "
" e(); \n "
" a d = *c->b(); \n "
" return d; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-11-08 08:11:41 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-11-04 07:02:25 +01:00
}
2019-11-30 09:22:03 +01:00
void nullpointer46 ( ) {
2019-12-01 12:16:31 +01:00
check ( " void f() { \n "
" char* p = new(std::nothrow) char[1]; \n "
" if( p ) {} \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-12-01 12:16:31 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void nullpointer47 ( ) {
2019-11-30 09:22:03 +01:00
check ( " void f(int *p) { \n "
" if(!p[0]) {} \n "
" const int *const a = p; \n "
" if(!a){} \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-11-30 09:22:03 +01:00
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:2]: (warning) Either the condition '!a' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
}
2019-12-03 18:30:52 +01:00
void nullpointer48 ( ) {
check ( " template<class T> \n "
" auto f(T& x) -> decltype(x); \n "
" int& g(int* x) { \n "
" return f(*x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-12-03 18:30:52 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-01-05 17:00:34 +01:00
void nullpointer49 ( ) {
2020-01-05 16:25:33 +01:00
check ( " void f(int *p, int n) { \n "
" int *q = 0; \n "
" if(n > 10) q = p; \n "
" *p +=2; \n "
" if(n < 120) *q+=12; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-02-19 07:55:04 +01:00
ASSERT_EQUALS ( " [test.cpp:5]: (warning) Possible null pointer dereference: q \n " , errout . str ( ) ) ;
2020-01-05 16:25:33 +01:00
check ( " void f(int *p, int n) { \n "
" int *q = 0; \n "
" if(n > 10) q = p; \n "
" *p +=2; \n "
" if(n > 10) *q+=12; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-01-05 16:25:33 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-01-05 17:00:34 +01:00
void nullpointer50 ( ) {
2020-01-05 16:25:33 +01:00
check ( " void f(int *p, int a) { \n "
" if(!p) { \n "
" if(a > 0) { \n "
" if(a > 10){} \n "
" else { \n "
" *p = 0; \n "
" } \n "
" } \n "
" } \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-01-05 16:25:33 +01:00
ASSERT_EQUALS (
" [test.cpp:2] -> [test.cpp:6]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
}
2020-02-13 16:27:06 +01:00
void nullpointer51 ( ) {
check ( " struct a { \n "
" a *b(); \n "
" }; \n "
" bool c(a *, const char *); \n "
" a *d(a *e) { \n "
" if (e) {} \n "
" if (c(e, \" \" )) \n "
" return nullptr; \n "
" return e->b(); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-02-13 16:27:06 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-02-16 16:02:22 +01:00
void nullpointer52 ( ) {
check ( " int f(int a, int* b) { \n "
" int* c = nullptr; \n "
" if(b) c = b; \n "
" if (!c) c = &a; \n "
" return *c; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-02-16 16:02:22 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f(int a, int* b) { \n "
" int* c = nullptr; \n "
" if(b) c = b; \n "
" bool d = !c; \n "
" if (d) c = &a; \n "
" return *c; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-02-16 16:02:22 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { int* x; }; \n "
" int f(int a, int* b) { \n "
" A c; \n "
" c.x = nullptr; \n "
" if(b) c.x = b; \n "
" if (!c.x) c.x = &a; \n "
" return *c.x; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-02-16 16:02:22 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { int* x; }; \n "
" int f(int a, int* b) { \n "
" A c; \n "
" c.x = nullptr; \n "
" if(b) c.x = b; \n "
" bool d = !c.x; \n "
2021-01-21 19:49:37 +01:00
" if (d) c.x = &a; \n "
2020-02-16 16:02:22 +01:00
" return *c.x; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-02-16 16:02:22 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2021-01-21 19:49:37 +01:00
check ( " struct A { int* x; }; \n "
" int f(int a, int* b) { \n "
" A c; \n "
" c.x = nullptr; \n "
" if(b) c.x = b; \n "
" bool d = !c.x; \n "
" if (!d) c.x = &a; \n "
" return *c.x; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (warning) Possible null pointer dereference: c.x \n " , errout . str ( ) ) ;
2020-02-16 16:02:22 +01:00
}
2020-03-01 16:46:20 +01:00
void nullpointer53 ( ) {
check ( " void f(int nParams, int* params) { \n "
" for (int n=1; n<nParams+10; ++n) { \n "
" params[n]=42; \n "
" } \n "
" } \n "
" void bar() { \n "
" f(0, 0); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-03-01 16:46:20 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Possible null pointer dereference: params \n " , errout . str ( ) ) ;
}
2020-05-19 07:53:54 +02:00
void nullpointer54 ( ) {
check ( " int foo (int **array, size_t n_array) { \n "
" size_t i; \n "
" for (i = 0; i < n_array; ++i) { \n "
" if (*array[i] == 1) \n "
" return 1; \n "
" } \n "
" return 0; \n "
2021-02-20 12:58:42 +01:00
" } \n "
2020-05-19 07:53:54 +02:00
" int bar() { \n "
2021-02-20 12:58:42 +01:00
" int **array = NULL; \n "
2020-05-19 07:53:54 +02:00
" foo (array, 0); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-05-19 07:53:54 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-05-21 08:47:48 +02:00
void nullpointer55 ( ) {
check ( " void f(const Token* tok) { \n "
" const Token* tok3 = tok; \n "
" while (tok3->astParent() && tok3->str() == \" , \" ) \n "
" tok3 = tok3->astParent(); \n "
" if (tok3 && tok3->str() == \" ( \" ) {} \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:5] -> [test.cpp:3]: (warning) Either the condition 'tok3' is redundant or there is possible null pointer dereference: tok3. \n " ,
errout . str ( ) ) ;
2020-05-21 08:47:48 +02:00
check ( " void f(int* t1, int* t2) { \n "
" while (t1 && t2 && \n "
" *t1 == *t2) { \n "
" t1 = nullptr; \n "
" t2 = nullptr; \n "
" } \n "
" if (!t1 || !t2) \n "
" return; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-05-21 08:47:48 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool f(int* i); \n "
" void g(int* i) { \n "
" while(f(i) && *i == 0) \n "
" i++; \n "
" if (!i) {} \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-05-21 08:47:48 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-05-28 07:41:47 +02:00
void nullpointer56 ( ) {
check ( " struct ListEntry { \n "
" struct ListEntry *next; \n "
" }; \n "
" static void dostuff(ListEntry * listHead) { \n "
" ListEntry *prev = NULL; \n "
" for (ListEntry *cursor = listHead; cursor != NULL; prev = cursor, cursor = cursor->next) {} \n "
" if (prev) {} \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2020-05-28 07:41:47 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-06-13 10:26:54 +02:00
void nullpointer57 ( ) {
check ( " void f() { \n "
" FILE* fptr = fopen( \" test \" , \" r \" ); \n "
" if (fptr != nullptr) { \n "
" std::function<void()> fn([&] { \n "
" fclose(fptr); \n "
" fptr = NULL; \n "
" }); \n "
" fgetc(fptr); \n "
" fn(); \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-07-20 06:25:35 +02:00
void nullpointer58 ( ) {
check ( " struct myStruct { char entry[0]; }; \n "
" void f() { \n "
" struct myStruct* sPtr = NULL; \n "
" int sz = (!*(&sPtr) || ((*(&sPtr))->entry[0] > 15)) ? \n "
" sizeof((*(&sPtr))->entry[0]) : 123456789; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-09-14 09:17:29 +02:00
void nullpointer59 ( ) {
check ( " struct Box { \n "
" struct Box* prev; \n "
" struct Box* next; \n "
" }; \n "
" void foo(Box** pfreeboxes) { \n "
" Box *b = *pfreeboxes; \n "
" *pfreeboxes = b->next; \n "
" if( *pfreeboxes ) \n "
" (*pfreeboxes)->prev = nullptr; \n "
" b->next = nullptr; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-09-23 07:45:03 +02:00
void nullpointer60 ( ) {
check ( " void f(){ \n "
" char uuid[128]; \n "
" char *s1; \n "
" memset(uuid, 0, sizeof(uuid)); \n "
" s1 = strchr(uuid, '='); \n "
" s1 = s1 ? s1 + 1 : &uuid[5]; \n "
" if (!strcmp( \" 00000000000000000000000000000000 \" , s1) ) \n "
" return; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-11-11 09:15:36 +01:00
void nullpointer61 ( ) {
2020-11-10 16:00:55 +01:00
check ( " struct a { \n "
" int *e; \n "
" }; \n "
" struct f { \n "
" a *g() const; \n "
" }; \n "
" void h() { \n "
" for (f b;;) { \n "
" a *c = b.g(); \n "
" int *d = c->e; \n "
" if (d) \n "
" ; \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" A* g() const; \n "
" A* h() const; \n "
" }; \n "
" void f(A* a) { \n "
" if (!a->h()) \n "
" return; \n "
" const A *b = a; \n "
" while (b && !b->h()) \n "
" b = b->g(); \n "
" if (!b || b == b->g()->h()) \n "
" return; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-11-11 09:15:36 +01:00
void nullpointer62 ( ) {
2020-11-10 16:00:55 +01:00
check ( " struct A { \n "
" bool f()() const; \n "
" }; \n "
" void a(A *x) { \n "
" std::string b = x && x->f() ? \" \" : \" \" ; \n "
" if (x) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" bool f()() const; \n "
" }; \n "
" void a(A *x) { \n "
" std::string b = (!x || x->f()) ? \" \" : \" \" ; \n "
" if (x) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" A * aa; \n "
" }; \n "
" void b(A*); \n "
" void a(A *x) { \n "
" b(x ? x->aa : nullptr); \n "
" if (!x) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-11-11 09:15:36 +01:00
void nullpointer63 ( ) {
2020-11-10 16:00:55 +01:00
check ( " struct A { \n "
" A* a() const; \n "
" A* b() const; \n "
" }; \n "
" A* f(A*); \n "
" void g(const A* x) { \n "
" A *d = x->a(); \n "
" d = f(d->b()) ? d->a() : nullptr; \n "
" if (d && f(d->b())) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-11-11 09:15:36 +01:00
void nullpointer64 ( ) {
2020-11-10 16:00:55 +01:00
check ( " struct A { \n "
" A* f() const; \n "
" int g() const; \n "
" }; \n "
" bool a; \n "
" bool b(A* c) { \n "
" if (c->g() == 0) \n "
" ; \n "
" A *aq = c; \n "
" if (c->g() == 0) \n "
" c = c->f(); \n "
" if (c) \n "
" for (A *d = c; d != aq; d = d->f()) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" A* g() const; \n "
" A* h() const; \n "
" }; \n "
" bool i(A*); \n "
" void f(A* x) { \n "
" if (i(x->g())) { \n "
" A *y = x->g(); \n "
" x = x->g()->h(); \n "
" if (x && x->g()) { \n "
" y = x->g()->h(); \n "
" } \n "
" if (!y) {} \n "
" } \n "
" } \n " ) ;
2022-01-16 12:32:46 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2020-11-10 16:00:55 +01:00
}
2020-11-20 09:36:09 +01:00
void nullpointer65 ( ) {
check ( " struct A { \n "
" double get(); \n "
" }; \n "
" double x; \n "
" double run(A** begin, A** end) { \n "
" A* a = nullptr; \n "
" while (begin != end) { \n "
" a = *begin; \n "
" x = a->get(); \n "
" ++begin; \n "
" } \n "
" x = 0; \n "
" if (a) \n "
" return a->get(); \n "
" return 0; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-12-14 11:13:47 +01:00
void nullpointer66 ( ) {
check ( " int f() { \n "
" int ret = 0; \n "
" int *v = nullptr; \n "
" if (!MyAlloc(&v)) { \n "
" ret = -1; \n "
" goto done; \n "
" } \n "
" DoSomething(*v); \n "
" done: \n "
" if (v) \n "
" MyFree(&v); \n "
" return ret; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-12-27 10:14:46 +01:00
void nullpointer67 ( ) {
check ( " int result; \n "
" \n "
" int test_b(void) { \n "
" char **string = NULL; \n "
" \n "
" /* The bug disappears if \" result = \" is omitted. */ \n "
" result = some_other_call(&string); \n "
" if (string && string[0]) \n "
" return 0; \n "
" return -1; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int result; \n "
" \n "
" int test_b(void) { \n "
" char **string = NULL; \n "
" \n "
" some_other_call(&string); \n "
" if (string && string[0]) \n "
" return 0; \n "
" return -1; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-01-10 14:46:19 +01:00
2021-01-08 22:55:04 +01:00
void nullpointer68 ( ) {
check ( " struct A { \n "
" A* b; \n "
" }; \n "
" void f(A* c) { \n "
" c = c->b; \n "
" if (c->b) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct A { \n "
" A* b; \n "
" }; \n "
" void f(A* c) { \n "
" A* d = c->b; \n "
" A *e = c; \n "
" while (nullptr != (e = e->b)) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2020-12-27 10:14:46 +01:00
2021-01-25 22:51:50 +01:00
void nullpointer69 ( ) {
2021-01-23 17:52:01 +01:00
check ( " void f(const Scope *scope) { \n "
" if (scope->definedType) {} \n "
" while (scope) { \n "
" scope = scope->nestedIn; \n "
" enumerator = scope->findEnumerator(); \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:5]: (warning) Either the condition 'scope' is redundant or there is possible null pointer dereference: scope. \n " ,
errout . str ( ) ) ;
check ( " void f(const Scope *scope) { \n "
" if (scope->definedType) {} \n "
" while (scope && scope->nestedIn) { \n "
" if (scope->type == Scope::eFunction && scope->functionOf) \n "
" scope = scope->functionOf; \n "
" else \n "
" scope = scope->nestedIn; \n "
" enumerator = scope->findEnumerator(); \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:8]: (warning) Either the condition 'scope' is redundant or there is possible null pointer dereference: scope. \n " ,
errout . str ( ) ) ;
check ( " struct a { \n "
" a *b() const; \n "
" void c(); \n "
" }; \n "
" void d() { \n "
" for (a *e;;) { \n "
" e->b()->c(); \n "
" while (e) \n "
" e = e->b(); \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-01-25 17:24:36 +01:00
void nullpointer70 ( ) {
check ( " struct Token { \n "
" const Token* nextArgument() const; \n "
" const Token* next() const; \n "
" int varId() const; \n "
" }; \n "
2021-05-13 20:21:02 +02:00
" int f(const Token *first, const Token* second) { \n "
2021-01-25 17:24:36 +01:00
" first = first->nextArgument(); \n "
" if (first) \n "
" first = first->next(); \n "
" if (second->next()->varId() == 0) { \n "
" second = second->nextArgument(); \n "
" if (!first || !second) \n "
" return 0; \n "
" } else if (!first) { \n "
" return 0; \n "
" } \n "
" return first->varId(); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Token { \n "
" const Token* nextArgument() const; \n "
" const Token* next() const; \n "
" int varId() const; \n "
2021-05-13 20:21:02 +02:00
" void str() const; "
2021-01-25 17:24:36 +01:00
" }; \n "
2021-05-13 20:21:02 +02:00
" void f(const Token *first) { \n "
2021-01-25 17:24:36 +01:00
" first = first->nextArgument(); \n "
" if (first) \n "
" first = first->next(); \n "
" first->str(); \n "
" } \n " ) ;
2021-08-04 21:07:31 +02:00
TODO_ASSERT_EQUALS (
" [test.cpp:8] -> [test.cpp:10]: (warning) Either the condition 'first' is redundant or there is possible null pointer dereference: first. \n " ,
" " ,
errout . str ( ) ) ;
2021-01-25 17:24:36 +01:00
}
2021-01-25 22:51:50 +01:00
2021-02-17 12:09:11 +01:00
void nullpointer71 ( ) {
check ( " void f() { \n "
" Device* dev = Get(); \n "
" SetCount(dev == nullptr ? 0 : dev->size()); \n "
" if (dev) \n "
" DoSomething(dev); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" Device* dev = Get(); \n "
" SetCount(dev != nullptr ? dev->size() : 0); \n "
" if (dev) \n "
" DoSomething(dev); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-06-29 19:42:47 +02:00
void nullpointer72 ( ) { // #10215
2021-06-30 20:27:35 +02:00
check ( " int test() { \n "
2021-08-26 19:37:04 +02:00
" int* p0 = nullptr, *p1 = nullptr; \n "
" getFoo(p0); \n "
" getBar(p1); \n "
" if (!(p0 != nullptr && p1 != nullptr)) \n "
" return {}; \n "
" return *p0 + *p1; \n "
2021-06-30 20:27:35 +02:00
" } \n " , true /*inconclusive*/ ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int test2() { \n "
2021-08-26 19:37:04 +02:00
" int* p0 = nullptr; \n "
" if (!(getBaz(p0) && p0 != nullptr)) \n "
" return 0; \n "
" return *p0; \n "
" } \n " , true /*inconclusive*/ ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int test3() { \n "
" Obj* PObj = nullptr; \n "
" if (!(GetObj(PObj) && PObj != nullptr)) \n "
" return 1; \n "
" if (!PObj->foo()) \n "
" test(); \n "
" PObj->bar(); \n "
2021-06-30 20:27:35 +02:00
" } \n " , true /*inconclusive*/ ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2021-06-29 19:42:47 +02:00
}
void nullpointer73 ( ) {
2021-06-19 13:58:57 +02:00
check ( " void f(bool flag2, int* ptr) { \n "
" bool flag1 = true; \n "
" if (flag2) { \n "
" if (ptr != nullptr) \n "
" (*ptr)++; \n "
" else \n "
" flag1 = false; \n "
" } \n "
" if (flag1 && flag2) \n "
" (*ptr)++; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(bool flag2, int* ptr) { \n "
" bool flag1 = true; \n "
" if (flag2) { \n "
" if (ptr != nullptr) \n "
" (*ptr)++; \n "
" else \n "
" flag1 = false; \n "
" } \n "
" if (!flag1 && flag2) \n "
" (*ptr)++; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:4] -> [test.cpp:10]: (warning) Either the condition 'ptr!=nullptr' is redundant or there is possible null pointer dereference: ptr. \n " , errout . str ( ) ) ;
}
2021-07-03 09:12:26 +02:00
void nullpointer74 ( ) {
check ( " struct d { \n "
" d* e(); \n "
" }; \n "
" void g(d* f) { \n "
" do { \n "
" f = f->e(); \n "
" if (f) {} \n "
" } while (0); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct d { \n "
" d* e(); \n "
" }; \n "
" void g(d* f, int i) { \n "
" do { \n "
" i--; \n "
" f = f->e(); \n "
" if (f) {} \n "
" } while (i > 0); \n "
" } \n " ) ;
ASSERT_EQUALS (
" [test.cpp:8] -> [test.cpp:7]: (warning) Either the condition 'f' is redundant or there is possible null pointer dereference: f. \n " ,
errout . str ( ) ) ;
check ( " struct d { \n "
" d* e(); \n "
" }; \n "
" void g(d* f, int i) { \n "
" do { \n "
" i--; \n "
" f = f->e(); \n "
" if (f) {} \n "
" } while (f && i > 0); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-07-18 10:01:22 +02:00
void nullpointer75 ( ) {
2021-07-18 07:46:31 +02:00
check ( " struct a { \n "
" a *b() const; \n "
" void c(); \n "
" int d() const; \n "
" }; \n "
" void e(a *x) { \n "
" while (x->b()->d() == 0) \n "
" x->c(); \n "
" x->c(); \n "
" if (x->b()) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-08-15 07:43:50 +02:00
void nullpointer76 ( )
{
check ( " int* foo(int y) { \n "
" std::unique_ptr<int> x = std::make_unique<int>(0); \n "
" if( y == 0 ) \n "
" return x.release(); \n "
" (*x) ++; \n "
" return x.release(); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-08-21 18:52:11 +02:00
void nullpointer77 ( )
{
check ( " bool h(int*); \n "
" void f(int* i) { \n "
" int* i = nullptr; \n "
" if (h(i) && *i == 1) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool h(int*); \n "
" void f(int* i) { \n "
" int* i = nullptr; \n "
" if (h(i)) \n "
" if (*i == 1) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2021-08-23 09:03:48 +02:00
check ( " bool h(int*); \n "
" void f(int* x) { \n "
" int* i = x; \n "
" if (h(i)) \n "
" i = nullptr; \n "
" if (h(i) && *i == 1) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2021-08-21 18:52:11 +02:00
}
2021-08-22 20:17:18 +02:00
void nullpointer78 ( ) // #7802
{
check ( " void f() \n "
" { \n "
" int **pp; \n "
" int *p = 0; \n "
" pp = &p; \n "
" **pp = 1; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:6]: (error) Null pointer dereference: *pp \n " , errout . str ( ) ) ;
}
2021-08-25 20:33:41 +02:00
void nullpointer79 ( ) // #10400
{
check ( " void resize(size_t nF, size_t nT) { \n "
" double* pValues = nullptr; \n "
" if (nF > 0 && nT > 0) \n "
" pValues = new double[nF * nT]; \n "
" for (size_t cc = 0; cc < nF * nT; ++cc) \n "
" pValues[cc] = 42; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-08-29 15:39:41 +02:00
void nullpointer80 ( ) // #10410
{
check ( " int f(int* a, int* b) { \n "
" if( a || b ) { \n "
" int n = a ? *a : *b; \n "
" if( b ) \n "
" n++; \n "
" return n; \n "
" } \n "
" return 0; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-09-04 08:45:55 +02:00
void nullpointer81 ( ) // #8724
{
check ( " void f(A **list) { \n "
" A *tmp_List = NULL; \n "
" *list = NULL; \n "
" while (1) { \n "
" if (*list == NULL) { \n "
" tmp_List = malloc (sizeof (ArchiveList_struct)); \n "
" *list = tmp_List; \n "
" } else { \n "
" tmp_List->next = malloc (sizeof (ArchiveList_struct)); \n "
" } \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-09-21 19:25:41 +02:00
void nullpointer82 ( ) // #10331
{
check ( " bool g(); \n "
" int* h(); \n "
" void f(int* ptr) { \n "
" if (!ptr) { \n "
" if (g()) \n "
" goto done; \n "
" ptr = h(); \n "
" if (!ptr) \n "
" return; \n "
" } \n "
" if (*ptr == 1) \n "
" return; \n "
" \n "
" done: \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-10-05 08:29:23 +02:00
void nullpointer83 ( ) // #9870
{
check ( " int* qux(); \n "
" int* f7c2(int *x) { \n "
" int* p = 0; \n "
" if (nullptr == x) \n "
" p = qux(); \n "
" if (nullptr == x) \n "
" return x; \n "
" *p = 1; \n "
" return x; \n "
" } \n " ) ;
ASSERT_EQUALS ( " [test.cpp:8]: (warning) Possible null pointer dereference: p \n " , errout . str ( ) ) ;
}
2021-10-06 08:39:58 +02:00
void nullpointer84 ( ) // #9873
{
check ( " void f(std::unique_ptr<A> P) { \n "
" A *RP = P.get(); \n "
" if (!RP) { \n "
" P->foo(); \n "
" } \n "
" } \n " ) ;
ASSERT_EQUALS (
" [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition '!RP' is redundant or there is possible null pointer dereference: P. \n " ,
errout . str ( ) ) ;
}
2021-10-15 10:57:40 +02:00
void nullpointer85 ( ) // #10210
{
check ( " struct MyStruct { \n "
" int GetId() const { \n "
" int id = 0; \n "
" int page = m_notebook->GetSelection(); \n "
" if (m_notebook && (m_notebook->GetPageCount() > 0)) \n "
" id = page; \n "
" return id; \n "
" } \n "
" wxNoteBook *m_notebook = nullptr; \n "
" }; \n "
" int f() { \n "
" const MyStruct &s = Get(); \n "
" return s.GetId(); \n "
" } \n " ) ;
ASSERT_EQUALS (
" [test.cpp:5] -> [test.cpp:4]: (warning) Either the condition 'm_notebook' is redundant or there is possible null pointer dereference: m_notebook. \n " ,
errout . str ( ) ) ;
}
2021-11-25 22:34:00 +01:00
void nullpointer86 ( )
{
check ( " struct A { \n "
" A* a() const; \n "
" int b() const; \n "
" }; \n "
" A* f(A* t) { \n "
" if (t->b() == 0) { \n "
" return t; \n "
" } \n "
" return t->a(); \n "
" } \n "
" void g(A* t) { \n "
" t = f(t->a()); \n "
" if (!t->a()) {} \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-12-02 17:28:31 +01:00
void nullpointer87 ( ) // #9291
{
check ( " int f(bool b, int* x) { \n "
" if (b && x == nullptr) \n "
" return 0; \n "
" else if (!b && x == nullptr) \n "
" return 1; \n "
" else if (!b && x != nullptr) \n "
" return *x; \n "
" else \n "
" return *x + 1; \n "
" } \n " ) ;
TODO_ASSERT_EQUALS ( " " , " [test.cpp:6] -> [test.cpp:9]: (warning) Either the condition 'x!=nullptr' is redundant or there is possible null pointer dereference: x. \n " , errout . str ( ) ) ;
check ( " void f(int n, int* p) { \n "
" int* r = nullptr; \n "
" if (n < 0) \n "
" return; \n "
" if (n == 0) \n "
" r = p; \n "
" else if (n > 0) \n "
" r = p + 1; \n "
" *r; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
void nullpointer88 ( ) // #9949
{
check ( " struct S { char **ppc; }; \n "
" int alloc(struct S* s) { \n "
" char** ppc = malloc(4096); \n "
" if (ppc != NULL) { \n "
" s->ppc = ppc; \n "
" return 1; \n "
" } \n "
" return 0; \n "
" } \n "
" void f() { \n "
" struct S* s = malloc(sizeof(struct S)); \n "
" s->ppc = NULL; \n "
" if (alloc(s)) \n "
" s->ppc[0] = \" \" ; \n "
" } \n " , /*inconclusive*/ false , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-12-06 20:06:48 +01:00
void nullpointer89 ( ) // #10640
{
check ( " typedef struct { \n "
" int x; \n "
" } foo_t; \n "
" typedef struct { \n "
" foo_t *y; \n "
" } bar_t; \n "
" void f(bar_t *ptr) { \n "
" if(ptr->y->x) \n "
" if(ptr->y != nullptr) {} \n "
" } \n " ) ;
ASSERT_EQUALS (
" [test.cpp:9] -> [test.cpp:8]: (warning) Either the condition 'ptr->y!=nullptr' is redundant or there is possible null pointer dereference: ptr->y. \n " ,
errout . str ( ) ) ;
}
2021-12-07 08:50:29 +01:00
void nullpointer90 ( ) // #6098
{
check ( " std::string definitionToName(Definition *ctx) \n "
" { \n "
" if (ctx->definitionType()==Definition::TypeMember) \n " // possible null pointer dereference
" { \n "
" return \" y \" ; \n "
" } \n "
" else if (ctx) \n " // ctx is checked against null
" { \n "
" if(ctx->definitionType()!=Definition::TypeMember) \n "
" { \n "
" return \" x \" ; \n "
" } \n "
" } \n "
" return \" unknown \" ; \n "
" } " ) ;
ASSERT_EQUALS (
" [test.cpp:7] -> [test.cpp:3]: (warning) Either the condition 'ctx' is redundant or there is possible null pointer dereference: ctx. \n " ,
errout . str ( ) ) ;
}
2022-01-04 21:19:45 +01:00
void nullpointer91 ( ) // #10678
{
check ( " void f(const char* PBeg, const char* PEnd) { \n "
" while (PEnd != nullptr) { \n "
" const int N = h(PEnd); \n "
" PEnd = g(); \n "
" const int Length = PEnd == nullptr ? 0 : PEnd - PBeg; \n "
" }; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-04-07 06:48:13 +02:00
}
void nullpointer92 ( )
{
check ( " bool g(bool); \n "
" int f(int* i) { \n "
" if (!g(!!i)) return 0; \n "
" return *i; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " bool g(bool); \n "
" int f(int* i) { \n "
" if (!g(!i)) return 0; \n "
" return *i; \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-01-04 21:19:45 +01:00
}
2022-04-10 08:10:11 +02:00
void nullpointer93 ( ) // #3929
{
check ( " int* GetThing( ) { return 0; } \n "
" int main() { \n "
" int* myNull = GetThing(); \n "
" *myNull=42; \n "
" return 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: myNull \n " , errout . str ( ) ) ;
check ( " struct foo { \n "
" int* GetThing(void) { return 0; } \n "
" }; \n "
" int main(void) { \n "
" foo myFoo; \n "
" int* myNull = myFoo.GetThing(); \n "
" *myNull=42; \n "
" return 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:7]: (error) Null pointer dereference: myNull \n " , errout . str ( ) ) ;
}
2015-01-05 14:54:24 +01:00
void nullpointer_addressOf ( ) { // address of
check ( " void f() { \n "
" struct X *x = 0; \n "
" if (addr == &x->y) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" struct X *x = 0; \n "
" if (addr == &x->y.z[0]) {} \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2022-02-02 19:30:49 +01:00
checkP ( " typedef int Count; \n " // #10018
" #define offsetof(TYPE, MEMBER) ((Count) & ((TYPE*)0)->MEMBER) \n "
" struct S { \n "
" int a[20]; \n "
" }; \n "
" int g(int i) { \n "
" return offsetof(S, a[i]); \n "
" } \n " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2015-01-05 14:54:24 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointerSwitch ( ) { // #2626
2021-05-13 20:21:02 +02:00
// extracttests.start: char *do_something();
2014-03-28 10:06:24 +01:00
check ( " char *f(int x) { \n "
" char *p = do_something(); \n "
" switch (x) { \n "
" case 1: \n "
" p = 0; \n "
" case 2: \n "
" *p = 0; \n "
" break; \n "
" } \n "
" return p; \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2019-03-16 09:17:50 +01:00
ASSERT_EQUALS ( " [test.cpp:7]: (warning) Possible null pointer dereference: p \n " , errout . str ( ) ) ;
2014-03-28 10:06:24 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer_cast ( ) { // #4692
2013-04-03 10:27:08 +02:00
check ( " char *nasm_skip_spaces(const char *p) { \n "
" if (p) \n "
" while (*p && nasm_isspace(*p)) \n "
" p++; \n "
2013-04-15 19:00:15 +02:00
" return p; \n "
2013-04-03 10:27:08 +02:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointer_castToVoid ( ) { // #3771
2012-04-30 12:36:41 +02:00
check ( " void f () { \n "
2013-04-15 19:00:15 +02:00
" int *buf; buf = NULL; \n "
" buf; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2019-08-05 16:26:32 +02:00
void nullpointer_subfunction ( ) {
check ( " int f(int* x, int* y) { \n "
" if (!x) \n "
" return; \n "
" return *x + *y; \n "
" } \n "
" void g() { \n "
" f(nullptr, nullptr); \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2010-10-31 11:51:25 +01:00
// Check if pointer is null and the dereference it
2014-11-20 14:20:09 +01:00
void pointerCheckAndDeRef ( ) {
2010-10-31 11:51:25 +01:00
check ( " void foo(char *p) { \n "
" if (!p) { \n "
" } \n "
" *p = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:4]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2010-11-12 19:42:02 +01:00
2011-02-19 21:28:18 +01:00
check ( " void foo(char *p) { \n "
" if (p && *p == 0) { \n "
" } \n "
" printf( \" %c \" , *p); \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-02-19 21:28:18 +01:00
2011-02-20 14:38:49 +01:00
check ( " void foo(char *p) { \n "
" if (p && *p == 0) { \n "
" } else { *p = 0; } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-02-20 14:38:49 +01:00
2011-07-31 17:19:23 +02:00
check ( " void foo(char *p) { \n "
" if (p) { \n "
" } \n "
" strcpy(p, \" abc \" ); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-07-31 17:19:23 +02:00
2011-07-31 17:32:25 +02:00
check ( " void foo(char *p) { \n "
" if (p) { \n "
" } \n "
" bar(); \n "
" strcpy(p, \" abc \" ); \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:5]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-07-31 17:32:25 +02:00
2010-10-31 11:51:25 +01:00
check ( " void foo(abc *p) { \n "
" if (!p) { \n "
" } \n "
2013-04-15 19:00:15 +02:00
" else { if (!p->x) { \n "
" } } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-13 10:49:17 +02:00
{
static const char code [ ] =
" void foo(char *p) { \n "
" if (!p) { \n "
" abort(); \n "
" } \n "
" *p = 0; \n "
" } " ;
check ( code , false ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
2013-04-13 10:49:17 +02:00
check ( code , true ) ;
2014-03-21 17:36:48 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-13 10:49:17 +02:00
}
2011-10-29 10:35:31 +02:00
check ( " void foo(char *p) { \n "
" if (!p) { \n "
2011-01-05 21:48:26 +01:00
" (*bail)(); \n "
" } \n "
" *p = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-01-05 21:48:26 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
check ( " void foo(char *p) { \n "
" if (!p) { \n "
" throw x; \n "
" } \n "
" *p = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-09-03 19:43:27 +02:00
check ( " void foo(char *p) { \n "
" if (!p) { \n "
" ab.abort(); \n "
" } \n "
" *p = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2011-09-03 19:43:27 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 21:09:32 +01:00
check ( " void foo(char *p) { \n "
2010-10-31 11:51:25 +01:00
" if (!p) { \n "
" switch (x) { } \n "
" } \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-11-04 21:09:32 +01:00
check ( " void foo(char *p) { \n "
2010-11-01 19:21:08 +01:00
" if (!p) { \n "
" } \n "
" return *x; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2010-11-01 19:21:08 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-01-25 15:16:22 +01:00
check ( " int foo(int *p) { \n "
" if (!p) { \n "
" x = *p; \n "
" return 5+*p; \n "
" } \n "
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n "
" [test.cpp:2] -> [test.cpp:4]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2012-01-25 15:16:22 +01:00
2010-11-04 21:09:32 +01:00
// operator!
check ( " void f() { \n "
" A a; \n "
" if (!a) { \n "
" a.x(); \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-11-04 21:09:32 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
// This is why this check can't be used on the simplified token list
check ( " void f(Foo *foo) { \n "
" if (!dynamic_cast<bar *>(foo)) { \n "
" *foo = 0; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2010-10-31 11:51:25 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-12-16 21:41:50 +01:00
// ticket: #2300 - calling unknown function that may initialize the pointer
check ( " Fred *fred; \n "
" void a() { \n "
" if (!fred) { \n "
" initfred(); \n "
" fred->x = 0; \n "
" } \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2010-12-16 21:41:50 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-09 20:16:16 +01:00
// ticket #1219
check ( " void foo(char *p) { \n "
" if (p) { \n "
" return; \n "
" } \n "
" *p = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:5]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-01-17 19:23:00 +01:00
// #2467 - unknown macro may terminate the application
check ( " void f(Fred *fred) { \n "
" if (fred == NULL) { \n "
" MACRO; \n "
" } \n "
" fred->a(); \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-01-17 19:23:00 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-01-23 08:38:09 +01:00
// #2493 - switch
check ( " void f(Fred *fred) { \n "
" if (fred == NULL) { \n "
" x = 0; \n "
" } \n "
" switch (x) { \n "
" case 1: \n "
" fred->a(); \n "
" break; \n "
" }; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-01-23 08:38:09 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-02-18 20:05:44 +01:00
2012-09-18 19:16:28 +02:00
// #4118 - second if
check ( " void f(char *p) { \n "
" int x = 1; \n "
" if (!p) x = 0; \n "
" if (x) *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-27 11:04:58 +02:00
// #2674 - different functions
check ( " class Fred { \n "
" public: \n "
" Wilma *wilma; \n "
" void a(); \n "
" void b(); \n "
" }; \n "
" \n "
" void Fred::a() { \n "
" if ( wilma ) { } \n "
" } \n "
" \n "
" void Fred::b() { \n "
" wilma->Reload(); \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2011-03-27 11:04:58 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-03-27 21:29:49 +02:00
check ( " void test(int *i) { \n "
2013-04-15 19:00:15 +02:00
" if(i == NULL) { } \n "
" else { \n "
2011-03-27 21:29:49 +02:00
" int b = *i; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2011-03-27 21:29:49 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-04-03 21:06:42 +02:00
// #2696 - false positives nr 1
check ( " void f() \n "
" { \n "
" struct foo *pFoo = NULL; \n "
" size_t len; \n "
" \n "
" len = sizeof(*pFoo) - sizeof(pFoo->data); \n "
" \n "
" if (pFoo) \n "
" bar(); \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2011-04-03 21:06:42 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// #2696 - false positives nr 2
check ( " void f() \n "
" { \n "
" struct foo *pFoo = NULL; \n "
" size_t len; \n "
" \n "
" while (pFoo) \n "
" pFoo = pFoo->next; \n "
" \n "
" len = sizeof(pFoo->data); \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2011-04-03 21:06:42 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// #2696 - false positives nr 3
check ( " void f() \n "
" { \n "
" struct foo *pFoo = NULL; \n "
" size_t len; \n "
" \n "
" while (pFoo) \n "
" pFoo = pFoo->next; \n "
" \n "
" len = decltype(*pFoo); \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2011-04-03 21:06:42 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-07-25 21:58:00 +02:00
check ( " int foo(struct Fred *fred) { \n "
2013-04-15 19:00:15 +02:00
" if (fred) { } \n "
2011-07-25 21:58:00 +02:00
" return fred->a; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'fred' is redundant or there is possible null pointer dereference: fred. \n " , errout . str ( ) ) ;
2011-08-01 07:51:32 +02:00
// #2789 - assign and check pointer
check ( " void f() { \n "
2013-04-15 19:00:15 +02:00
" char *p; p = x(); \n "
" if (!p) { } \n "
2011-08-01 07:51:32 +02:00
" *p = 0; \n "
2013-03-20 15:36:16 +01:00
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2011-08-07 17:06:25 +02:00
// check, assign and use
check ( " void f() { \n "
" char *p; \n "
" if (p == 0 && (p = malloc(10)) != 0) { \n "
" *p = 0; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-08-07 17:06:25 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-29 15:48:54 +02:00
2012-02-11 16:15:38 +01:00
// check, assign and use
check ( " void f() { \n "
" char *p; \n "
" if (p == 0 && (p = malloc(10)) != a && (*p = a)) { \n "
" *p = 0; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2012-02-11 16:15:38 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// check, and use
check ( " void f() { \n "
" char *p; \n "
" if (p == 0 && (*p = 0)) { \n "
" return; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:3]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2012-02-11 16:15:38 +01:00
// check, and use
check ( " void f() { \n "
" struct foo *p; \n "
" if (p == 0 && p->x == 10) { \n "
" return; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2015-07-29 19:54:57 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:3]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2012-02-11 16:15:38 +01:00
// check, and use
check ( " void f() { \n "
" struct foo *p; \n "
" if (p == 0 || p->x == 10) { \n "
" return; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2012-02-11 16:15:38 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// check, and use
check ( " void f() { \n "
2013-04-15 19:00:15 +02:00
" char *p; p = malloc(10); \n "
" if (p == NULL && (*p = a)) { \n "
2012-02-11 16:15:38 +01:00
" return; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2016-10-18 21:44:02 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:3]: (warning) Either the condition 'p==NULL' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2012-02-11 16:15:38 +01:00
// check, and use
check ( " void f(struct X *p, int x) { \n "
" if (!p && x==1 || p && p->x==0) { \n "
" return; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2012-02-11 16:15:38 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-29 15:48:54 +02:00
{
const char code [ ] = " void f(Fred *fred) { \n "
" if (fred == NULL) { } \n "
" fred->x(); \n "
" } " ;
2020-05-21 08:47:48 +02:00
check ( code ) ; // inconclusive
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'fred==NULL' is redundant or there is possible null pointer dereference: fred. \n " , errout . str ( ) ) ;
2011-10-29 15:48:54 +02:00
}
2011-12-03 13:10:07 +01:00
check ( " void f(char *s) { \n " // #3358
" if (s==0); \n "
" strcpy(a, s?b:c); \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-12-03 13:10:07 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-17 12:44:11 +01:00
// sizeof
check ( " void f(struct fred_t *fred) { \n "
" if (!fred) \n "
2013-04-15 19:00:15 +02:00
" int sz = sizeof(fred->x); \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2011-12-17 12:44:11 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-18 13:33:23 +01:00
// check in macro
check ( " void f(int *x) { \n "
" $if (!x) {} \n "
" *x = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-12-25 14:23:50 +01:00
// return ?:
check ( " int f(ABC *p) { \n " // FP : return ?:
" if (!p) {} \n "
" return p ? p->x : 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " int f(ABC *p) { \n " // no fn
" if (!p) {} \n "
" return q ? p->x : 0; \n "
" } " ) ;
2020-02-13 16:27:06 +01:00
TODO_ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , " " , errout . str ( ) ) ;
2014-04-02 19:39:42 +02:00
check ( " int f(ABC *p) { \n " // FP : return &&
" if (!p) {} \n "
" return p && p->x; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-09-02 12:03:05 +02:00
check ( " void f(int x, int *p) { \n "
" if (x || !p) {} \n "
" *p = 0; \n "
" } " ) ;
2017-05-15 20:05:11 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
2015-04-20 13:46:02 +02:00
// sizeof
check ( " void f() { \n "
" int *pointer = NULL; \n "
" pointer = func(sizeof pointer[0]); \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2015-04-20 13:46:02 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2010-10-31 11:51:25 +01:00
}
2010-12-26 19:29:58 +01:00
// Test CheckNullPointer::nullConstantDereference
2014-11-20 14:20:09 +01:00
void nullConstantDereference ( ) {
2021-05-13 20:21:02 +02:00
check ( " int f() { \n "
2012-01-25 15:16:22 +01:00
" int* p = 0; \n "
" return p[4]; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
2013-06-07 06:37:53 +02:00
2013-10-12 15:19:15 +02:00
check ( " void f() { \n "
" typeof(*NULL) y; \n "
" } " , true ) ;
2013-11-08 13:22:07 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-11-27 19:41:36 +01:00
check ( " int * f() { \n "
" return NULL; \n "
" } \n "
" int main() { \n "
" return *f(); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:5]: (error) Null pointer dereference: f() \n " , errout . str ( ) ) ;
2010-12-26 19:29:58 +01:00
}
2014-11-20 14:20:09 +01:00
void gcc_statement_expression ( ) {
2011-03-13 10:34:54 +01:00
// Ticket #2621
check ( " void f(struct ABC *abc) { \n "
" ({ if (abc) dbg(); }) \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-06-26 21:46:33 +02:00
2014-11-20 14:20:09 +01:00
void snprintf_with_zero_size ( ) {
2011-06-26 21:46:33 +02:00
// Ticket #2840
check ( " void f() { \n "
" int bytes = snprintf(0, 0, \" %u \" , 1); \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2011-06-26 21:46:33 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void snprintf_with_non_zero_size ( ) {
2011-06-26 21:46:33 +02:00
// Ticket #2840
check ( " void f() { \n "
" int bytes = snprintf(0, 10, \" %u \" , 1); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
}
2011-10-12 22:11:27 +02:00
2014-11-20 14:20:09 +01:00
void printf_with_invalid_va_argument ( ) {
2011-10-12 22:11:27 +02:00
check ( " void f() { \n "
" printf( \" %s \" , 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
check ( " void f(char* s) { \n "
" printf( \" %s \" , s); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" char* s = 0; \n "
" printf( \" %s \" , s); \n "
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: s \n " , errout . str ( ) ) ;
2011-10-12 22:11:27 +02:00
2011-12-03 13:10:07 +01:00
check ( " void f() { \n "
" char *s = 0; \n "
" printf( \" %s \" , s == 0 ? a : s); \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2011-12-03 13:10:07 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-12 22:11:27 +02:00
check ( " void f() { \n "
" printf( \" %u%s \" , 0, 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
check ( " void f(char* s) { \n "
" printf( \" %u%s \" , 0, s); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" char* s = 0; \n "
" printf( \" %u%s \" , 123, s); \n "
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: s \n " , errout . str ( ) ) ;
2011-10-12 22:11:27 +02:00
check ( " void f() { \n "
" printf( \" %%%s%% \" , 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
check ( " void f(char* s) { \n "
" printf( \" text: %s, %s \" , s, 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" char* s = \" blabla \" ; \n "
" printf( \" %s \" , s); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-28 21:32:07 +01:00
check ( " void f(char* s) { \n "
" printf( \" text: %m%s, %s \" , s, 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
check ( " void f(char* s) { \n "
" printf( \" text: %*s, %s \" , s, 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
2011-11-30 20:23:29 +01:00
// Ticket #3364
check ( " void f() { \n "
" printf( \" %-*.*s \" , s, 0); \n "
" sprintf( \" %* \" , s); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-10-12 22:11:27 +02:00
}
2014-11-20 14:20:09 +01:00
void scanf_with_invalid_va_argument ( ) {
2011-10-12 22:11:27 +02:00
check ( " void f(char* s) { \n "
" sscanf(s, \" %s \" , 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
check ( " void f() { \n "
" scanf( \" %d \" , 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
2011-10-16 07:06:18 +02:00
check ( " void f(char* foo) { \n "
" char location[200]; \n "
" int width, height; \n "
" sscanf(imgInfo, \" %s %d %d \" , location, &width, &height); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // ticket #3207
check ( " void f(char *dummy) { \n "
" int iVal; \n "
" sscanf(dummy, \" %d%c \" , &iVal); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ; // ticket #3211
check ( " void f(char *dummy) { \n "
" int* iVal = 0; \n "
" sscanf(dummy, \" %d \" , iVal); \n "
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: iVal \n " , errout . str ( ) ) ;
2011-10-16 07:06:18 +02:00
check ( " void f(char *dummy) { \n "
" int* iVal; \n "
" sscanf(dummy, \" %d \" , foo(iVal)); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(char *dummy) { \n "
" int* iVal = 0; \n "
" sscanf(dummy, \" %d%d \" , foo(iVal), iVal); \n "
" } " ) ;
2014-01-22 20:16:31 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-02 17:09:32 +01:00
check ( " void f(char* dummy) { \n "
" sscanf(dummy, \" %*d%u \" , 0); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
2011-10-12 22:11:27 +02:00
}
2011-10-23 23:36:57 +02:00
2014-11-20 14:20:09 +01:00
void nullpointer_in_return ( ) {
2021-05-13 20:21:02 +02:00
// extracttests.start: int maybe(); int *g();
2011-10-23 23:36:57 +02:00
check ( " int foo() { \n "
" int* iVal = 0; \n "
2021-05-13 20:21:02 +02:00
" if(maybe()) iVal = g(); \n "
2011-10-23 23:36:57 +02:00
" return iVal[0]; \n "
" } " ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (warning) Possible null pointer dereference: iVal \n " , errout . str ( ) ) ;
2011-10-23 23:36:57 +02:00
check ( " int foo(int* iVal) { \n "
" return iVal[0]; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2011-10-23 23:36:57 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-11-04 09:49:49 +01:00
2014-11-20 14:20:09 +01:00
void nullpointer_in_typeid ( ) {
2011-11-04 09:49:49 +01:00
// Should throw std::bad_typeid
check ( " struct PolymorphicA { virtual ~A() {} }; \n "
" bool foo() { \n "
" PolymorphicA* a = 0; \n "
" return typeid(*a) == typeid(*a); \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2011-11-11 09:58:17 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-04 09:49:49 +01:00
check ( " struct NonPolymorphicA { ~A() {} }; \n "
" bool foo() { \n "
" NonPolymorphicA* a = 0; \n "
" return typeid(*a) == typeid(*a); \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2011-11-11 09:58:17 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-04 09:49:49 +01:00
check ( " bool foo() { \n "
" char* c = 0; \n "
" return typeid(*c) == typeid(*c); \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2011-11-11 09:58:17 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-11-04 09:49:49 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer_in_for_loop ( ) {
2011-11-07 21:10:21 +01:00
// Ticket #3278
check ( " void f(int* ptr, int cnt){ \n "
" if (!ptr) \n "
" cnt = 0; \n "
" for (int i = 0; i < cnt; ++i) \n "
" *ptr++ = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-11-13 22:35:13 +01:00
2014-11-20 14:20:09 +01:00
void nullpointerDelete ( ) {
2011-11-13 22:35:13 +01:00
check ( " void f() { \n "
" K *k = getK(); \n "
" if (k) \n "
" k->doStuff(); \n "
" delete k; \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f() { \n "
" K *k = getK(); \n "
" if (k) \n "
" k[0] = ptr; \n "
" delete [] k; \n "
" k = new K[10]; \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-11-23 21:19:54 +01:00
2019-09-20 15:07:27 +02:00
void nullpointerSubFunction ( ) {
2019-09-20 15:06:37 +02:00
check ( " void g(int* x) { *x; } \n "
" void f(int* x) { \n "
" if (x) \n "
" g(x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-20 15:06:37 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2014-11-20 14:20:09 +01:00
void nullpointerExit ( ) {
2011-11-23 21:19:54 +01:00
check ( " void f() { \n "
" K *k = getK(); \n "
" if (!k) \n "
" exit(1); \n "
" k->f(); \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2011-12-25 17:01:45 +01:00
2014-11-20 14:20:09 +01:00
void nullpointerStdString ( ) {
2012-01-25 15:16:22 +01:00
check ( " void f(std::string s1) { \n "
" void* p = 0; \n "
" s1 = 0; \n "
2014-09-26 20:40:44 +02:00
" s1 = ' \\ 0'; \n "
2012-01-25 15:16:22 +01:00
" std::string s2 = 0; \n "
2014-09-26 20:40:44 +02:00
" std::string s2 = ' \\ 0'; \n "
2012-01-25 15:16:22 +01:00
" std::string s3(0); \n "
" foo(std::string(0)); \n "
" s1 = p; \n "
" std::string s4 = p; \n "
" std::string s5(p); \n "
" foo(std::string(p)); \n "
" } " , true ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:9]: (error) Null pointer dereference: p \n "
" [test.cpp:10]: (error) Null pointer dereference: p \n "
2018-12-29 09:26:57 +01:00
" [test.cpp:11]: (error) Null pointer dereference: p \n "
" [test.cpp:12]: (warning, inconclusive) Possible null pointer dereference: p \n "
2014-10-16 19:12:02 +02:00
" [test.cpp:3]: (error) Null pointer dereference \n "
" [test.cpp:5]: (error) Null pointer dereference \n "
2014-09-26 20:40:44 +02:00
" [test.cpp:7]: (error) Null pointer dereference \n "
" [test.cpp:8]: (error) Null pointer dereference \n "
2014-01-22 20:16:31 +01:00
, errout . str ( ) ) ;
2012-01-25 15:16:22 +01:00
2017-02-24 19:44:28 +01:00
check ( " void f(std::string s1) { \n "
" s1 = nullptr; \n "
" std::string s2 = nullptr; \n "
" std::string s3(nullptr); \n "
" foo(std::string(nullptr)); \n "
" } " , true ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n "
" [test.cpp:3]: (error) Null pointer dereference \n "
" [test.cpp:4]: (error) Null pointer dereference \n "
" [test.cpp:5]: (error) Null pointer dereference \n "
, errout . str ( ) ) ;
check ( " void f(std::string s1) { \n "
" s1 = NULL; \n "
" std::string s2 = NULL; \n "
" std::string s3(NULL); \n "
" foo(std::string(NULL)); \n "
" } " , true ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (error) Null pointer dereference \n "
" [test.cpp:3]: (error) Null pointer dereference \n "
" [test.cpp:4]: (error) Null pointer dereference \n "
" [test.cpp:5]: (error) Null pointer dereference \n "
, errout . str ( ) ) ;
2012-01-25 15:16:22 +01:00
check ( " void f(std::string s1, const std::string& s2, const std::string* s3) { \n "
" void* p = 0; \n "
2013-04-15 19:00:15 +02:00
" if (x) { return; } \n "
2012-01-25 15:16:22 +01:00
" foo(s1 == p); \n "
" foo(s2 == p); \n "
" foo(s3 == p); \n "
" foo(p == s1); \n "
" foo(p == s2); \n "
" foo(p == s3); \n "
" } " , true ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n "
" [test.cpp:5]: (error) Null pointer dereference: p \n "
" [test.cpp:7]: (error) Null pointer dereference: p \n "
" [test.cpp:8]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
2012-01-26 16:50:59 +01:00
2015-09-13 10:53:05 +02:00
check ( " void f(std::string s1, const std::string& s2, const std::string* s3) { \n "
" void* p = 0; \n "
" if (x) { return; } \n "
" foo(0 == s1.size()); \n "
" foo(0 == s2.size()); \n "
" foo(0 == s3->size()); \n "
" foo(s1.size() == 0); \n "
" foo(s2.size() == 0); \n "
" foo(s3->size() == 0); \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(std::string s1, const std::string& s2) { \n "
" if (x) { return; } \n "
" foo(0 == s1[0]); \n "
" foo(0 == s2[0]); \n "
" foo(s1[0] == 0); \n "
" foo(s2[0] == 0); \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(std::string s1, const std::string& s2) { \n "
" if (x) { return; } \n "
" foo(s1 == ' \\ 0'); \n "
" foo(s2 == ' \\ 0'); \n "
" foo(' \\ 0' == s1); \n "
" foo(' \\ 0' == s2); \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-01-26 16:50:59 +01:00
check ( " class Bar { \n "
" std::string s; \n "
" Bar() : s(0) {} \n "
" }; \n "
" class Foo { \n "
" std::string s; \n "
" Foo(); \n "
" }; \n "
" Foo::Foo() : s(0) {} " ) ;
2012-04-23 18:26:27 +02:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference \n "
" [test.cpp:9]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
2012-04-02 12:12:02 +02:00
check ( " void f() { \n "
" std::string s = 0 == x ? \" a \" : \" b \" ; \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-08-24 21:50:19 +02:00
check ( " void f() { \n "
" const std::string s = g(); \n "
" ASSERT_MESSAGE( \" Error on s \" , 0 == s.compare( \" Some text \" )); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2017-02-24 19:10:34 +01:00
check ( " void foo(int i, std::string s); \n "
" void bar() { \n "
" foo(0, \" \" ); \n "
" foo(0, 0); \n "
" foo(var, 0); \n "
2017-02-24 19:44:28 +01:00
" foo(var, NULL); \n "
" foo(var, nullptr); \n "
2017-02-24 19:10:34 +01:00
" foo(0, var); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference \n "
2017-02-24 19:44:28 +01:00
" [test.cpp:5]: (error) Null pointer dereference \n "
" [test.cpp:6]: (error) Null pointer dereference \n "
" [test.cpp:7]: (error) Null pointer dereference \n " , errout . str ( ) ) ;
2021-11-29 19:12:07 +01:00
check ( " std::string f() { \n " // #9827
" char* p = NULL; \n "
2022-02-02 19:29:01 +01:00
" int r = g(p); \n "
" if (!r) \n "
" return \" \" ; \n "
2021-11-29 19:12:07 +01:00
" std::string s(p); \n "
" return s; \n "
" } \n " , /*inconclusive*/ true ) ;
2022-02-02 19:29:01 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-01-25 15:16:22 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointerStdStream ( ) {
2012-03-16 17:24:03 +01:00
check ( " void f(std::ifstream& is) { \n "
" char* p = 0; \n "
" is >> p; \n "
" } " ) ;
2014-05-17 16:44:44 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:3]: (error) Possible null pointer dereference: p \n " , " " , errout . str ( ) ) ;
2012-03-16 17:24:03 +01:00
check ( " void f(const std::ostringstream& oss, char* q) { \n "
" char const* p = 0; \n " // Simplification makes detection of bug difficult
" oss << p; \n "
" oss << foo << p; \n "
" if(q == 0) \n "
" oss << foo << q; \n "
2015-07-25 14:17:55 +02:00
" } " , false ) ;
2016-01-30 20:43:21 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (error) Null pointer dereference: p \n "
" [test.cpp:4]: (error) Null pointer dereference: p \n "
2017-05-15 20:05:11 +02:00
" [test.cpp:5] -> [test.cpp:6]: (warning) Either the condition 'q==0' is redundant or there is possible null pointer dereference: q. \n " , errout . str ( ) ) ;
2012-03-16 17:24:03 +01:00
check ( " void f(const char* p) { \n "
" if(p == 0) { \n "
" std::cout << p; \n "
" std::cerr << p; \n "
" std::cin >> p; \n "
" std::cout << abc << p; \n "
" } \n "
2015-07-25 14:17:55 +02:00
" } " , false ) ;
2017-05-15 20:05:11 +02:00
TODO_ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n "
" [test.cpp:2] -> [test.cpp:4]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n "
" [test.cpp:2] -> [test.cpp:5]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n "
" [test.cpp:2] -> [test.cpp:6]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n " ,
" [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n "
" [test.cpp:2] -> [test.cpp:4]: (warning) Either the condition 'p==0' is redundant or there is possible null pointer dereference: p. \n " ,
2014-06-22 10:02:14 +02:00
errout . str ( ) ) ;
2012-03-16 17:24:03 +01:00
check ( " void f() { \n "
" void* p1 = 0; \n "
" std::cout << p1; \n " // No char*
" char* p2 = 0; \n "
" std::cin >> (int)p; \n " // result casted
" std::cout << (int)p; \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2012-03-16 17:24:03 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(const std::string& str) { \n "
" long long ret = 0; \n "
" std::istringstream istr(str); \n "
" istr >> std::hex >> ret; \n " // Read integer
" return ret; \n "
2012-04-30 12:36:41 +02:00
" } " , true ) ;
2012-03-16 17:24:03 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-11-10 19:53:20 +01:00
check ( " void f(int* i) { \n "
" if(i) return; \n "
" std::cout << i; \n " // Its no char* (#4240)
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-05-17 18:18:20 +02:00
2016-11-27 11:40:42 +01:00
// #5811 false positive: (error) Null pointer dereference
2014-05-17 18:18:20 +02:00
check ( " using namespace std; \n "
" std::string itoip(int ip) { \n "
" stringstream out; \n "
" out << ((ip >> 0) & 0xFF); \n "
" return out.str(); \n "
2022-02-27 09:03:24 +01:00
" } " , true ) ;
2014-05-17 18:18:20 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-05-17 19:14:29 +02:00
// avoid regression from first fix attempt for #5811...
check ( " void deserialize(const std::string &data) { \n "
" std::istringstream iss(data); \n "
" unsigned int len = 0; \n "
" if (!(iss >> len)) \n "
" return; \n "
2015-07-25 14:17:55 +02:00
" } \n " , true ) ;
2014-05-17 19:14:29 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-03-16 17:24:03 +01:00
}
2019-04-26 11:30:09 +02:00
void nullpointerSmartPointer ( ) {
2021-05-13 20:21:02 +02:00
// extracttests.start: void dostuff(int);
2019-04-26 11:30:09 +02:00
check ( " struct Fred { int x; }; \n "
" void f(std::shared_ptr<Fred> p) { \n "
" if (p) {} \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f(std::shared_ptr<Fred> p) { \n "
" p = nullptr; \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f(std::unique_ptr<Fred> p) { \n "
" if (p) {} \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f(std::unique_ptr<Fred> p) { \n "
" p = nullptr; \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f() { \n "
" std::shared_ptr<Fred> p; \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f(std::shared_ptr<Fred> p) { \n "
" p.reset(); \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f(std::shared_ptr<Fred> p) { \n "
" Fred * pp = nullptr; \n "
" p.reset(pp); \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f(Fred& f) { \n "
" std::shared_ptr<Fred> p; \n "
" p.reset(&f); \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f(std::shared_ptr<Fred> p) { \n "
2021-05-13 20:21:02 +02:00
" p.reset(); \n "
2019-04-26 11:30:09 +02:00
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
check ( " struct Fred { int x; }; \n "
" void f() { \n "
" std::shared_ptr<Fred> p(nullptr); \n "
" dostuff(p->x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-04-26 11:30:09 +02:00
ASSERT_EQUALS ( " [test.cpp:4]: (error) Null pointer dereference: p \n " , errout . str ( ) ) ;
2019-05-05 09:51:36 +02:00
check ( " struct A {}; \n "
" void f(int n) { \n "
" std::unique_ptr<const A*[]> p; \n "
" p.reset(new const A*[n]); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-05-05 09:51:36 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-07-23 22:45:58 +02:00
// #9216
check ( " struct A { \n "
" void reset(); \n "
" void f(); \n "
" }; \n "
" void g(std::unique_ptr<A> var) { \n "
" var->reset(); \n "
" var->f(); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-07-23 22:45:58 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-11-10 09:44:59 +01:00
// #9439
check ( " char* g(); \n "
" char* f() { \n "
" std::unique_ptr<char> x(g()); \n "
" if( x ) {} \n "
" return x.release(); \n "
" } \n " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2020-09-09 04:54:38 +02:00
// #9496
check ( " std::shared_ptr<int> f() { \n "
" return std::shared_ptr<int>(nullptr); \n "
" } \n "
" void g() { \n "
" int a = *f(); \n "
2020-09-09 04:55:53 +02:00
" } \n " ,
true ) ;
2020-09-09 04:54:38 +02:00
ASSERT_EQUALS ( " [test.cpp:5]: (error) Null pointer dereference: f() \n " , errout . str ( ) ) ;
2019-04-26 11:30:09 +02:00
}
2014-11-20 14:20:09 +01:00
void functioncall ( ) { // #3443 - function calls
2011-12-25 17:01:45 +01:00
// dereference pointer and then check if it's null
{
// function not seen
check ( " void f(int *p) { \n "
" *p = 0; \n "
" foo(p); \n "
" if (p) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// function seen (taking pointer parameter)
check ( " void foo(int *p) { } \n "
" \n "
" void f(int *p) { \n "
" *p = 0; \n "
" foo(p); \n "
" if (p) { } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2011-12-25 17:01:45 +01:00
2012-09-06 18:33:15 +02:00
// function seen (taking reference parameter)
check ( " void foo(int *&p) { } \n "
" \n "
" void f(int *p) { \n "
" *p = 0; \n "
" foo(p); \n "
" if (p) { } \n "
" } " , true ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2011-12-25 17:01:45 +01:00
// function implementation not seen
check ( " void foo(int *p); \n "
" \n "
" void f(int *p) { \n "
" *p = 0; \n "
" foo(p); \n "
" if (p) { } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2011-12-26 07:13:10 +01:00
// inconclusive
check ( " void f(int *p) { \n "
" *p = 0; \n "
" foo(p); \n "
" if (p) { } \n "
" } " , true ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:4] -> [test.cpp:2]: (warning, inconclusive) Either the condition 'p' is redundant or there is possible null pointer dereference: p. \n " ,
errout . str ( ) ) ;
2011-12-25 17:01:45 +01:00
}
// dereference struct pointer and then check if it's null
{
// function not seen
check ( " void f(struct ABC *abc) { \n "
" abc->a = 0; \n "
" foo(abc); \n "
" if (abc) { } \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
// function seen (taking pointer parameter)
check ( " void foo(struct ABC *abc) { } \n "
" \n "
" void f(struct ABC *abc) { \n "
" abc->a = 0; \n "
" foo(abc); \n "
" if (abc) { } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc. \n " ,
errout . str ( ) ) ;
2011-12-25 17:01:45 +01:00
// function implementation not seen
check ( " void foo(struct ABC *abc); \n "
" \n "
" void f(struct ABC *abc) { \n "
" abc->a = 0; \n "
" foo(abc); \n "
" if (abc) { } \n "
" } " ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:6] -> [test.cpp:4]: (warning) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc. \n " ,
errout . str ( ) ) ;
2011-12-26 07:13:10 +01:00
// inconclusive
check ( " void f(struct ABC *abc) { \n "
" abc->a = 0; \n "
" foo(abc); \n "
" if (abc) { } \n "
" } " , true ) ;
2021-01-08 22:55:04 +01:00
ASSERT_EQUALS (
" [test.cpp:4] -> [test.cpp:2]: (warning, inconclusive) Either the condition 'abc' is redundant or there is possible null pointer dereference: abc. \n " ,
errout . str ( ) ) ;
2011-12-25 17:01:45 +01:00
}
}
2012-01-31 15:49:34 +01:00
2014-11-20 14:20:09 +01:00
void functioncalllibrary ( ) {
2014-04-02 19:39:42 +02:00
Settings settings1 ;
Tokenizer tokenizer ( & settings1 , this ) ;
2015-03-14 19:23:33 +01:00
std : : istringstream code ( " void f() { int a,b,c; x(a,b,c); } " ) ;
2021-11-29 07:34:39 +01:00
ASSERT_EQUALS ( true , tokenizer . tokenize ( code , " test.c " ) ) ;
2013-07-15 21:57:58 +02:00
const Token * xtok = Token : : findsimplematch ( tokenizer . tokens ( ) , " x " ) ;
// nothing bad..
{
Library library ;
2013-07-22 20:21:45 +02:00
Library : : ArgumentChecks arg ;
2016-12-06 12:31:16 +01:00
library . functions [ " x " ] . argumentChecks [ 1 ] = arg ;
library . functions [ " x " ] . argumentChecks [ 2 ] = arg ;
library . functions [ " x " ] . argumentChecks [ 3 ] = arg ;
2013-07-15 21:57:58 +02:00
2017-02-24 19:10:34 +01:00
std : : list < const Token * > null ;
CheckNullPointer : : parseFunctionCall ( * xtok , null , & library ) ;
2013-07-15 21:57:58 +02:00
ASSERT_EQUALS ( 0U , null . size ( ) ) ;
}
// for 1st parameter null pointer is not ok..
{
Library library ;
2013-12-26 17:48:25 +01:00
Library : : ArgumentChecks arg ;
2016-12-06 12:31:16 +01:00
library . functions [ " x " ] . argumentChecks [ 1 ] = arg ;
library . functions [ " x " ] . argumentChecks [ 2 ] = arg ;
library . functions [ " x " ] . argumentChecks [ 3 ] = arg ;
library . functions [ " x " ] . argumentChecks [ 1 ] . notnull = true ;
2013-07-15 21:57:58 +02:00
2017-02-24 19:10:34 +01:00
std : : list < const Token * > null ;
CheckNullPointer : : parseFunctionCall ( * xtok , null , & library ) ;
2013-07-15 21:57:58 +02:00
ASSERT_EQUALS ( 1U , null . size ( ) ) ;
ASSERT_EQUALS ( " a " , null . front ( ) - > str ( ) ) ;
2015-03-14 19:23:33 +01:00
}
2013-07-15 21:57:58 +02:00
}
2012-11-21 08:56:17 +01:00
2014-11-20 14:20:09 +01:00
void functioncallDefaultArguments ( ) {
2012-11-21 08:56:17 +01:00
check ( " void f(int *p = 0) { \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ;
2013-04-25 09:25:56 +02:00
check ( " void f(int *p = 0) { \n "
" if (!p) \n "
" return; \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-11-21 08:56:17 +01:00
check ( " void f(char a, int *p = 0) { \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" printf( \" p = %d \" , *p); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" printf( \" p[1] = %d \" , p[1]); \n "
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ;
2016-01-20 08:42:58 +01:00
check ( " void f(int *p = 0) { \n "
" buf[p] = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2012-11-21 08:56:17 +01:00
check ( " void f(int *p = 0) { \n "
" if (p != 0 && bar()) \n "
" *p = 0; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2012-11-21 08:56:17 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p) { \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" if (p != 0) \n "
" *p = 0; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2012-11-21 08:56:17 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-01-10 08:22:54 +01:00
check ( " void f(int *p = 0) { \n "
" int y; \n "
" if (p == 0) \n "
" p = &y; \n "
" *p = 0; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-01-10 08:22:54 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-08-11 15:39:37 +02:00
check ( " void f(int a, int *p = 0) { \n "
2012-11-21 08:56:17 +01:00
" if (a != 0) \n "
" *p = 0; \n "
2015-07-25 14:17:55 +02:00
" } " , true ) ;
2020-01-05 16:25:33 +01:00
ASSERT_EQUALS (
" [test.cpp:3]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " ,
errout . str ( ) ) ;
2012-11-21 08:56:17 +01:00
check ( " void f(int *p = 0) { \n "
" p = a; \n "
2013-04-15 19:00:15 +02:00
" *p = 0; \n " // <- don't simplify and verify
2015-07-25 14:17:55 +02:00
" } " ) ;
2012-11-21 08:56:17 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" p += a; \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-02-28 08:45:21 +01:00
check ( " int f(int *p = 0) { \n "
" if (p == 0) { \n "
" return 0; \n "
" } \n "
" return *p; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-02-28 08:45:21 +01:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
2014-05-22 19:48:00 +02:00
" std::cout << p ? *p : 0; \n " // Due to operator precedence, this is equivalent to: (std::cout << p) ? *p : 0;
" } " ) ;
2015-05-24 17:02:00 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ; // Check the first branch of ternary
2014-05-22 19:48:00 +02:00
check ( " void f(char *p = 0) { \n "
" std::cout << p ? *p : 0; \n " // Due to operator precedence, this is equivalent to: (std::cout << p) ? *p : 0;
" } " ) ;
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" std::cout << (p ? *p : 0); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" std::cout << p; \n "
2013-02-28 08:45:21 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" std::cout << (p && p[0] ? *p : 42); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void isEmpty(int *p = 0) { \n "
2013-04-15 19:00:15 +02:00
" return p && *p; \n "
2013-02-28 08:45:21 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void g(int *p = 0) { \n "
2013-04-15 19:00:15 +02:00
" return !p || *p; \n "
2013-02-28 08:45:21 +01:00
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2013-04-25 09:25:56 +02:00
// bar may initialize p but be can't know for sure without knowing
// if p is passed in by reference and is modified by bar()
check ( " void f(int *p = 0) { \n "
" bar(p); \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
2019-02-28 17:02:46 +01:00
" printf( \" %p \" , p); \n "
2013-04-25 09:25:56 +02:00
" *p = 0; \n "
2015-02-01 15:05:00 +01:00
" } " , true ) ;
2019-11-24 01:40:31 +01:00
ASSERT_EQUALS ( " [test.cpp:3]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ;
2013-04-25 09:25:56 +02:00
// The init() function may or may not initialize p, but since the address
// of p is passed in, it's a good bet that p may be modified and
// so we should not report an error.
check ( " void f(int *p = 0) { \n "
" init(&p); \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void init(int* &g); \n "
" void f(int *p = 0) { \n "
" init(p); \n "
" *p = 0; \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" if (p == 0) { \n "
" init(&p); \n "
" } \n "
" *p = 0; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-04-25 09:25:56 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
check ( " void f(int *p = 0) { \n "
" if (p == 0) { \n "
" throw SomeException; \n "
" } \n "
" *p = 0; \n "
2015-07-25 14:17:55 +02:00
" } " ) ;
2013-04-25 09:25:56 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2014-05-22 19:48:00 +02:00
2021-05-13 20:21:02 +02:00
check ( " void foo(int x, int *p = 0) { \n "
2014-05-22 19:48:00 +02:00
" int var1 = x ? *p : 5; \n "
" } " ) ;
2015-02-01 15:05:00 +01:00
ASSERT_EQUALS ( " [test.cpp:2]: (warning) Possible null pointer dereference if the default parameter value is used: p \n " , errout . str ( ) ) ;
2012-11-21 08:56:17 +01:00
}
2014-11-20 14:20:09 +01:00
void nullpointer_internal_error ( ) { // ticket #5080
2013-10-12 10:50:59 +02:00
check ( " struct A { unsigned int size; }; \n "
" struct B { struct A *a; }; \n "
" void f(struct B *b) { \n "
" unsigned int j; \n "
" for (j = 0; j < b[0].a->size; ++j) { \n "
" } \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2013-10-12 10:50:59 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2015-07-31 21:15:43 +02:00
void ticket6505 ( ) {
check ( " void foo(MythSocket *socket) { \n "
2016-10-23 13:54:44 +02:00
" bool do_write=0; \n "
2015-07-31 21:15:43 +02:00
" if (socket) { \n "
" do_write=something(); \n "
" } \n "
" if (do_write) { \n "
" socket->func(); \n "
" } \n "
" } \n "
" void bar() { \n "
" foo(0); \n "
" } \n " , true , " test.c " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2016-10-09 15:15:29 +02:00
void subtract ( ) {
check ( " void foo(char *s) { \n "
2021-05-13 20:21:02 +02:00
" char *p = s - 20; \n "
2016-10-09 15:15:29 +02:00
" } \n "
2021-02-20 12:58:42 +01:00
" void bar() { foo(0); } " ) ;
2019-08-05 16:26:32 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted. \n " ,
errout . str ( ) ) ;
2016-10-09 15:15:29 +02:00
check ( " void foo(char *s) { \n "
" if (!s) {} \n "
2021-05-13 20:21:02 +02:00
" char *p = s - 20; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-05-01 17:30:29 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " void foo(char *s) { \n "
" s -= 20; \n "
" } \n "
2021-02-20 12:58:42 +01:00
" void bar() { foo(0); } " ) ;
2019-08-05 16:26:32 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted. \n " ,
errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " void foo(char *s) { \n "
" if (!s) {} \n "
" s -= 20; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-05-01 17:30:29 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is overflow in pointer subtraction. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " int* f8() { int *x = NULL; return --x; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted. \n " , errout . str ( ) ) ;
check ( " int* f9() { int *x = NULL; return x--; } " ) ;
ASSERT_EQUALS ( " [test.cpp:1]: (error) Overflow in pointer arithmetic, NULL pointer is subtracted. \n " , errout . str ( ) ) ;
}
void addNull ( ) {
check ( " void foo(char *s) { \n "
" char * p = s + 20; \n "
" } \n "
2021-02-20 12:58:42 +01:00
" void bar() { foo(0); } " ) ;
2019-08-05 16:26:32 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Pointer addition with NULL pointer. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " void foo(char *s) { \n "
" if (!s) {} \n "
" char * p = s + 20; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-05-01 17:30:29 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " void foo(char *s) { \n "
" char * p = 20 + s; \n "
" } \n "
2021-02-20 12:58:42 +01:00
" void bar() { foo(0); } " ) ;
2019-08-05 16:26:32 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Pointer addition with NULL pointer. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " void foo(char *s) { \n "
" if (!s) {} \n "
" char * p = 20 + s; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-05-01 17:30:29 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " void foo(char *s) { \n "
" s += 20; \n "
" } \n "
2021-02-20 12:58:42 +01:00
" void bar() { foo(0); } " ) ;
2019-08-05 16:26:32 +02:00
ASSERT_EQUALS ( " [test.cpp:2]: (error) Pointer addition with NULL pointer. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " void foo(char *s) { \n "
" if (!s) {} \n "
" s += 20; \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2018-05-01 17:30:29 +02:00
ASSERT_EQUALS ( " [test.cpp:2] -> [test.cpp:3]: (warning) Either the condition '!s' is redundant or there is pointer arithmetic with NULL pointer. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
check ( " int* f7() { int *x = NULL; return ++x; } " ) ;
2018-05-02 12:57:24 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) Pointer addition with NULL pointer. \n " , errout . str ( ) ) ;
2018-04-03 21:32:37 +02:00
2021-02-20 12:58:42 +01:00
check ( " int* f10() { int *x = NULL; return x++; } " ) ;
2018-05-02 12:57:24 +02:00
ASSERT_EQUALS ( " [test.cpp:1]: (error) Pointer addition with NULL pointer. \n " , errout . str ( ) ) ;
2018-05-09 09:06:49 +02:00
check ( " class foo {}; \n "
" const char* get() const { return 0; } \n "
2021-02-20 12:58:42 +01:00
" void f(foo x) { if (get()) x += get(); } " ) ;
2018-05-09 09:06:49 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2016-10-09 15:15:29 +02:00
}
2018-12-18 07:56:33 +01:00
2019-12-15 16:36:46 +01:00
void isPointerDeRefFunctionDecl ( ) {
check ( " const char** get() { return 0; } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
}
2021-11-29 07:34:39 +01:00
# define ctu(code) ctu_(code, __FILE__, __LINE__)
void ctu_ ( const char code [ ] , const char * file , int line ) {
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 ;
2021-09-15 20:28:58 +02:00
CheckNullPointer checkNullPointer ( & tokenizer , & settings , this ) ;
fileInfo . push_back ( checkNullPointer . getFileInfo ( & tokenizer , & settings ) ) ;
checkNullPointer . 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-25 12:04:01 +01:00
setMultiline ( ) ;
2018-12-18 07:56:33 +01:00
ctu ( " void f(int *fp) { \n "
" a = *fp; \n "
" } \n "
" int main() { \n "
" int *p = 0; \n "
" f(p); \n "
" } " ) ;
2018-12-25 12:04:01 +01:00
ASSERT_EQUALS ( " test.cpp:2:error:Null pointer dereference: fp \n "
2018-12-30 18:31:37 +01:00
" test.cpp:5:note:Assignment 'p=0', assigned value is 0 \n "
2018-12-25 12:04:01 +01:00
" test.cpp:6:note:Calling function f, 1st argument is null \n "
" test.cpp:2:note:Dereferencing argument fp that is null \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 "
" call(4,0); \n "
" } " ) ;
2018-12-25 12:04:01 +01:00
ASSERT_EQUALS ( " test.cpp:1:error:Null pointer dereference: p \n "
" test.cpp:4:note:Calling function call, 2nd argument is null \n "
2018-12-30 16:23:25 +01:00
" test.cpp:2:note:Calling function use, 1st argument is null \n "
2018-12-25 12:04:01 +01:00
" test.cpp:1:note:Dereferencing argument p that is null \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 "
" dostuff(a, 0); \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 "
" dostuff(a, 0); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-12-31 08:16:21 +01:00
2018-12-31 08:24:39 +01:00
// else
ctu ( " void dostuff(int mask, int *p) { \n "
" if (mask == 13) ; \n "
" else *p = 45; \n "
" } \n "
" \n "
" void f() { \n "
" dostuff(0, 0); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-12-31 08:16:21 +01:00
// ?, &&, ||
ctu ( " void dostuff(int mask, int *p) { \n "
" x = (mask & 1) ? *p : 0; \n "
" } \n "
" \n "
" void f() { \n "
" dostuff(0, 0); \n "
" } " ) ;
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2019-09-20 15:06:37 +02:00
ctu ( " void g(int* x) { *x; } \n "
" void f(int* x) { \n "
" if (x) \n "
" g(x); \n "
2021-02-20 12:58:42 +01:00
" } " ) ;
2019-09-20 15:06:37 +02:00
ASSERT_EQUALS ( " " , errout . str ( ) ) ;
2018-12-18 07:56:33 +01:00
}
2010-10-31 11:51:25 +01:00
} ;
REGISTER_TEST ( TestNullPointer )