From 50dc58958fa2173bb02d8976e0f77d604fd759f9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 2 May 2024 23:10:53 +0200 Subject: [PATCH] Partial fix for #12695 FP assertWithSideEffect with library functions (#6373) --- cfg/gtk.cfg | 3 +++ cfg/opengl.cfg | 1 + cfg/qt.cfg | 1 + cfg/sqlite3.cfg | 2 ++ cfg/std.cfg | 9 +++++++++ lib/checkassert.cpp | 5 +++++ test/cfg/std.cpp | 19 +++++++++++++++++++ 7 files changed, 40 insertions(+) diff --git a/cfg/gtk.cfg b/cfg/gtk.cfg index 1e20a335128..492cf592988 100644 --- a/cfg/gtk.cfg +++ b/cfg/gtk.cfg @@ -1506,6 +1506,7 @@ + false @@ -1679,6 +1680,7 @@ + false @@ -4039,6 +4041,7 @@ + false diff --git a/cfg/opengl.cfg b/cfg/opengl.cfg index 72dcaddb961..ec37abf5516 100644 --- a/cfg/opengl.cfg +++ b/cfg/opengl.cfg @@ -211,6 +211,7 @@ + false diff --git a/cfg/qt.cfg b/cfg/qt.cfg index 824c4bf2881..8d599ed5d3d 100644 --- a/cfg/qt.cfg +++ b/cfg/qt.cfg @@ -72,6 +72,7 @@ + false diff --git a/cfg/sqlite3.cfg b/cfg/sqlite3.cfg index 0b0a439b18c..6c1053b393f 100644 --- a/cfg/sqlite3.cfg +++ b/cfg/sqlite3.cfg @@ -966,6 +966,7 @@ + false @@ -988,6 +989,7 @@ + false diff --git a/cfg/std.cfg b/cfg/std.cfg index a736d1ede38..bcea6e88b28 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -4806,6 +4806,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + false @@ -5119,6 +5120,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + false @@ -5377,6 +5379,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + false @@ -5620,6 +5623,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + arg1 < 'A' || arg1 > 'Z' ? arg1 : arg1 + 32 false @@ -8129,6 +8133,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init + false @@ -8171,6 +8176,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init + false @@ -8217,6 +8223,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init + false @@ -8667,6 +8674,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init + false @@ -8815,6 +8823,7 @@ initializer list (7) string& replace (const_iterator i1, const_iterator i2, init + diff --git a/lib/checkassert.cpp b/lib/checkassert.cpp index 0202ed7454a..3ed3ae12aa7 100644 --- a/lib/checkassert.cpp +++ b/lib/checkassert.cpp @@ -68,6 +68,11 @@ void CheckAssert::assertWithSideEffects() return ac.second.iteratorInfo.container > 0; // bailout, takes iterators -> assume read access })) continue; + if (tmp->str() == "get" && Token::simpleMatch(tmp->astParent(), ".") && astIsSmartPointer(tmp->astParent()->astOperand1())) + continue; + if (f->containerYield == Library::Container::Yield::START_ITERATOR || // bailout for std::begin/end + f->containerYield == Library::Container::Yield::END_ITERATOR) + continue; sideEffectInAssertError(tmp, mSettings->library.getFunctionName(tmp)); } continue; diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 9ce1a976a68..b949f9db9bc 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -5022,3 +5022,22 @@ void assertWithSideEffect_system() // cppcheck-suppress [assertWithSideEffect,checkLibraryNoReturn] // TODO: #8329 assert(std::system("abc")); } + +void assertWithSideEffect_std_map_at(const std::map& m) // #12695 +{ + // cppcheck-suppress checkLibraryNoReturn + assert(m.at(0)); +} + +void assertWithSideEffect_std_unique_ptr_get(std::unique_ptr& p) +{ + // cppcheck-suppress checkLibraryNoReturn + assert(p.get()); +} + +void assertWithSideEffect_std_begin(const std::vector& v) { + // cppcheck-suppress checkLibraryFunction // TODO + assert(std::is_sorted(std::begin(v), std::end(v), [](const std::string& a, const std::string& b) { + return a.size() < b.size(); + })); // cppcheck-suppress checkLibraryNoReturn +}