From cd7532df215ab1b63d945f6c42d2c694da1a1f40 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 10 Feb 2022 20:48:51 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20#7709=20FN=20C-style=20pointer=20casting?= =?UTF-8?q?=20with=20built-in=20or=20typedef=20types=20/=E2=80=A6=20(#3814?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/checkother.cpp | 7 +++---- lib/token.cpp | 2 +- test/cfg/googletest.cpp | 2 +- test/cfg/opencv2.cpp | 4 ++-- test/cfg/std.cpp | 14 ++++++------- test/cfg/windows.cpp | 2 +- test/testother.cpp | 46 ++++++++++++++++++++++++++++++++++++++++- 7 files changed, 60 insertions(+), 17 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index bbe613fa2..723b5b4bd 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -301,11 +301,11 @@ void CheckOther::warningOldStylePointerCast() tok = scope->bodyStart; for (; tok && tok != scope->bodyEnd; tok = tok->next()) { // Old style pointer casting.. - if (!Token::Match(tok, "( const|volatile| const|volatile| %type% * const| ) (| %name%|%num%|%bool%|%char%|%str%")) + if (!Token::Match(tok, "( const|volatile| const|volatile|class|struct| %type% * const|&| ) (| %name%|%num%|%bool%|%char%|%str%")) continue; // skip first "const" in "const Type* const" - while (Token::Match(tok->next(), "const|volatile")) + while (Token::Match(tok->next(), "const|volatile|class|struct")) tok = tok->next(); const Token* typeTok = tok->next(); // skip second "const" in "const Type* const" @@ -316,8 +316,7 @@ void CheckOther::warningOldStylePointerCast() if (p->hasKnownIntValue() && p->values().front().intvalue==0) // Casting nullpointers is safe continue; - // Is "type" a class? - if (typeTok->type()) + if (typeTok->tokType() == Token::eType || typeTok->tokType() == Token::eName) cstyleCastError(tok); } } diff --git a/lib/token.cpp b/lib/token.cpp index c9f38f7ce..b16b75dfa 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -586,7 +586,7 @@ bool Token::simpleMatch(const Token *tok, const char pattern[], size_t pattern_l return false; // shortcut const char *current = pattern; const char *end = pattern + pattern_len; - const char *next = (const char*)std::memchr(pattern, ' ', pattern_len); + const char *next = static_cast(std::memchr(pattern, ' ', pattern_len)); if (!next) next = end; diff --git a/test/cfg/googletest.cpp b/test/cfg/googletest.cpp index bff59dc1a..1e4f71da2 100644 --- a/test/cfg/googletest.cpp +++ b/test/cfg/googletest.cpp @@ -29,7 +29,7 @@ namespace ExampleNamespace { TEST(ASSERT, ASSERT) { - int *a = (int*)calloc(10,sizeof(int)); + int *a = (int*)calloc(10,sizeof(int)); // cppcheck-suppress cstyleCast ASSERT_TRUE(a != nullptr); a[0] = 10; diff --git a/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index 505cd83bf..e88ee4ab1 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -27,7 +27,7 @@ void validCode(char* argStr) cvStr += " World"; std::cout << cvStr; - char * pBuf = (char *)cv::fastMalloc(20); + char * pBuf = (char *)cv::fastMalloc(20); // cppcheck-suppress cstyleCast cv::fastFree(pBuf); } @@ -39,7 +39,7 @@ void ignoredReturnValue() void memleak() { - char * pBuf = (char *)cv::fastMalloc(1000); + char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast std::cout << pBuf; // cppcheck-suppress memleak } diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index e49b4e914..84e407481 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -2029,7 +2029,7 @@ void uninitvar_longjmp(void) void uninitvar_malloc(void) { size_t size; - // cppcheck-suppress uninitvar + // cppcheck-suppress [uninitvar, cstyleCast] int *p = (int*)std::malloc(size); free(p); } @@ -2261,7 +2261,7 @@ void uninivar_bsearch(void) void* base; size_t num; size_t size; - // cppcheck-suppress uninitvar + // cppcheck-suppress [uninitvar, cstyleCast] (void)std::bsearch(key,base,num,size,(int (*)(const void*,const void*))strcmp); } @@ -2271,11 +2271,11 @@ void minsize_bsearch(const void* key, const void* base, { int Base[3] = {42, 43, 44}; - (void)std::bsearch(key,Base,2,size,(int (*)(const void*,const void*))strcmp); - (void)std::bsearch(key,Base,3,size,(int (*)(const void*,const void*))strcmp); - (void)std::bsearch(key,Base,4,size,(int (*)(const void*,const void*))strcmp); + (void)std::bsearch(key,Base,2,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast + (void)std::bsearch(key,Base,3,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast + (void)std::bsearch(key,Base,4,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast - (void)std::bsearch(key,base,2,size,(int (*)(const void*,const void*))strcmp); + (void)std::bsearch(key,base,2,size,(int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast } void uninitvar_qsort(void) @@ -2284,7 +2284,7 @@ void uninitvar_qsort(void) size_t n; size_t size; // cppcheck-suppress uninitvar - (void)std::qsort(base,n,size, (int (*)(const void*,const void*))strcmp); + (void)std::qsort(base,n,size, (int (*)(const void*,const void*))strcmp); // cppcheck-suppress cstyleCast } void uninitvar_putc(void) diff --git a/test/cfg/windows.cpp b/test/cfg/windows.cpp index f0a06681c..0aa8de5da 100644 --- a/test/cfg/windows.cpp +++ b/test/cfg/windows.cpp @@ -513,7 +513,7 @@ void memleak_HeapAlloc() void memleak_LocalAlloc() { LPTSTR pszBuf; - // cppcheck-suppress LocalAllocCalled + // cppcheck-suppress [LocalAllocCalled, cstyleCast] pszBuf = (LPTSTR)LocalAlloc(LPTR, MAX_PATH*sizeof(TCHAR)); (void)LocalSize(pszBuf); (void)LocalFlags(pszBuf); diff --git a/test/testother.cpp b/test/testother.cpp index 80e55792e..b79404433 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1460,6 +1460,50 @@ private: " v.push_back((Base*)new Derived);\n" "}"); ASSERT_EQUALS("[test.cpp:5]: (style) C-style pointer casting\n", errout.str()); + + // #7709 + checkOldStylePointerCast("typedef struct S S;\n" + "typedef struct S SS;\n" + "typedef class C C;\n" + "typedef long LONG;\n" + "typedef long* LONGP;\n" + "struct T {};\n" + "typedef struct T TT;\n" + "typedef struct T2 {} TT2;\n" + "void f(int* i) {\n" + " S* s = (S*)i;\n" + " SS* ss = (SS*)i;\n" + " struct S2* s2 = (struct S2*)i;\n" + " C* c = (C*)i;\n" + " class C2* c2 = (class C2*)i;\n" + " long* l = (long*)i;\n" + " LONG* l2 = (LONG*)i;\n" + " LONGP l3 = (LONGP)i;\n" + " TT* tt = (TT*)i;\n" + " TT2* tt2 = (TT2*)i;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:10]: (style) C-style pointer casting\n" + "[test.cpp:11]: (style) C-style pointer casting\n" + "[test.cpp:12]: (style) C-style pointer casting\n" + "[test.cpp:13]: (style) C-style pointer casting\n" + "[test.cpp:14]: (style) C-style pointer casting\n" + "[test.cpp:15]: (style) C-style pointer casting\n" + "[test.cpp:16]: (style) C-style pointer casting\n" + "[test.cpp:17]: (style) C-style pointer casting\n" + "[test.cpp:18]: (style) C-style pointer casting\n" + "[test.cpp:19]: (style) C-style pointer casting\n", + errout.str()); + + // #8649 + checkOldStylePointerCast("struct S {};\n" + "void g(S*& s);\n" + "void f(int i) {\n" + " g((S*&)i);\n" + " S*& r = (S*&)i;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) C-style pointer casting\n" + "[test.cpp:5]: (style) C-style pointer casting\n", + errout.str()); } #define checkInvalidPointerCast(...) checkInvalidPointerCast_(__FILE__, __LINE__, __VA_ARGS__) @@ -7623,7 +7667,7 @@ private: " *reg = 12;\n" " *reg = 34;\n" "}"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("test.cpp:2:style:C-style pointer casting\n", errout.str()); } void redundantVarAssignment_trivial() {