| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9c5275f commit 709fec8
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5011,6 +5011,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun | |||
| 5011 | 5011 | <arg nr="3" direction="in"> | |
| 5012 | 5012 | <not-null/> | |
| 5013 | 5013 | <not-uninit/> | |
| 5014 | + <minsize type="argvalue" arg="4"/> | ||
| 5014 | 5015 | <strz/> | |
| 5015 | 5016 | </arg> | |
| 5016 | 5017 | <arg nr="4" direction="in"> | |
@@ -5096,6 +5097,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun | |||
| 5096 | 5097 | <arg nr="3" direction="in"> | |
| 5097 | 5098 | <not-null/> | |
| 5098 | 5099 | <not-uninit/> | |
| 5100 | + <minsize type="argvalue" arg="4"/> | ||
| 5099 | 5101 | <strz/> | |
| 5100 | 5102 | </arg> | |
| 5101 | 5103 | <arg nr="4" direction="in"> | |
@@ -5211,6 +5213,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun | |||
| 5211 | 5213 | <not-null/> | |
| 5212 | 5214 | <not-uninit/> | |
| 5213 | 5215 | <strz/> | |
| 5216 | + <minsize type="argvalue" arg="3"/> | ||
| 5214 | 5217 | </arg> | |
| 5215 | 5218 | <arg nr="3" direction="in"> | |
| 5216 | 5219 | <not-uninit/> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -554,25 +554,20 @@ ValueFlow::Value CheckBufferOverrun::getBufferSize(const Token *bufTok) const | |||
| 554 | 554 | return *value; | |
| 555 | 555 | } | |
| 556 | 556 | ||
| 557 | - MathLib::bigint dim = -1; | ||
| 558 | - if (var) { | ||
| 559 | - dim = std::accumulate(var->dimensions().cbegin(), var->dimensions().cend(), 1LL, [](MathLib::bigint i1, const Dimension &dim) { | ||
| 560 | - return i1 * dim.num; | ||
| 561 | - }); | ||
| 562 | - } | ||
| 563 | - else if (bufTok->tokType() == Token::Type::eString) { | ||
| 564 | - dim = Token::getStrLength(bufTok) + 1; | ||
| 565 | - } | ||
| 566 | - else | ||
| 557 | + if (!var) | ||
| 567 | 558 | return ValueFlow::Value(-1); | |
| 568 | 559 | ||
| 560 | + const MathLib::bigint dim = std::accumulate(var->dimensions().cbegin(), var->dimensions().cend(), 1LL, [](MathLib::bigint i1, const Dimension &dim) { | ||
| 561 | + return i1 * dim.num; | ||
| 562 | + }); | ||
| 563 | + | ||
| 569 | 564 | ValueFlow::Value v; | |
| 570 | 565 | v.setKnown(); | |
| 571 | 566 | v.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE; | |
| 572 | 567 | ||
| 573 | - if (var && var->isPointerArray()) | ||
| 568 | + if (var->isPointerArray()) | ||
| 574 | 569 | v.intvalue = dim * mSettings->platform.sizeof_pointer; | |
| 575 | - else if (var && var->isPointer()) | ||
| 570 | + else if (var->isPointer()) | ||
| 576 | 571 | return ValueFlow::Value(-1); | |
| 577 | 572 | else { | |
| 578 | 573 | const MathLib::bigint typeSize = bufTok->valueType()->typeSize(mSettings->platform); | |
@@ -651,7 +646,7 @@ void CheckBufferOverrun::bufferOverflow() | |||
| 651 | 646 | argtok = argtok->astOperand2() ? argtok->astOperand2() : argtok->astOperand1(); | |
| 652 | 647 | while (Token::Match(argtok, ".|::")) | |
| 653 | 648 | argtok = argtok->astOperand2(); | |
| 654 | - if (!argtok || (!argtok->variable() && argtok->tokType() != Token::Type::eString)) | ||
| 649 | + if (!argtok || !argtok->variable()) | ||
| 655 | 650 | continue; | |
| 656 | 651 | if (argtok->valueType() && argtok->valueType()->pointer == 0) | |
| 657 | 652 | continue; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -219,12 +219,14 @@ void bufferAccessOutOfBounds(void) | |||
| 219 | 219 | strncpy_s(a,5,"abcd",5); | |
| 220 | 220 | // string will be truncated, error is returned, but no buffer overflow | |
| 221 | 221 | strncpy_s(a,5,"abcde",6); | |
| 222 | + // TODO cppcheck-suppress bufferAccessOutOfBounds | ||
| 222 | 223 | strncpy_s(a,5,"a",6); | |
| 223 | 224 | strncpy_s(a,5,"abcdefgh",4); | |
| 224 | 225 | // valid call | |
| 225 | 226 | strncat_s(a,5,"1",2); | |
| 226 | 227 | // cppcheck-suppress bufferAccessOutOfBounds | |
| 227 | 228 | strncat_s(a,10,"1",2); | |
| 229 | + // TODO cppcheck-suppress bufferAccessOutOfBounds | ||
| 228 | 230 | strncat_s(a,5,"1",5); | |
| 229 | 231 | fread(a,1,5,stdin); | |
| 230 | 232 | // cppcheck-suppress bufferAccessOutOfBounds | |
@@ -516,6 +518,7 @@ void nullpointer(int value) | |||
| 516 | 518 | wcstok(NULL,L"xyz",&pWcsUninit); | |
| 517 | 519 | ||
| 518 | 520 | strxfrm(0,"foo",0); | |
| 521 | + // TODO: error message (#6306 and http://trac.cppcheck.net/changeset/d11eb4931aea51cf2cb74faccdcd2a3289b818d6/) | ||
| 519 | 522 | strxfrm(0,"foo",42); | |
| 520 | 523 | wcsxfrm(0,L"foo",0); | |
| 521 | 524 | // TODO: error message when arg1==NULL and arg3!=0 #6306: https://trac.cppcheck.net/ticket/6306#comment:2 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -238,7 +238,6 @@ class TestBufferOverrun : public TestFixture { | |||
| 238 | 238 | TEST_CASE(buffer_overrun_34); //#11035 | |
| 239 | 239 | TEST_CASE(buffer_overrun_35); //#2304 | |
| 240 | 240 | TEST_CASE(buffer_overrun_36); | |
| 241 | - TEST_CASE(buffer_overrun_37); | ||
| 242 | 241 | TEST_CASE(buffer_overrun_errorpath); | |
| 243 | 242 | TEST_CASE(buffer_overrun_bailoutIfSwitch); // ticket #2378 : bailoutIfSwitch | |
| 244 | 243 | TEST_CASE(buffer_overrun_function_array_argument); | |
@@ -3325,6 +3324,23 @@ class TestBufferOverrun : public TestFixture { | |||
| 3325 | 3324 | " (void)strxfrm(dest,src,3);\n" // << | |
| 3326 | 3325 | "}"); | |
| 3327 | 3326 | ASSERT_EQUALS("[test.cpp:6]: (error) Buffer is accessed out of bounds: dest\n", errout.str()); | |
| 3327 | + // source size is too small | ||
| 3328 | + check("void f(void) {\n" | ||
| 3329 | + " const char src[2] = \"ab\";\n" | ||
| 3330 | + " char dest[3] = \"abc\";\n" | ||
| 3331 | + " (void)strxfrm(dest,src,1);\n" | ||
| 3332 | + " (void)strxfrm(dest,src,2);\n" | ||
| 3333 | + " (void)strxfrm(dest,src,3);\n" // << | ||
| 3334 | + "}"); | ||
| 3335 | + ASSERT_EQUALS("[test.cpp:6]: (error) Buffer is accessed out of bounds: src\n", errout.str()); | ||
| 3336 | + // source size is too small | ||
| 3337 | + check("void f(void) {\n" | ||
| 3338 | + " const char src[1] = \"a\";\n" | ||
| 3339 | + " char dest[3] = \"abc\";\n" | ||
| 3340 | + " (void)strxfrm(dest,src,1);\n" | ||
| 3341 | + " (void)strxfrm(dest,src,2);\n" // << | ||
| 3342 | + "}"); | ||
| 3343 | + ASSERT_EQUALS("[test.cpp:5]: (error) Buffer is accessed out of bounds: src\n", errout.str()); | ||
| 3328 | 3344 | } | |
| 3329 | 3345 | ||
| 3330 | 3346 | void buffer_overrun_33() { // #2019 | |
@@ -3387,32 +3403,6 @@ class TestBufferOverrun : public TestFixture { | |||
| 3387 | 3403 | ASSERT_EQUALS("", errout.str()); | |
| 3388 | 3404 | } | |
| 3389 | 3405 | ||
| 3390 | - void buffer_overrun_37() { // #11765 | ||
| 3391 | - check("void f() {\n" | ||
| 3392 | - " char buf[128];\n" | ||
| 3393 | - " memcpy(buf, \"Error\", 6);\n" | ||
| 3394 | - "}\n"); | ||
| 3395 | - ASSERT_EQUALS("", errout.str()); | ||
| 3396 | - | ||
| 3397 | - check("void f() {\n" | ||
| 3398 | - " char buf[128];\n" | ||
| 3399 | - " memcpy(buf, \"Error\", 16);\n" | ||
| 3400 | - "}\n"); | ||
| 3401 | - ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: \"Error\"\n", errout.str()); | ||
| 3402 | - | ||
| 3403 | - check("void f() {\n" | ||
| 3404 | - " char buf[128];\n" | ||
| 3405 | - " memcpy(buf, L\"Error\", 10);\n" // at least 12 bytes on any platform | ||
| 3406 | - "}\n"); | ||
| 3407 | - ASSERT_EQUALS("", errout.str()); | ||
| 3408 | - | ||
| 3409 | - check("void f() {\n" | ||
| 3410 | - " char buf[128];\n" | ||
| 3411 | - " memcpy(buf, L\"Error\", 26);\n" // at most 24 bytes | ||
| 3412 | - "}\n"); | ||
| 3413 | - ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds: L\"Error\"\n", errout.str()); | ||
| 3414 | - } | ||
| 3415 | - | ||
| 3416 | 3406 | void buffer_overrun_errorpath() { | |
| 3417 | 3407 | setMultiline(); | |
| 3418 | 3408 | const Settings settingsOld = settings0; | |
@@ -4275,7 +4265,9 @@ class TestBufferOverrun : public TestFixture { | |||
| 4275 | 4265 | check("void f() {\n" | |
| 4276 | 4266 | " mymemset(\"abc\", 0, 20);\n" | |
| 4277 | 4267 | "}", settings); | |
| 4278 | - ASSERT_EQUALS("[test.cpp:2]: (error) Buffer is accessed out of bounds: \"abc\"\n", errout.str()); | ||
| 4268 | + TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Buffer is accessed out of bounds.\n", | ||
| 4269 | + "", | ||
| 4270 | + errout.str()); | ||
| 4279 | 4271 | ||
| 4280 | 4272 | check("void f() {\n" | |
| 4281 | 4273 | " mymemset(temp, \"abc\", 4);\n" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments