Skip to content

Commit

Permalink
Fix #12236 FP related to decltype/__typeof__ (#5717)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Dec 4, 2023
1 parent 6192d0d commit 41e6c87
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ $(libcppdir)/checkfunctions.o: lib/checkfunctions.cpp lib/addoninfo.h lib/astuti
$(libcppdir)/checkinternal.o: lib/checkinternal.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkinternal.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkinternal.cpp

$(libcppdir)/checkio.o: lib/checkio.cpp lib/addoninfo.h lib/check.h lib/checkio.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(libcppdir)/checkio.o: lib/checkio.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkio.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/checkio.cpp

$(libcppdir)/checkleakautovar.o: lib/checkleakautovar.cpp lib/addoninfo.h lib/astutils.h lib/check.h lib/checkleakautovar.h lib/checkmemoryleak.h lib/checknullpointer.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
Expand Down
5 changes: 5 additions & 0 deletions lib/checkio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//---------------------------------------------------------------------------
#include "checkio.h"

#include "astutils.h"
#include "errortypes.h"
#include "library.h"
#include "mathlib.h"
Expand Down Expand Up @@ -152,6 +153,10 @@ void CheckIO::checkFileUsage()
for (const Scope * scope : symbolDatabase->functionScopes) {
int indent = 0;
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "%name% (") && isUnevaluated(tok)) {
tok = tok->linkAt(1);
continue;
}
if (tok->str() == "{")
indent++;
else if (tok->str() == "}") {
Expand Down
5 changes: 5 additions & 0 deletions lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
if (!tok || tok == endToken)
break;

if (Token::Match(tok, "%name% (") && isUnevaluated(tok)) {
tok = tok->linkAt(1);
continue;
}

if (Token::Match(tok, "const %type%"))
tok = tok->tokAt(2);

Expand Down
6 changes: 3 additions & 3 deletions man/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -966,15 +966,15 @@ To use a .cfg file shipped with cppcheck, pass the `--library=<lib>` option. The
| avr.cfg | |
| bento4.cfg | [Bento4](http://www.bento4.com/) |
| boost.cfg | [Boost](http://www.boost.org/)|
| bsd.cfg | [*BSD](https://www.freebsd.org/) |
| bsd.cfg | [BSD](https://www.freebsd.org/) |
| cairo.cfg | [cairo](https://www.cairographics.org/) |
| cppcheck-lib.cfg | [Cppcheck](http://cppcheck.net/) | Used in selfcheck of the Cppcheck code base
| cppunit.cfg | [CppUnit](https://sourceforge.net/projects/cppunit/) |
| dpdk.cfg | |
| embedded_sql.cfg | |
| emscripten.cfg | |
| ginac.cfg | |
| gnu.cfg | [*nix](https://www.gnu.org/) |
| gnu.cfg | [GNU](https://www.gnu.org/) |
| googletest.cfg | [GoogleTest](https://github.com/google/googletest) |
| gtk.cfg | [GTK](https://www.gtk.org/) |
| icu.cfg | |
Expand All @@ -997,7 +997,7 @@ To use a .cfg file shipped with cppcheck, pass the `--library=<lib>` option. The
| pcre.cfg | [PCRE](https://pcre.org/) |
| posix.cfg | [POSIX](https://pubs.opengroup.org/onlinepubs/9699919799/) |
| python.cfg | |
| qt.cfg | [Qt](https://www.qt.io/) |
| qt.cfg | [Qt](https://doc.qt.io/qt.html) |
| ruby.cfg | |
| sdl.cfg | |
| sfml.cfg | |
Expand Down
9 changes: 9 additions & 0 deletions test/cfg/std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,15 @@ void nullPointer_localtime_s(const std::time_t *restrict time, struct tm *restri
(void)std::localtime_s(time, NULL);
(void)std::localtime_s(time, result);
}

void memleak_localtime_s(const std::time_t *restrict time, struct tm *restrict result)
{
const time_t t = time(0);
const struct tm* const now = new tm();
if (localtime_s(now, &t) == 0)
std::cout << now->tm_mday << std::endl;
// cppcheck-suppress memleak
}
#endif // __STDC_LIB_EXT1__

size_t nullPointer_strftime(char *s, size_t max, const char *fmt, const struct tm *p)
Expand Down
8 changes: 8 additions & 0 deletions test/testio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ class TestIO : public TestFixture {
" fclose(f[1]);\n"
"}");
ASSERT_EQUALS("", errout.str());

// #12236
check("void f() {\n"
" FILE* f = fopen(\"abc\", \"r\");\n"
" decltype(fclose(f)) y;\n"
" y = fclose(f);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void fileIOwithoutPositioning() {
Expand Down
25 changes: 25 additions & 0 deletions test/testleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class TestLeakAutoVar : public TestFixture {
TEST_CASE(deallocuse10);
TEST_CASE(deallocuse11); // #8302
TEST_CASE(deallocuse12);
TEST_CASE(deallocuse13);

TEST_CASE(doublefree1);
TEST_CASE(doublefree2);
Expand All @@ -127,6 +128,7 @@ class TestLeakAutoVar : public TestFixture {
TEST_CASE(doublefree13); // #11008
TEST_CASE(doublefree14); // #9708
TEST_CASE(doublefree15);
TEST_CASE(doublefree16);

// exit
TEST_CASE(exit1);
Expand Down Expand Up @@ -908,6 +910,20 @@ class TestLeakAutoVar : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void deallocuse13() {
check("void f() {\n" // #9695
" auto* a = new int[2];\n"
" delete[] a;\n"
" a[1] = 0;\n"
" auto* b = static_cast<int*>(malloc(8));\n"
" free(b);\n"
" b[1] = 0;\n"
"}\n", true);
ASSERT_EQUALS("[test.cpp:4]: (error) Dereferencing 'a' after it is deallocated / released\n"
"[test.cpp:7]: (error) Dereferencing 'b' after it is deallocated / released\n",
errout.str());
}

void doublefree1() { // #3895
check("void f(char *p) {\n"
" if (x)\n"
Expand Down Expand Up @@ -1566,6 +1582,15 @@ class TestLeakAutoVar : public TestFixture {
ASSERT_EQUALS("", errout.str());
}

void doublefree16() { // #12236
check("void f() {\n"
" FILE* f = fopen(\"abc\", \"r\");\n"
" decltype(fclose(f)) y;\n"
" y = fclose(f);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}

void exit1() {
check("void f() {\n"
" char *p = malloc(10);\n"
Expand Down

0 comments on commit 41e6c87

Please sign in to comment.