From 233e27b579daf2261097cce76a49c22216902988 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 9 Dec 2023 00:36:55 +0100 Subject: [PATCH] Add tests for #8399/#10646/#10833 (#5743) --- test/cfg/windows.cpp | 14 ++++++++++++++ test/testautovariables.cpp | 7 +++++++ test/testtype.cpp | 9 +++++++++ 3 files changed, 30 insertions(+) diff --git a/test/cfg/windows.cpp b/test/cfg/windows.cpp index 9c5ab3bb7..5fbff780a 100644 --- a/test/cfg/windows.cpp +++ b/test/cfg/windows.cpp @@ -593,6 +593,20 @@ void memleak_LocalAlloc() // cppcheck-suppress memleak } +void memleak_dupenv_s() // #10646 +{ + char* pValue; + size_t len; + errno_t err = _dupenv_s(&pValue, &len, "pathext"); + if (err) return -1; + printf("pathext = %s\n", pValue); + free(pValue); + err = _dupenv_s(&pValue, &len, "nonexistentvariable"); + if (err) return -1; + printf("nonexistentvariable = %s\n", pValue); + // cppcheck-suppress memleak +} + void resourceLeak_CreateSemaphoreA() { HANDLE hSemaphore; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index ede111f1c..b48f49fe7 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -2933,6 +2933,13 @@ private: ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2] -> [test.cpp:3]: (error) Using object that is a temporary.\n", errout.str()); + // #10833 + check("struct A { std::string s; };\n" + "const std::string& f(A* a) {\n" + " return a ? a->s : \"\";\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Reference to temporary returned.\n", errout.str()); + check("std::span f() {\n" " std::vector v{};\n" " return v;\n" diff --git a/test/testtype.cpp b/test/testtype.cpp index dbf6c07ae..a55bfa505 100644 --- a/test/testtype.cpp +++ b/test/testtype.cpp @@ -490,6 +490,8 @@ private: dui.std = "c++11"; // this is set by the production code via cppcheck::Platform::getLimitDefines() dui.defines.emplace_back("INT_MIN=-2147483648"); + dui.defines.emplace_back("INT32_MAX=2147483647"); + dui.defines.emplace_back("int32_t=int"); checkP("int fun(int x)\n" "{\n" @@ -501,6 +503,13 @@ private: " fun(INT_MIN);\n" "}", settings, "test.cpp", dui); ASSERT_EQUALS("[test.cpp:3]: (error) Signed integer overflow for expression '-x'.\n", errout.str()); + + checkP("void f() {\n" // #8399 + " int32_t i = INT32_MAX;\n" + " i << 1;\n" + " i << 2;\n" + "}", settings, "test.cpp", dui); + ASSERT_EQUALS("[test.cpp:4]: (error) Signed integer overflow for expression 'i<<2'.\n", errout.str()); } };