Skip to content

Commit 921f5c1

Browse files
committed
Fixed #10547 (False positive: constVariable, buffer written by stream read)
1 parent c49d246 commit 921f5c1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/checkother.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1525,8 +1525,13 @@ void CheckOther::checkConstPointer()
15251525
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(true) || p->scope()->function->hasVirtualSpecifier())
15261526
continue;
15271527
}
1528-
if (nonConstPointers.find(p) == nonConstPointers.end())
1528+
if (nonConstPointers.find(p) == nonConstPointers.end()) {
1529+
const Token *start = (p->isArgument()) ? p->scope()->bodyStart : p->nameToken()->next();
1530+
const int indirect = p->isArray() ? p->dimensions().size() : 1;
1531+
if (isVariableChanged(start, p->scope()->bodyEnd, indirect, p->declarationId(), false, mSettings, mTokenizer->isCPP()))
1532+
continue;
15291533
constVariableError(p, nullptr);
1534+
}
15301535
}
15311536
}
15321537
void CheckOther::constVariableError(const Variable *var, const Function *function)

test/testother.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,6 +2764,13 @@ class TestOther : public TestFixture {
27642764
" memcpy(data.buf, &a, sizeof(a));\n"
27652765
"}");
27662766
ASSERT_EQUALS("", errout.str());
2767+
2768+
// #10547
2769+
check("void foo(std::istream &istr) {\n"
2770+
" unsigned char x[2];\n"
2771+
" istr >> x[0];\n"
2772+
"}");
2773+
ASSERT_EQUALS("", errout.str());
27672774
}
27682775

27692776
void switchRedundantAssignmentTest() {

0 commit comments

Comments
 (0)