Skip to content

Commit dbf50fd

Browse files
committed
Fix the ASAN issue
1 parent d214ce0 commit dbf50fd

1 file changed

Lines changed: 29 additions & 15 deletions

File tree

test/testvalueflow.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,22 @@ class TestValueFlow : public TestFixture {
517517
return values;
518518
}
519519

520+
// The expression of the container recorded by the container size value of the token. The
521+
// container token has to be resolved while the tokenizer is alive.
522+
#define containerOfSizeValue(...) containerOfSizeValue_(__FILE__, __LINE__, __VA_ARGS__)
523+
std::string containerOfSizeValue_(const char* file, int line, const char code[], const char tokstr[]) {
524+
SimpleTokenizer tokenizer(settings, *this);
525+
ASSERT_LOC(tokenizer.tokenize(code), file, line);
526+
const Token* tok = Token::findmatch(tokenizer.tokens(), tokstr);
527+
if (!tok)
528+
return "";
529+
for (const ValueFlow::Value& v : tok->values()) {
530+
if (v.isContainerSizeValue() && v.container)
531+
return v.container->expressionString();
532+
}
533+
return "";
534+
}
535+
520536
#define lifetimeValues(...) lifetimeValues_(__FILE__, __LINE__, __VA_ARGS__)
521537
template<size_t size>
522538
std::vector<std::string> lifetimeValues_(const char* file, int line, const char (&code)[size], const char tokstr[]) {
@@ -7723,22 +7739,21 @@ class TestValueFlow : public TestFixture {
77237739
" std::vector<int> v(3);\n"
77247740
" return v.data();\n"
77257741
"}";
7726-
const std::list<ValueFlow::Value> dataValues =
7727-
tokenValues(code, "( ) ;", ValueFlow::Value::ValueType::CONTAINER_SIZE);
7728-
ASSERT_EQUALS("", isKnownContainerSizeValue(dataValues, 3));
7729-
ASSERT(dataValues.front().container);
7730-
ASSERT_EQUALS("v", dataValues.front().container->str());
7742+
ASSERT_EQUALS("",
7743+
isKnownContainerSizeValue(tokenValues(code, "( ) ;", ValueFlow::Value::ValueType::CONTAINER_SIZE),
7744+
3));
7745+
ASSERT_EQUALS("v", containerOfSizeValue(code, "( ) ;"));
77317746

77327747
// an empty associative container implies that its default-inserted elements are empty as well
77337748
code = "void f(const std::string& k) {\n"
77347749
" std::map<std::string, std::vector<int>> m;\n"
77357750
" m[k].front();\n"
77367751
"}";
7737-
const std::list<ValueFlow::Value> elementValues =
7738-
tokenValues(code, "[ k ] . front", ValueFlow::Value::ValueType::CONTAINER_SIZE);
7739-
ASSERT_EQUALS("", isKnownContainerSizeValue(elementValues, 0));
7740-
ASSERT(elementValues.front().container);
7741-
ASSERT_EQUALS("m[k]", elementValues.front().container->expressionString());
7752+
ASSERT_EQUALS(
7753+
"",
7754+
isKnownContainerSizeValue(tokenValues(code, "[ k ] . front", ValueFlow::Value::ValueType::CONTAINER_SIZE),
7755+
0));
7756+
ASSERT_EQUALS("m[k]", containerOfSizeValue(code, "[ k ] . front"));
77427757

77437758
// ..also for nested associative containers..
77447759
code = "void f(int a, int b) {\n"
@@ -7830,11 +7845,10 @@ class TestValueFlow : public TestFixture {
78307845
" auto it = v.begin();\n"
78317846
" if (it != v.end()) {}\n"
78327847
"}";
7833-
const std::list<ValueFlow::Value> itValues =
7834-
tokenValues(code, "it !=", ValueFlow::Value::ValueType::CONTAINER_SIZE);
7835-
ASSERT_EQUALS("", isKnownContainerSizeValue(itValues, 3));
7836-
ASSERT(itValues.front().container);
7837-
ASSERT_EQUALS("v", itValues.front().container->str());
7848+
ASSERT_EQUALS(
7849+
"",
7850+
isKnownContainerSizeValue(tokenValues(code, "it !=", ValueFlow::Value::ValueType::CONTAINER_SIZE), 3));
7851+
ASSERT_EQUALS("v", containerOfSizeValue(code, "it !="));
78387852
}
78397853

78407854
void valueFlowContainerElement()

0 commit comments

Comments
 (0)