From 67b61a0ab7447cfb6b52cf80b37f2ef95767eb27 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 31 Oct 2023 19:08:08 +0100 Subject: [PATCH] Fix #12134 false negative: passedByValue (#5607) --- cfg/std.cfg | 3 +++ lib/checkother.cpp | 2 +- lib/checkother.h | 2 +- test/cfg/std.cpp | 7 +++++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/cfg/std.cfg b/cfg/std.cfg index cc9785244..490480a56 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -6977,6 +6977,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + false @@ -6990,6 +6991,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + false @@ -7003,6 +7005,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + false diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 8324fb130..3cd33d2e5 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3596,7 +3596,7 @@ void CheckOther::checkShadowVariables() } } -void CheckOther::shadowError(const Token *var, const Token *shadowed, std::string type) +void CheckOther::shadowError(const Token *var, const Token *shadowed, const std::string& type) { ErrorPath errorPath; errorPath.emplace_back(shadowed, "Shadowed declaration"); diff --git a/lib/checkother.h b/lib/checkother.h index 420c208b5..2499ef1b5 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -284,7 +284,7 @@ private: void accessMovedError(const Token *tok, const std::string &varname, const ValueFlow::Value *value, bool inconclusive); void funcArgNamesDifferent(const std::string & functionName, nonneg int index, const Token* declaration, const Token* definition); void funcArgOrderDifferent(const std::string & functionName, const Token * declaration, const Token * definition, const std::vector & declarations, const std::vector & definitions); - void shadowError(const Token *var, const Token *shadowed, std::string type); + void shadowError(const Token *var, const Token *shadowed, const std::string& type); void knownArgumentError(const Token *tok, const Token *ftok, const ValueFlow::Value *value, const std::string &varexpr, bool isVariableExpressionHidden); void knownPointerToBoolError(const Token* tok, const ValueFlow::Value* value); void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2); diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index a281552c8..ecabb2f95 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -4638,6 +4638,13 @@ void string_view_unused(std::string_view v) v.substr(1, 3); } +// cppcheck-suppress passedByValue +void string_substr(std::string s) +{ + // cppcheck-suppress ignoredReturnValue + s.substr(1, 3); +} + void stdspan() { #ifndef __cpp_lib_span