Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #13734 - FP unsignedLessThanZero for unknown array size #7410

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3747,13 +3747,15 @@ bool Variable::arrayDimensions(const Settings& settings, bool& isContainer)
// TODO: collect timing information for this call?
ValueFlow::valueFlowConstantFoldAST(const_cast<Token *>(dimension_.tok), settings);
if (dimension_.tok) {
if (const ValueFlow::Value* v = dimension_.tok->getKnownValue(ValueFlow::Value::ValueType::INT))
if (const ValueFlow::Value* v = dimension_.tok->getKnownValue(ValueFlow::Value::ValueType::INT)) {
dimension_.num = v->intvalue;
dimension_.known = true;
}
else if (dimension_.tok->isTemplateArg() && !dimension_.tok->values().empty()) {
assert(dimension_.tok->values().size() == 1);
dimension_.num = dimension_.tok->values().front().intvalue;
dimension_.known = true;
}
dimension_.known = true;
}
}
mDimensions.push_back(dimension_);
Expand Down
7 changes: 7 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8852,6 +8852,13 @@ class TestOther : public TestFixture {
" f<uint32_t>(0);\n"
"}");
ASSERT_EQUALS("", errout_str());

// #13734
check("void f() {\n"
" uint8_t a[N + 1];\n"
" for (unsigned p = 0; p < (sizeof(a) / sizeof((a)[0])); ++p) {}\n"
"}");
ASSERT_EQUALS("", errout_str());
}

void checkSignOfPointer() {
Expand Down
10 changes: 10 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,16 @@ class TestValueFlow : public TestFixture {
values = tokenValues(code, "=");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(4LL * 5, values.back().intvalue);

// #13734
code = "void f() {\n"
" int a[N + 1];"
" x = sizeof(a) / sizeof(a[0]);\n"
"}";
values = tokenValues(code,"/");
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(-1, values.back().intvalue);
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Impossible, values.back().valueKind);
}

void valueFlowComma()
Expand Down
Loading