Skip to content

Commit dad8923

Browse files
committed
fixed #14506 - arrayIndexThenCheck had the wrong severity check
1 parent 24c7f0b commit dad8923

2 files changed

Lines changed: 30 additions & 29 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,10 @@ void CheckBufferOverrun::bufferOverflowError(const Token *tok, const ValueFlow::
706706

707707
void CheckBufferOverrun::arrayIndexThenCheck()
708708
{
709-
if (!mSettings->severity.isEnabled(Severity::portability))
709+
if (!mSettings->severity.isEnabled(Severity::style))
710710
return;
711711

712-
logChecker("CheckBufferOverrun::arrayIndexThenCheck");
712+
logChecker("CheckBufferOverrun::arrayIndexThenCheck"); // style
713713

714714
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
715715
for (const Scope * const scope : symbolDatabase->functionScopes) {

test/testbufferoverrun.cpp

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ class TestBufferOverrun : public TestFixture {
3535
TestBufferOverrun() : TestFixture("TestBufferOverrun") {}
3636

3737
private:
38-
/*const*/ Settings settings0 = settingsBuilder().library("std.cfg").severity(Severity::warning).severity(Severity::style).severity(Severity::portability).build();
38+
/*const*/ Settings settings0 = settingsBuilder().library("std.cfg").severity(Severity::warning).severity(Severity::style).build();
3939
const Settings settings0_i = settingsBuilder(settings0).certainty(Certainty::inconclusive).build();
40+
const Settings settings0_p = settingsBuilder(settings0).severity(Severity::portability).build();
4041
const Settings settings1 = settingsBuilder(settings0).severity(Severity::performance).certainty(Certainty::inconclusive).build();
4142

4243
struct CheckOptions
@@ -3789,40 +3790,40 @@ class TestBufferOverrun : public TestFixture {
37893790
check("void f() {\n"
37903791
" char a[10];\n"
37913792
" char *p = a + 100;\n"
3792-
"}");
3793+
"}", settings0_p);
37933794
ASSERT_EQUALS("[test.cpp:3:17]: (portability) Undefined behaviour, pointer arithmetic 'a+100' is out of bounds. [pointerOutOfBounds]\n", errout_str());
37943795

37953796
check("char *f() {\n"
37963797
" char a[10];\n"
37973798
" return a + 100;\n"
3798-
"}");
3799+
"}", settings0_p);
37993800
ASSERT_EQUALS("[test.cpp:3:14]: (portability) Undefined behaviour, pointer arithmetic 'a+100' is out of bounds. [pointerOutOfBounds]\n", errout_str());
38003801

38013802
check("void f(int i) {\n"
38023803
" char x[10];\n"
38033804
" if (i == 123) {}\n"
38043805
" dostuff(x+i);\n"
3805-
"}");
3806+
"}", settings0_p);
38063807
ASSERT_EQUALS("[test.cpp:3:11] -> [test.cpp:4:14]: (portability) Undefined behaviour, when 'i' is 123 the pointer arithmetic 'x+i' is out of bounds. [pointerOutOfBoundsCond]\n", errout_str());
38073808

38083809
check("void f(int i) {\n"
38093810
" char x[10];\n"
38103811
" if (i == -1) {}\n"
38113812
" dostuff(x+i);\n"
3812-
"}");
3813+
"}", settings0_p);
38133814
ASSERT_EQUALS("[test.cpp:3:11] -> [test.cpp:4:14]: (portability) Undefined behaviour, when 'i' is -1 the pointer arithmetic 'x+i' is out of bounds. [pointerOutOfBoundsCond]\n", errout_str());
38143815

38153816
check("void f() {\n" // #6350 - fp when there is cast of buffer
38163817
" wchar_t buf[64];\n"
38173818
" p = (unsigned char *) buf + sizeof (buf);\n"
3818-
"}", dinit(CheckOptions, $.cpp = false));
3819+
"}", settings0_p, false);
38193820
ASSERT_EQUALS("", errout_str());
38203821

38213822
check("int f() {\n"
38223823
" const char d[] = \"0123456789\";\n"
38233824
" char *cp = d + 3;\n"
38243825
" return cp - d;\n"
3825-
"}");
3826+
"}", settings0_p);
38263827
ASSERT_EQUALS("", errout_str());
38273828
}
38283829

@@ -3831,15 +3832,15 @@ class TestBufferOverrun : public TestFixture {
38313832
" char *p = malloc(10);\n"
38323833
" p += 100;\n"
38333834
" free(p);"
3834-
"}");
3835+
"}", settings0_p);
38353836
TODO_ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'p+100' is out of bounds.\n", "", errout_str());
38363837

38373838
check("void f() {\n"
38383839
" char *p = malloc(10);\n"
38393840
" p += 10;\n"
38403841
" *p = 0;\n"
38413842
" free(p);"
3842-
"}");
3843+
"}", settings0_p);
38433844
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) p is out of bounds.\n", "", errout_str());
38443845

38453846
check("void f() {\n"
@@ -3848,7 +3849,7 @@ class TestBufferOverrun : public TestFixture {
38483849
" p -= 10;\n"
38493850
" *p = 0;\n"
38503851
" free(p);"
3851-
"}");
3852+
"}", settings0_p);
38523853
ASSERT_EQUALS("", errout_str());
38533854

38543855
check("void f() {\n"
@@ -3857,15 +3858,15 @@ class TestBufferOverrun : public TestFixture {
38573858
" p = p - 1;\n"
38583859
" *p = 0;\n"
38593860
" free(p);"
3860-
"}");
3861+
"}", settings0_p);
38613862
ASSERT_EQUALS("", errout_str());
38623863
}
38633864

38643865
void pointer_out_of_bounds_3() {
38653866
check("struct S { int a[10]; };\n"
38663867
"void f(struct S *s) {\n"
38673868
" int *p = s->a + 100;\n"
3868-
"}");
3869+
"}", settings0_p);
38693870
ASSERT_EQUALS("[test.cpp:3:19]: (portability) Undefined behaviour, pointer arithmetic 's->a+100' is out of bounds. [pointerOutOfBounds]\n", errout_str());
38703871

38713872
check("template <class T> class Vector\n"
@@ -3881,36 +3882,36 @@ class TestBufferOverrun : public TestFixture {
38813882
" const T* P2 = PDat + 1;\n"
38823883
" const T* P1 = P2 - 1;\n"
38833884
"}\n"
3884-
"Vector<std::array<long, 2>> Foo;\n");
3885+
"Vector<std::array<long, 2>> Foo;\n", settings0_p);
38853886
ASSERT_EQUALS("", errout_str());
38863887
}
38873888

38883889
void pointer_out_of_bounds_4() {
38893890
check("const char* f() {\n"
38903891
" g(\"Hello\" + 6);\n"
3891-
"}");
3892+
"}", settings0_p);
38923893
ASSERT_EQUALS("", errout_str());
38933894

38943895
check("const char* f() {\n"
38953896
" g(\"Hello\" + 7);\n"
3896-
"}");
3897+
"}", settings0_p);
38973898
ASSERT_EQUALS("[test.cpp:2:15]: (portability) Undefined behaviour, pointer arithmetic '\"Hello\"+7' is out of bounds. [pointerOutOfBounds]\n", errout_str());
38983899

38993900
check("const char16_t* f() {\n"
39003901
" g(u\"Hello\" + 6);\n"
3901-
"}");
3902+
"}", settings0_p);
39023903
ASSERT_EQUALS("", errout_str());
39033904

39043905
check("const char16_t* f() {\n"
39053906
" g(u\"Hello\" + 7);\n"
3906-
"}");
3907+
"}", settings0_p);
39073908
ASSERT_EQUALS("[test.cpp:2:16]: (portability) Undefined behaviour, pointer arithmetic 'u\"Hello\"+7' is out of bounds. [pointerOutOfBounds]\n", errout_str());
39083909

39093910
check("void f() {\n" // #4647
39103911
" int val = 5;\n"
39113912
" std::string hi = \"hi\" + val;\n"
39123913
" std::cout << hi << std::endl;\n"
3913-
"}\n");
3914+
"}\n", settings0_p);
39143915
ASSERT_EQUALS("[test.cpp:3:27]: (portability) Undefined behaviour, pointer arithmetic '\"hi\"+val' is out of bounds. [pointerOutOfBounds]\n", errout_str());
39153916

39163917
check("void f(const char* s, int len) {\n" // #11026
@@ -3920,7 +3921,7 @@ class TestBufferOverrun : public TestFixture {
39203921
"void g() {\n"
39213922
" f(\"a\", 1);\n"
39223923
" f(\"bbb\", 3);\n"
3923-
"}\n");
3924+
"}\n", settings0_p);
39243925
ASSERT_EQUALS("", errout_str());
39253926

39263927
check("void f(int i, const char* a) {\n" // #11140
@@ -3933,14 +3934,14 @@ class TestBufferOverrun : public TestFixture {
39333934
"void h() {\n"
39343935
" for (int i = 0; \"012\"[i]; ++i)\n"
39353936
" f(i, \"345\");\n"
3936-
"}\n");
3937+
"}\n", settings0_p);
39373938
ASSERT_EQUALS("", errout_str());
39383939
}
39393940

39403941
void pointer_out_of_bounds_5() { // #10227
39413942
check("int foo(char str[6]) {\n"
39423943
" return !((0 && *(\"STRING\" + 14) == 0) || memcmp(str, \"STRING\", 6) == 0);\n"
3943-
"}\n");
3944+
"}\n", settings0_p);
39443945
ASSERT_EQUALS("", errout_str());
39453946
}
39463947

@@ -3950,26 +3951,26 @@ class TestBufferOverrun : public TestFixture {
39503951
check("char *f() {\n"
39513952
" char x[10];\n"
39523953
" return x-1;\n"
3953-
"}");
3954+
"}", settings0_p);
39543955
ASSERT_EQUALS("[test.cpp:3:13]: (portability) Undefined behaviour, pointer arithmetic 'x-1' is out of bounds. [pointerOutOfBounds]\n", errout_str());
39553956

39563957
check("void f(int i) {\n"
39573958
" char x[10];\n"
39583959
" if (i == 123) {}\n"
39593960
" dostuff(x-i);\n"
3960-
"}");
3961+
"}", settings0_p);
39613962
ASSERT_EQUALS("[test.cpp:3:11] -> [test.cpp:4:14]: (portability) Undefined behaviour, when 'i' is 123 the pointer arithmetic 'x-i' is out of bounds. [pointerOutOfBoundsCond]\n", errout_str());
39623963

39633964
check("void f(int i) {\n"
39643965
" char x[10];\n"
39653966
" if (i == -20) {}\n"
39663967
" dostuff(x-i);\n"
3967-
"}");
3968+
"}", settings0_p);
39683969
TODO_ASSERT_EQUALS("[test.cpp:4]: (portability) Undefined behaviour, when 'i' is -20 the pointer arithmetic 'x-i' is out of bounds.\n", "", errout_str());
39693970

39703971
check("void f(const char *x[10]) {\n"
39713972
" return x-4;\n"
3972-
"}");
3973+
"}", settings0_p);
39733974
ASSERT_EQUALS("", errout_str());
39743975
}
39753976

@@ -5296,14 +5297,14 @@ class TestBufferOverrun : public TestFixture {
52965297
check("void f() {\n"
52975298
" char arr[10];\n"
52985299
" char *p = arr + 20;\n"
5299-
"}");
5300+
"}", settings0_p);
53005301
ASSERT_EQUALS("[test.cpp:3:19]: (portability) Undefined behaviour, pointer arithmetic 'arr+20' is out of bounds. [pointerOutOfBounds]\n", errout_str());
53015302

53025303
check("char(*g())[1];\n" // #7950
53035304
"void f() {\n"
53045305
" int a[2];\n"
53055306
" int* b = a + sizeof(*g());\n"
5306-
"}\n");
5307+
"}\n", settings0_p);
53075308
ASSERT_EQUALS("", errout_str());
53085309
}
53095310

0 commit comments

Comments
 (0)