From 233c533ee0ce7caa1b47557388b22ce51ad32053 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:38:25 +0100 Subject: [PATCH 01/11] Update checkimpl.cpp --- lib/checkimpl.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/checkimpl.cpp b/lib/checkimpl.cpp index 99954315d26..c1a0c45727f 100644 --- a/lib/checkimpl.cpp +++ b/lib/checkimpl.cpp @@ -47,6 +47,9 @@ ErrorPath CheckImpl::getErrorPath(const Token* errtok, const ValueFlow::Value* v errorPath.emplace_back(errtok, std::move(bug)); } else if (mSettings.verbose || mSettings.outputFormat == Settings::OutputFormat::xml || !mSettings.templateLocation.empty()) { errorPath = value->errorPath; + // array passed to subfunction + if (errorPath.empty() && Token::simpleMatch(errtok, "[") && errtok->astOperand1() && !errtok->astOperand1()->values().empty() && errtok->astOperand1()->values().front().isTokValue()) + errorPath = errtok->astOperand1()->values().front().errorPath; errorPath.emplace_back(errtok, std::move(bug)); } else { if (value->condition) From 0b1fb56470fcf06265b9bf668c54e3355b113878 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:39:21 +0100 Subject: [PATCH 02/11] Update testbufferoverrun.cpp --- test/testbufferoverrun.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 0e0bf5ce385..e207691221b 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -3799,6 +3799,17 @@ class TestBufferOverrun : public TestFixture { "[test.cpp:9:14]: note: Assignment 'j=-1', assigned value is -1\n" "[test.cpp:10:9]: note: Assuming condition is false\n" "[test.cpp:12:13]: note: Negative array index\n", errout_str()); + + check("char g(const char* s) {\n" + " return s[6];\n" + "}\n" + "void f() {\n" + " g(\"abc\");\n" + "}\n", s); + ASSERT_EQUALS("[test.cpp:2:13]: error: Array 's[4]' accessed at index 6, which is out of bounds. [arrayIndexOutOfBounds]\n" + "[test.cpp:5:7]: note: Calling function 'g', 1st argument '\"abc\"' value is \"abc\"\n" + "[test.cpp:2:13]: note: Array index out of bounds\n", + errout_str()); } void buffer_overrun_bailoutIfSwitch() { From c03aeb16926b7b3792e52bdfed5ba81b92a4a184 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:25:24 +0100 Subject: [PATCH 03/11] Update checkimpl.cpp [skip ci] --- lib/checkimpl.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/checkimpl.cpp b/lib/checkimpl.cpp index c1a0c45727f..99954315d26 100644 --- a/lib/checkimpl.cpp +++ b/lib/checkimpl.cpp @@ -47,9 +47,6 @@ ErrorPath CheckImpl::getErrorPath(const Token* errtok, const ValueFlow::Value* v errorPath.emplace_back(errtok, std::move(bug)); } else if (mSettings.verbose || mSettings.outputFormat == Settings::OutputFormat::xml || !mSettings.templateLocation.empty()) { errorPath = value->errorPath; - // array passed to subfunction - if (errorPath.empty() && Token::simpleMatch(errtok, "[") && errtok->astOperand1() && !errtok->astOperand1()->values().empty() && errtok->astOperand1()->values().front().isTokValue()) - errorPath = errtok->astOperand1()->values().front().errorPath; errorPath.emplace_back(errtok, std::move(bug)); } else { if (value->condition) From 6d1c5403e3a20f47d532ba7819dd4c7fe2d42dd5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:26:19 +0100 Subject: [PATCH 04/11] Update checkbufferoverrun.h [skip ci] --- lib/checkbufferoverrun.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index afac19d796c..0e7e6653ea6 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -102,7 +102,8 @@ class CPPCHECKLIB CheckBufferOverrunImpl : public CheckImpl void arrayIndex(); void arrayIndexError(const Token* tok, const std::vector& dimensions, - const std::vector& indexes); + const std::vector& indexes, + const ErrorPath& errorPathArray); void negativeIndexError(const Token* tok, const std::vector& dimensions, const std::vector& indexes); From 4708686a6a5737a220cc355819b3c161da719b76 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:26:46 +0100 Subject: [PATCH 05/11] Update checkbufferoverrun.cpp [skip ci] --- lib/checkbufferoverrun.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index c97642b9930..560219e8666 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -221,7 +221,7 @@ static bool getDimensionsEtc(const Token * const arrayToken, const Settings &set return ChildrenToVisit::op1_and_op2; }); } - } else if (const Token *stringLiteral = array->getValueTokenMinStrSize(settings, &path)) { + } else if (const Token *stringLiteral = array->getValueTokenMinStrSize(settings, &path, &errorPath)) { Dimension dim; dim.tok = nullptr; dim.num = Token::getStrArraySize(stringLiteral); @@ -360,7 +360,7 @@ void CheckBufferOverrunImpl::arrayIndex() const std::vector& indexValues = getOverrunIndexValues(tok, tok->astOperand1(), dimensions, indexTokens, path); if (!indexValues.empty()) - arrayIndexError(tok, dimensions, indexValues); + arrayIndexError(tok, dimensions, indexValues, std::move(errorPath)); } // Negative index @@ -421,7 +421,8 @@ static std::string arrayIndexMessage(const Token* tok, void CheckBufferOverrunImpl::arrayIndexError(const Token* tok, const std::vector& dimensions, - const std::vector& indexes) + const std::vector& indexes, + const ErrorPath& errorPathArray) { if (!tok) { reportError(tok, Severity::error, "arrayIndexOutOfBounds", "Array 'arr[16]' accessed at index 16, which is out of bounds.", CWE_BUFFER_OVERRUN, Certainty::normal); @@ -440,7 +441,10 @@ void CheckBufferOverrunImpl::arrayIndexError(const Token* tok, index = &indexValue; } - reportError(getErrorPath(tok, index, "Array index out of bounds"), + ErrorPath errorPath = getErrorPath(tok, index, "Array index out of bounds"); + errorPath.insert(errorPath.end(), errorPathArray.begin(), errorPathArray.end()); + + reportError(errorPath, index->errorSeverity() ? Severity::error : Severity::warning, index->condition ? "arrayIndexOutOfBoundsCond" : "arrayIndexOutOfBounds", arrayIndexMessage(tok, dimensions, indexes, condition), @@ -1342,7 +1346,7 @@ void CheckBufferOverrun::runChecks(const Tokenizer &tokenizer, ErrorLogger& erro void CheckBufferOverrun::getErrorMessages(ErrorLogger& errorLogger, const Settings &settings) const { CheckBufferOverrunImpl c(nullptr, settings, errorLogger); - c.arrayIndexError(nullptr, std::vector(), std::vector()); + c.arrayIndexError(nullptr, std::vector(), std::vector(), {}); c.pointerArithmeticError(nullptr, nullptr, nullptr); c.negativeIndexError(nullptr, std::vector(), std::vector()); c.arrayIndexThenCheckError(nullptr, "i"); From 67659a69fbb9c1b3226b31d0e0a28677e3274f46 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:27:16 +0100 Subject: [PATCH 06/11] Update testbufferoverrun.cpp --- test/testbufferoverrun.cpp | 76 +++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index e207691221b..addd01371a1 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -442,7 +442,7 @@ class TestBufferOverrun : public TestFixture { " str[i] = 0;\n" "}\n" "void b() { a(16); }\n"); - ASSERT_EQUALS("[test.cpp:4:8]: (error) Array 'str[16]' accessed at index 16, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:8] -> [test.cpp:3:15]: (error) Array 'str[16]' accessed at index 16, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } void array_index_4() { @@ -1304,7 +1304,7 @@ class TestBufferOverrun : public TestFixture { " p[10] = 7;\n" " free(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:4]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:4] -> [test.cpp:3:14]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" @@ -1312,7 +1312,7 @@ class TestBufferOverrun : public TestFixture { " p[10] = 7;\n" " free(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:4]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:4] -> [test.cpp:3:15]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" @@ -2711,14 +2711,14 @@ class TestBufferOverrun : public TestFixture { " const char *str = \"abc\";\n" " bar(str[10]);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3:12]: (error) Array 'str[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:12] -> [test.cpp:2:23]: (error) Array 'str[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" " const char *str = \"abc\";\n" " bar(str[4]);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:12]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:12] -> [test.cpp:3:23]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" @@ -2732,7 +2732,7 @@ class TestBufferOverrun : public TestFixture { " const char *str = \"a\tc\";\n" " bar(str[4]);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:12]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:12] -> [test.cpp:3:23]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" // #6973 " const char *name = \"\";\n" @@ -2937,7 +2937,7 @@ class TestBufferOverrun : public TestFixture { " p[5] = 1;\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:4:6]: (error) Array 'p[3]' accessed at index 5, which is out of bounds. [arrayIndexOutOfBounds]\n", + "[test.cpp:4:6] -> [test.cpp:3:20]: (error) Array 'p[3]' accessed at index 5, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" @@ -3032,7 +3032,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int* z = new int(1);\n" @@ -3040,7 +3040,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int* z = new int{};\n" @@ -3048,7 +3048,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int* z = new int[5];\n" @@ -3056,7 +3056,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void g() {\n" " int* z = new int[5]();\n" @@ -3064,7 +3064,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 1;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5];\n" @@ -3072,7 +3072,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5]();\n" @@ -3080,7 +3080,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5]{};\n" @@ -3088,7 +3088,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5]{ 0 };\n" @@ -3096,7 +3096,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } void buffer_overrun_2_struct() { @@ -3800,15 +3800,15 @@ class TestBufferOverrun : public TestFixture { "[test.cpp:10:9]: note: Assuming condition is false\n" "[test.cpp:12:13]: note: Negative array index\n", errout_str()); - check("char g(const char* s) {\n" + check("char g(const char* s) {\n" // #10253 " return s[6];\n" "}\n" "void f() {\n" " g(\"abc\");\n" "}\n", s); - ASSERT_EQUALS("[test.cpp:2:13]: error: Array 's[4]' accessed at index 6, which is out of bounds. [arrayIndexOutOfBounds]\n" - "[test.cpp:5:7]: note: Calling function 'g', 1st argument '\"abc\"' value is \"abc\"\n" - "[test.cpp:2:13]: note: Array index out of bounds\n", + ASSERT_EQUALS("[test.cpp:5:7]: error: Array 's[4]' accessed at index 6, which is out of bounds. [arrayIndexOutOfBounds]\n" + "[test.cpp:2:13]: note: Array index out of bounds\n" + "[test.cpp:5:7]: note: Calling function 'g', 1st argument '\"abc\"' value is \"abc\"\n", errout_str()); } @@ -4082,7 +4082,7 @@ class TestBufferOverrun : public TestFixture { " if (cond) x = \"abcde\";\n" " return x[20];\n" // <- array index out of bounds when x is "abcde" "}\n"); - ASSERT_EQUALS("[test.cpp:4:11]: (error) Array 'x[6]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:11] -> [test.cpp:3:17] -> [test.cpp:3:7]: (error) Array 'x[6]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } void pointer_out_of_bounds_1() { @@ -4322,7 +4322,7 @@ class TestBufferOverrun : public TestFixture { " char *s; s = new char[10];\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:16]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #1670 - false negative when using return check("char f()\n" @@ -4330,7 +4330,7 @@ class TestBufferOverrun : public TestFixture { " int *s; s = new int[10];\n" " return s[10];\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:13]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:13] -> [test.cpp:3:15]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("struct Fred { char c[10]; };\n" "char f()\n" @@ -4363,7 +4363,7 @@ class TestBufferOverrun : public TestFixture { " buf[9] = 0;\n" " delete [] buf;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:6:6]: (error) Array 'buf[9]' accessed at index 9, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:6:6] -> [test.cpp:5:7]: (error) Array 'buf[9]' accessed at index 9, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo()\n" "{\n" @@ -4371,7 +4371,7 @@ class TestBufferOverrun : public TestFixture { " char *s; s = new char[Size];\n" " s[Size] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:6]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5:6] -> [test.cpp:4:16]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo()\n" "{\n" @@ -4379,7 +4379,7 @@ class TestBufferOverrun : public TestFixture { " E *e; e = new E[10];\n" " e[10] = ZERO;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:6]: (error) Array 'e[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5:6] -> [test.cpp:4:13]: (error) Array 'e[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } // data is allocated with malloc @@ -4389,14 +4389,14 @@ class TestBufferOverrun : public TestFixture { " char *s; s = (char *)malloc(10);\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:16]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #842 check("void f() {\n" " int *tab4 = (int *)malloc(20 * sizeof(int));\n" " tab4[20] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:15]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #1478 check("void foo() {\n" @@ -4405,7 +4405,7 @@ class TestBufferOverrun : public TestFixture { " p = (char *)malloc(10);\n" " p[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:6]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5:6] -> [test.cpp:4:7]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #1134 check("void f() {\n" @@ -4413,7 +4413,7 @@ class TestBufferOverrun : public TestFixture { " x = (int *)malloc(10 * sizeof(int));\n" " x[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 'x[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:7]: (error) Array 'x[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int *tab4; tab4 = malloc(20 * sizeof(int));\n" @@ -4439,21 +4439,21 @@ class TestBufferOverrun : public TestFixture { " E *tab4 = (E *)malloc(Size * 4);\n" " tab4[Size] = Size;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:3:13]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " enum E { Size = 20 };\n" " E *tab4 = (E *)malloc(4 * Size);\n" " tab4[Size] = Size;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:3:13]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " enum E { ZERO };\n" " E *tab4 = (E *)malloc(20 * sizeof(E));\n" " tab4[20] = ZERO;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:3:13]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" // #8721 " unsigned char **cache = malloc(32);\n" @@ -4467,14 +4467,14 @@ class TestBufferOverrun : public TestFixture { " for (int i = 0; i < 3; i++)\n" " a[i] = NULL;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:2:11]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int **a = new int*[2];\n" " for (int i = 0; i < 3; i++)\n" " a[i] = NULL;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:2:11]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f(const uint8_t* a) {\n" // 10421 " uint8_t* p = (uint8_t*)malloc(20U * sizeof(uint8_t));\n" @@ -4483,7 +4483,7 @@ class TestBufferOverrun : public TestFixture { " p[i] = a[i];\n" " free(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:10]: (error) Array 'p[20]' accessed at index 29, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5:10] -> [test.cpp:2:16] -> [test.cpp:3:9]: (error) Array 'p[20]' accessed at index 29, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } // statically allocated buffer @@ -4493,14 +4493,14 @@ class TestBufferOverrun : public TestFixture { " const char *s = \"123\";\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 's[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:21]: (error) Array 's[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo()\n" "{\n" " char *s; s = \"\";\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 's[1]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:18]: (error) Array 's[1]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo() {\n" " const char *s = \"\";\n" From 4caa17c58701069af9b4c1f274dc82f80a89bf30 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:40:53 +0100 Subject: [PATCH 07/11] Update token.h [skip ci] --- lib/token.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/token.h b/lib/token.h index 5e200f63b9c..cf38191dbfa 100644 --- a/lib/token.h +++ b/lib/token.h @@ -1415,7 +1415,7 @@ class CPPCHECKLIB Token { const ValueFlow::Value* getContainerSizeValue(MathLib::bigint val) const; const Token *getValueTokenMaxStrLength() const; - const Token *getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path = nullptr) const; + const Token *getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path = nullptr, ErrorPath* errorPath = nullptr) const; /** Add token value. Return true if value is added. */ bool addValue(const ValueFlow::Value &value); From 18a195d10c7a7a4c907735bc76ab6abb6be80b78 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:41:10 +0100 Subject: [PATCH 08/11] Update token.cpp --- lib/token.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/token.cpp b/lib/token.cpp index 3f8e2a88d4c..d9ec6c9a909 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1978,12 +1978,13 @@ const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, nonneg int ar return ret; } -const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path) const +const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::bigint* path, ErrorPath* errorPath) const { if (!mImpl->mValues) return nullptr; const Token *ret = nullptr; int minsize = INT_MAX; + auto itMin = mImpl->mValues->end(); for (auto it = mImpl->mValues->begin(); it != mImpl->mValues->end(); ++it) { if (it->isTokValue() && it->tokvalue && it->tokvalue->tokType() == Token::eString) { const int size = getStrSize(it->tokvalue, settings); @@ -1992,9 +1993,12 @@ const Token *Token::getValueTokenMinStrSize(const Settings &settings, MathLib::b ret = it->tokvalue; if (path) *path = it->path; + itMin = it; } } } + if (errorPath && itMin != mImpl->mValues->end()) + errorPath->insert(errorPath->end(), itMin->errorPath.begin(), itMin->errorPath.end()); return ret; } From 97b9e686ba039a57bea55926bc021d0e4d65ce71 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:26:34 +0100 Subject: [PATCH 09/11] Update testbufferoverrun.cpp [skip ci] --- test/testbufferoverrun.cpp | 74 +++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index addd01371a1..eba322b443c 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -442,7 +442,7 @@ class TestBufferOverrun : public TestFixture { " str[i] = 0;\n" "}\n" "void b() { a(16); }\n"); - ASSERT_EQUALS("[test.cpp:4:8] -> [test.cpp:3:15]: (error) Array 'str[16]' accessed at index 16, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:15] -> [test.cpp:4:8]: (error) Array 'str[16]' accessed at index 16, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } void array_index_4() { @@ -1304,7 +1304,7 @@ class TestBufferOverrun : public TestFixture { " p[10] = 7;\n" " free(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:4] -> [test.cpp:3:14]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:14] -> [test.cpp:4:4]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" @@ -1312,7 +1312,7 @@ class TestBufferOverrun : public TestFixture { " p[10] = 7;\n" " free(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:4] -> [test.cpp:3:15]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:15] -> [test.cpp:4:4]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" @@ -2711,14 +2711,14 @@ class TestBufferOverrun : public TestFixture { " const char *str = \"abc\";\n" " bar(str[10]);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3:12] -> [test.cpp:2:23]: (error) Array 'str[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:23] -> [test.cpp:3:12]: (error) Array 'str[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" " const char *str = \"abc\";\n" " bar(str[4]);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:12] -> [test.cpp:3:23]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:23] -> [test.cpp:4:12]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f()\n" "{\n" @@ -2732,7 +2732,7 @@ class TestBufferOverrun : public TestFixture { " const char *str = \"a\tc\";\n" " bar(str[4]);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:12] -> [test.cpp:3:23]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:23] -> [test.cpp:4:12]: (error) Array 'str[4]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" // #6973 " const char *name = \"\";\n" @@ -2937,7 +2937,7 @@ class TestBufferOverrun : public TestFixture { " p[5] = 1;\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:4:6] -> [test.cpp:3:20]: (error) Array 'p[3]' accessed at index 5, which is out of bounds. [arrayIndexOutOfBounds]\n", + "[test.cpp:3:20] -> [test.cpp:4:6]: (error) Array 'p[3]' accessed at index 5, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" @@ -3032,7 +3032,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:4:10]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int* z = new int(1);\n" @@ -3040,7 +3040,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:4:10]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int* z = new int{};\n" @@ -3048,7 +3048,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:4:10]: (error) Array 'z[1]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int* z = new int[5];\n" @@ -3056,7 +3056,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 0;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void g() {\n" " int* z = new int[5]();\n" @@ -3064,7 +3064,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = 1;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:12]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:12] -> [test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5];\n" @@ -3072,7 +3072,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5]();\n" @@ -3080,7 +3080,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5]{};\n" @@ -3088,7 +3088,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void h() {\n" " int** z = new int* [5]{ 0 };\n" @@ -3096,7 +3096,7 @@ class TestBufferOverrun : public TestFixture { " z[n] = nullptr;\n" " delete[] z;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10] -> [test.cpp:2:13]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:13] -> [test.cpp:4:10]: (error) Array 'z[5]' accessed at index 7, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } void buffer_overrun_2_struct() { @@ -3806,9 +3806,9 @@ class TestBufferOverrun : public TestFixture { "void f() {\n" " g(\"abc\");\n" "}\n", s); - ASSERT_EQUALS("[test.cpp:5:7]: error: Array 's[4]' accessed at index 6, which is out of bounds. [arrayIndexOutOfBounds]\n" - "[test.cpp:2:13]: note: Array index out of bounds\n" - "[test.cpp:5:7]: note: Calling function 'g', 1st argument '\"abc\"' value is \"abc\"\n", + ASSERT_EQUALS("[test.cpp:2:13]: error: Array 's[4]' accessed at index 6, which is out of bounds. [arrayIndexOutOfBounds]\n" + "[test.cpp:5:7]: note: Calling function 'g', 1st argument '\"abc\"' value is \"abc\"\n" + "[test.cpp:2:13]: note: Array index out of bounds\n", errout_str()); } @@ -4082,7 +4082,7 @@ class TestBufferOverrun : public TestFixture { " if (cond) x = \"abcde\";\n" " return x[20];\n" // <- array index out of bounds when x is "abcde" "}\n"); - ASSERT_EQUALS("[test.cpp:4:11] -> [test.cpp:3:17] -> [test.cpp:3:7]: (error) Array 'x[6]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:17] -> [test.cpp:3:7] -> [test.cpp:4:11]: (error) Array 'x[6]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } void pointer_out_of_bounds_1() { @@ -4322,7 +4322,7 @@ class TestBufferOverrun : public TestFixture { " char *s; s = new char[10];\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:16]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:16] -> [test.cpp:4:6]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #1670 - false negative when using return check("char f()\n" @@ -4330,7 +4330,7 @@ class TestBufferOverrun : public TestFixture { " int *s; s = new int[10];\n" " return s[10];\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:13] -> [test.cpp:3:15]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:15] -> [test.cpp:4:13]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("struct Fred { char c[10]; };\n" "char f()\n" @@ -4363,7 +4363,7 @@ class TestBufferOverrun : public TestFixture { " buf[9] = 0;\n" " delete [] buf;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:6:6] -> [test.cpp:5:7]: (error) Array 'buf[9]' accessed at index 9, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5:7] -> [test.cpp:6:6]: (error) Array 'buf[9]' accessed at index 9, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo()\n" "{\n" @@ -4371,7 +4371,7 @@ class TestBufferOverrun : public TestFixture { " char *s; s = new char[Size];\n" " s[Size] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:6] -> [test.cpp:4:16]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:16] -> [test.cpp:5:6]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo()\n" "{\n" @@ -4379,7 +4379,7 @@ class TestBufferOverrun : public TestFixture { " E *e; e = new E[10];\n" " e[10] = ZERO;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:6] -> [test.cpp:4:13]: (error) Array 'e[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:13] -> [test.cpp:5:6]: (error) Array 'e[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } // data is allocated with malloc @@ -4389,14 +4389,14 @@ class TestBufferOverrun : public TestFixture { " char *s; s = (char *)malloc(10);\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:16]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:16] -> [test.cpp:4:6]: (error) Array 's[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #842 check("void f() {\n" " int *tab4 = (int *)malloc(20 * sizeof(int));\n" " tab4[20] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3:9] -> [test.cpp:2:15]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:15] -> [test.cpp:3:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #1478 check("void foo() {\n" @@ -4405,7 +4405,7 @@ class TestBufferOverrun : public TestFixture { " p = (char *)malloc(10);\n" " p[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:6] -> [test.cpp:4:7]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:4:7] -> [test.cpp:5:6]: (error) Array 'p[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); // ticket #1134 check("void f() {\n" @@ -4413,7 +4413,7 @@ class TestBufferOverrun : public TestFixture { " x = (int *)malloc(10 * sizeof(int));\n" " x[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:7]: (error) Array 'x[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:7] -> [test.cpp:4:6]: (error) Array 'x[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int *tab4; tab4 = malloc(20 * sizeof(int));\n" @@ -4439,21 +4439,21 @@ class TestBufferOverrun : public TestFixture { " E *tab4 = (E *)malloc(Size * 4);\n" " tab4[Size] = Size;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:3:13]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:13] -> [test.cpp:4:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " enum E { Size = 20 };\n" " E *tab4 = (E *)malloc(4 * Size);\n" " tab4[Size] = Size;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:3:13]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:13] -> [test.cpp:4:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " enum E { ZERO };\n" " E *tab4 = (E *)malloc(20 * sizeof(E));\n" " tab4[20] = ZERO;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:9] -> [test.cpp:3:13]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:13] -> [test.cpp:4:9]: (error) Array 'tab4[20]' accessed at index 20, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" // #8721 " unsigned char **cache = malloc(32);\n" @@ -4467,14 +4467,14 @@ class TestBufferOverrun : public TestFixture { " for (int i = 0; i < 3; i++)\n" " a[i] = NULL;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:2:11]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:11] -> [test.cpp:4:6]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f() {\n" " int **a = new int*[2];\n" " for (int i = 0; i < 3; i++)\n" " a[i] = NULL;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:2:11]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:11] -> [test.cpp:4:6]: (error) Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void f(const uint8_t* a) {\n" // 10421 " uint8_t* p = (uint8_t*)malloc(20U * sizeof(uint8_t));\n" @@ -4483,7 +4483,7 @@ class TestBufferOverrun : public TestFixture { " p[i] = a[i];\n" " free(p);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:10] -> [test.cpp:2:16] -> [test.cpp:3:9]: (error) Array 'p[20]' accessed at index 29, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:2:16] -> [test.cpp:3:9] -> [test.cpp:5:10]: (error) Array 'p[20]' accessed at index 29, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); } // statically allocated buffer @@ -4493,14 +4493,14 @@ class TestBufferOverrun : public TestFixture { " const char *s = \"123\";\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:21]: (error) Array 's[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:21] -> [test.cpp:4:6]: (error) Array 's[4]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo()\n" "{\n" " char *s; s = \"\";\n" " s[10] = 0;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:6] -> [test.cpp:3:18]: (error) Array 's[1]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:3:18] -> [test.cpp:4:6]: (error) Array 's[1]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]\n", errout_str()); check("void foo() {\n" " const char *s = \"\";\n" From f7ba0dfc90ae42b6720ed9cf0d3160508d30ea48 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:27:00 +0100 Subject: [PATCH 10/11] Update checkbufferoverrun.h [skip ci] --- lib/checkbufferoverrun.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checkbufferoverrun.h b/lib/checkbufferoverrun.h index 0e7e6653ea6..7564900fdcc 100644 --- a/lib/checkbufferoverrun.h +++ b/lib/checkbufferoverrun.h @@ -103,7 +103,7 @@ class CPPCHECKLIB CheckBufferOverrunImpl : public CheckImpl void arrayIndexError(const Token* tok, const std::vector& dimensions, const std::vector& indexes, - const ErrorPath& errorPathArray); + ErrorPath errorPath); void negativeIndexError(const Token* tok, const std::vector& dimensions, const std::vector& indexes); From 54fd8e469bcd526c35eca9d8845075a3f54f0466 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:27:25 +0100 Subject: [PATCH 11/11] Update checkbufferoverrun.cpp --- lib/checkbufferoverrun.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 560219e8666..742d5dbb6a8 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -422,7 +422,7 @@ static std::string arrayIndexMessage(const Token* tok, void CheckBufferOverrunImpl::arrayIndexError(const Token* tok, const std::vector& dimensions, const std::vector& indexes, - const ErrorPath& errorPathArray) + ErrorPath errorPath) { if (!tok) { reportError(tok, Severity::error, "arrayIndexOutOfBounds", "Array 'arr[16]' accessed at index 16, which is out of bounds.", CWE_BUFFER_OVERRUN, Certainty::normal); @@ -441,8 +441,8 @@ void CheckBufferOverrunImpl::arrayIndexError(const Token* tok, index = &indexValue; } - ErrorPath errorPath = getErrorPath(tok, index, "Array index out of bounds"); - errorPath.insert(errorPath.end(), errorPathArray.begin(), errorPathArray.end()); + const ErrorPath errorPathTok = getErrorPath(tok, index, "Array index out of bounds"); + errorPath.insert(errorPath.end(), errorPathTok.begin(), errorPathTok.end()); reportError(errorPath, index->errorSeverity() ? Severity::error : Severity::warning,