Skip to content

Commit 7e3ba00

Browse files
committed
Fix
1 parent cc85761 commit 7e3ba00

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,19 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
636636
}
637637
break;
638638
case Library::ArgumentChecks::MinSize::Type::ARGVALUE: {
639-
if (arg && arg->hasKnownIntValue()) {
640-
MathLib::bigint myMinsize = arg->getKnownIntValue();
639+
if (arg) {
640+
const ValueFlow::Value* argVal = arg->hasKnownIntValue() ? &arg->values().front() : arg->getMaxValue(true);
641+
if (!argVal)
642+
break;
643+
MathLib::bigint myMinsize = argVal->intvalue;
641644
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
642645
if (baseSize != 0)
643646
myMinsize *= baseSize;
644647
const bool ok = myMinsize <= bufferSize.intvalue;
645648
if (!ok) {
646-
if (bufferSize.errorPath.empty())
647-
bufferSize.errorPath = arg->values().front().errorPath;
649+
bufferSize.errorPath.insert(bufferSize.errorPath.end(), argVal->errorPath.begin(), argVal->errorPath.end());
648650
if (!bufferSize.condition)
649-
bufferSize.condition = arg->values().front().condition;
651+
bufferSize.condition = argVal->condition;
650652
}
651653
return ok;
652654
}

test/testbufferoverrun.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3600,10 +3600,18 @@ class TestBufferOverrun : public TestFixture {
36003600
" int a[1];\n"
36013601
" if (i != 2) return;\n"
36023602
" memset(a, 0, i * sizeof(int));\n"
3603+
"}"
3604+
"void g(int i) {\n"
3605+
" int a[1];\n"
3606+
" if (i != 2) {}\n"
3607+
" memset(a, 0, i * sizeof(int));\n"
36033608
"}", s);
36043609
ASSERT_EQUALS("[test.cpp:4:12]: warning: Buffer is accessed out of bounds: a [bufferAccessOutOfBounds]\n"
36053610
"[test.cpp:3:11]: note: Assuming that condition 'i!=2' is not redundant\n"
3606-
"[test.cpp:4:12]: note: Buffer overrun\n", errout_str());
3611+
"[test.cpp:4:12]: note: Buffer overrun\n"
3612+
"[test.cpp:8:12]: warning: Buffer is accessed out of bounds: a [bufferAccessOutOfBounds]\n"
3613+
"[test.cpp:7:11]: note: Assuming that condition 'i!=2' is not redundant\n"
3614+
"[test.cpp:8:12]: note: Buffer overrun\n", errout_str());
36073615
}
36083616

36093617
void buffer_overrun_bailoutIfSwitch() {

0 commit comments

Comments
 (0)