Skip to content
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
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ bool astIsUnknownSignChar(const Token *tok)

bool astIsGenericChar(const Token* tok)
{
return tok && tok->valueType() && (tok->valueType()->type == ValueType::Type::CHAR || tok->valueType()->type == ValueType::Type::WCHAR_T);
return !astIsPointer(tok) && tok && tok->valueType() && (tok->valueType()->type == ValueType::Type::CHAR || tok->valueType()->type == ValueType::Type::WCHAR_T);
}

bool astIsIntegral(const Token *tok, bool unknown)
Expand Down
11 changes: 6 additions & 5 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6957,18 +6957,19 @@ static std::vector<ValueFlow::Value> getInitListSize(const Token* tok,
if (!args.empty() && container->stdStringLike) {
if (astIsGenericChar(args[0])) // init list of chars
return { makeContainerSizeValue(args.size(), known) };
if (astIsIntegral(args[0], false)) {
if (astIsIntegral(args[0], false)) { // { count, 'c' }
if (args.size() > 1)
return {makeContainerSizeValue(args[0], known)};
} else if (astIsPointer(args[0])) {
// TODO: Try to read size of string literal
if (args.size() == 2 && astIsIntegral(args[1], false))
// TODO: Try to read size of string literal { "abc" }
if (args.size() == 2 && astIsIntegral(args[1], false)) // { char*, count }
return {makeContainerSizeValue(args[1], known)};
} else if (astIsContainer(args[0])) {
if (args.size() == 1)
if (args.size() == 1) // copy constructor { str }
return getContainerValues(args[0]);
if (args.size() == 3)
if (args.size() == 3) // { str, pos, count }
return {makeContainerSizeValue(args[2], known)};
// TODO: { str, pos }, { ..., alloc }
}
return {};
} else if ((args.size() == 1 && astIsContainer(args[0]) && args[0]->valueType()->container == container) ||
Expand Down
7 changes: 7 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5126,6 +5126,13 @@ class TestValueFlow : public TestFixture {
"}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "s . size"), 3));

code = "void f(const char* p) {\n"
" if (p == nullptr) return;\n"
" std::string s { p };\n" // size of s is unknown
" s.front();\n"
"}";
ASSERT(tokenValues(code, "s . front").empty());

code = "void f() {\n"
" std::string s = { 'a', 'b', 'c' };\n" // size of s is 3
" s.size();\n"
Expand Down