From 7062b0a97322776611b1abf165153cc063fc5e27 Mon Sep 17 00:00:00 2001 From: orbitcowboy Date: Fri, 4 Feb 2022 12:30:37 +0100 Subject: [PATCH] std.cfg: Improved configuration of std::[w]string::substr(). Do not allow negative length arguments. --- cfg/std.cfg | 3 +++ test/cfg/std.cpp | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/cfg/std.cfg b/cfg/std.cfg index f704dafc0..ea6be2cc7 100644 --- a/cfg/std.cfg +++ b/cfg/std.cfg @@ -6813,6 +6813,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + 0: @@ -6825,6 +6826,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + 0: @@ -6837,6 +6839,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun + 0: diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 3e06bd92a..84f0a37e7 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -31,6 +31,26 @@ #include #include +void invalidFunctionArg_std_string_substr(const std::string &str, std::size_t pos, std::size_t len) { + // cppcheck-suppress invalidFunctionArg + (void)str.substr(-1,len); + // cppcheck-suppress invalidFunctionArg + (void)str.substr(pos,-1); + // no warning is expected for + (void)str.substr(pos,len); + (void)str.substr(pos, std::wstring::npos); +} + +void invalidFunctionArg_std_wstring_substr(const std::wstring &str, std::size_t pos, std::size_t len) { + // cppcheck-suppress invalidFunctionArg + (void)str.substr(-1,len); + // cppcheck-suppress invalidFunctionArg + (void)str.substr(pos,-1); + // no warning is expected for + (void)str.substr(pos,len); + (void)str.substr(pos, std::string::npos); +} + double invalidFunctionArg_log10(double d = 0.0) { // cppcheck-suppress invalidFunctionArg return log10(d);