Skip to content

Commit ffa42a8

Browse files
committed
fix out-of-range shifts for ValueType constness/volatileness bits
ValueType::constness and ValueType::volatileness have one bit per pointer level and were set and tested with 1 << pointer. The pointer depth comes from the analysed code, so a declaration with 32 or more '*' shifted an int by 32 or more, which is undefined behaviour. On x86 the shift count wraps and level 33 aliased level 1. Add qualifierBit(), which returns 0 for levels that do not fit in the mask, and use it at every site in symboldatabase.cpp.
1 parent e1b5791 commit ffa42a8

2 files changed

Lines changed: 60 additions & 34 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@
5757
#include <utility>
5858
//---------------------------------------------------------------------------
5959

60+
// Bit for the given indirection level in ValueType::constness / ValueType::volatileness.
61+
// Only the first 32 levels have a bit; deeper levels are not tracked.
62+
static int qualifierBit(int indirection)
63+
{
64+
if (indirection < 0 || indirection >= 32)
65+
return 0;
66+
return static_cast<int>(1U << indirection);
67+
}
68+
6069
SymbolDatabase::SymbolDatabase(Tokenizer& tokenizer)
6170
: mTokenizer(tokenizer)
6271
, mSettings(tokenizer.getSettings())
@@ -2671,8 +2680,8 @@ void Variable::setValueType(const ValueType &vt)
26712680
mValueType = tmp;
26722681
if ((mValueType->pointer > 0) && (!isArray() || Token::Match(mNameToken->previous(), "( * %name% )")))
26732682
setFlag(fIsPointer, true);
2674-
setFlag(fIsConst, mValueType->constness & (1U << mValueType->pointer));
2675-
setFlag(fIsVolatile, mValueType->volatileness & (1U << mValueType->pointer));
2683+
setFlag(fIsConst, mValueType->constness & qualifierBit(mValueType->pointer));
2684+
setFlag(fIsVolatile, mValueType->volatileness & qualifierBit(mValueType->pointer));
26762685
if (mValueType->smartPointerType)
26772686
setFlag(fIsSmartPointer, true);
26782687
}
@@ -6183,9 +6192,9 @@ const Function* Scope::findFunction(const Token* tok, bool requireConst, Referen
61836192
while (Token::Match(typeToken, "%name%|*|&|::|<"))
61846193
{
61856194
if (typeToken->str() == "const")
6186-
ret.constness |= (1 << ret.pointer);
6195+
ret.constness |= qualifierBit(ret.pointer);
61876196
else if (typeToken->str() == "volatile")
6188-
ret.volatileness |= (1 << ret.pointer);
6197+
ret.volatileness |= qualifierBit(ret.pointer);
61896198
else if (typeToken->str() == "*")
61906199
ret.pointer++;
61916200
else if (typeToken->str() == "<") {
@@ -6939,13 +6948,13 @@ void SymbolDatabase::setValueType(Token* tok, const Variable& var, const SourceL
69396948
if (var.isArray()) // constness propagates to arrays, but not to regular pointers
69406949
valuetype.constness |= 1;
69416950
else
6942-
valuetype.constness |= (1 << valuetype.pointer);
6951+
valuetype.constness |= qualifierBit(valuetype.pointer);
69436952
}
69446953
if (vt && (vt->volatileness & 1) != 0) {
69456954
if (var.isArray())
69466955
valuetype.volatileness |= 1;
69476956
else
6948-
valuetype.volatileness |= (1 << valuetype.pointer);
6957+
valuetype.volatileness |= qualifierBit(valuetype.pointer);
69496958
}
69506959
}
69516960
setValueType(tok, valuetype);
@@ -7078,16 +7087,16 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
70787087
autoTok = var1Tok->tokAt(-3);
70797088
if (autoTok) {
70807089
ValueType vt(*vt2);
7081-
if (vt.constness & (1 << vt.pointer))
7082-
vt.constness &= ~(1 << vt.pointer);
7083-
if (vt.volatileness & (1 << vt.pointer))
7084-
vt.volatileness &= ~(1 << vt.pointer);
7090+
if (vt.constness & qualifierBit(vt.pointer))
7091+
vt.constness &= ~qualifierBit(vt.pointer);
7092+
if (vt.volatileness & qualifierBit(vt.pointer))
7093+
vt.volatileness &= ~qualifierBit(vt.pointer);
70857094
if (autoTok->strAt(1) == "*" && vt.pointer)
70867095
vt.pointer--;
70877096
if (Token::Match(autoTok->tokAt(-1), "const|constexpr"))
7088-
vt.constness |= (1 << vt.pointer);
7097+
vt.constness |= qualifierBit(vt.pointer);
70897098
if (Token::simpleMatch(autoTok->tokAt(-1), "volatile"))
7090-
vt.volatileness |= (1 << vt.pointer);
7099+
vt.volatileness |= qualifierBit(vt.pointer);
70917100
setValueType(autoTok, vt);
70927101
setAutoTokenProperties(autoTok);
70937102
if (vt2->pointer > vt.pointer)
@@ -7104,18 +7113,18 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
71047113
ValueType vt2_(*vt2);
71057114
if (vt2_.pointer == 0 && autoTok->strAt(1) == "*")
71067115
vt2_.pointer = 1;
7107-
if ((vt.constness & (1 << vt2->pointer)) != 0)
7108-
vt2_.constness |= (1 << vt2->pointer);
7109-
if ((vt.volatileness & (1 << vt2->pointer)) != 0)
7110-
vt2_.volatileness |= (1 << vt2->pointer);
7116+
if ((vt.constness & qualifierBit(vt2->pointer)) != 0)
7117+
vt2_.constness |= qualifierBit(vt2->pointer);
7118+
if ((vt.volatileness & qualifierBit(vt2->pointer)) != 0)
7119+
vt2_.volatileness |= qualifierBit(vt2->pointer);
71117120
if (!Token::Match(autoTok->tokAt(1), "*|&")) {
71127121
vt2_.constness = vt.constness;
71137122
vt2_.volatileness = vt.volatileness;
71147123
}
71157124
if (Token::simpleMatch(autoTok->tokAt(1), "* const"))
7116-
vt2_.constness |= (1 << vt2->pointer);
7125+
vt2_.constness |= qualifierBit(vt2->pointer);
71177126
if (Token::simpleMatch(autoTok->tokAt(1), "* volatile"))
7118-
vt2_.volatileness |= (1 << vt2->pointer);
7127+
vt2_.volatileness |= qualifierBit(vt2->pointer);
71197128
var->setValueType(vt2_);
71207129
if (vt2->typeScope && vt2->typeScope->definedType) {
71217130
var->type(vt2->typeScope->definedType);
@@ -7259,13 +7268,13 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
72597268
varvt.reference = Reference::LValue;
72607269
if (isconst) {
72617270
if (varvt.pointer && varvt.reference != Reference::None)
7262-
varvt.constness |= (1 << varvt.pointer);
7271+
varvt.constness |= qualifierBit(varvt.pointer);
72637272
else
72647273
varvt.constness |= 1;
72657274
}
72667275
if (isvolatile) {
72677276
if (varvt.pointer && varvt.reference != Reference::None)
7268-
varvt.volatileness |= (1 << varvt.pointer);
7277+
varvt.volatileness |= qualifierBit(varvt.pointer);
72697278
else
72707279
varvt.volatileness |= 1;
72717280
}
@@ -7340,9 +7349,9 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
73407349
if (autoToken->strAt(1) == "*" && autovt.pointer)
73417350
autovt.pointer--;
73427351
if (isconst)
7343-
varvt.constness |= (1 << autovt.pointer);
7352+
varvt.constness |= qualifierBit(autovt.pointer);
73447353
if (isvolatile)
7345-
varvt.volatileness |= (1 << autovt.pointer);
7354+
varvt.volatileness |= qualifierBit(autovt.pointer);
73467355
setValueType(parent->previous(), varvt);
73477356
auto * var = const_cast<Variable *>(parent->previous()->variable());
73487357
if (var) {
@@ -7389,9 +7398,9 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
73897398
else if (typeTok->str() == "*")
73907399
pointer++;
73917400
else if (typeTok->str() == "const")
7392-
constness |= 1 << pointer;
7401+
constness |= qualifierBit(pointer);
73937402
else if (typeTok->str() == "volatile")
7394-
volatileness |= 1 << pointer;
7403+
volatileness |= qualifierBit(pointer);
73957404
typeTok = typeTok->previous();
73967405
}
73977406

@@ -7406,10 +7415,10 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, const
74067415
varVt->volatileness |= volatileness;
74077416

74087417
if (Token::simpleMatch(typeTok->previous(), "const auto"))
7409-
varVt->constness |= 1 << pointer;
7418+
varVt->constness |= qualifierBit(pointer);
74107419

74117420
if (Token::simpleMatch(typeTok->previous(), "volatile auto"))
7412-
varVt->volatileness |= 1 << pointer;
7421+
varVt->volatileness |= qualifierBit(pointer);
74137422

74147423
varTok->setValueType(varVt);
74157424

@@ -7647,9 +7656,9 @@ static const Token* parsedecl(const Token* type,
76477656
type->type()->typeStart->str() != type->str() && type->type()->typeStart != previousType)
76487657
parsedecl(type->type()->typeStart, valuetype, defaultSignedness, settings);
76497658
else if (Token::Match(type, "const|constexpr"))
7650-
valuetype->constness |= (1 << (valuetype->pointer - pointer0));
7659+
valuetype->constness |= qualifierBit(valuetype->pointer - pointer0);
76517660
else if (Token::simpleMatch(type, "volatile"))
7652-
valuetype->volatileness |= (1 << (valuetype->pointer - pointer0));
7661+
valuetype->volatileness |= qualifierBit(valuetype->pointer - pointer0);
76537662
else if (settings.clang && type->str().size() > 2 && type->str().find("::") < type->str().find('<')) {
76547663
TokenList typeTokens(settings, type->isCpp() ? Standards::Language::CPP : Standards::Language::C);
76557664
std::string::size_type pos1 = 0;
@@ -7713,9 +7722,9 @@ static const Token* parsedecl(const Token* type,
77137722
valuetype->reference = Reference::RValue;
77147723
}
77157724
if (type->str() == "const")
7716-
valuetype->constness |= (1 << valuetype->pointer);
7725+
valuetype->constness |= qualifierBit(valuetype->pointer);
77177726
if (type->str() == "volatile")
7718-
valuetype->volatileness |= (1 << valuetype->pointer);
7727+
valuetype->volatileness |= qualifierBit(valuetype->pointer);
77197728
type = type->next();
77207729
}
77217730
break;
@@ -7914,7 +7923,7 @@ void SymbolDatabase::setGenericValueType(Token *par)
79147923

79157924
const auto matchVt = [](const ValueType *control, const ValueType *type) {
79167925
// Strip top level qualifiers of controlling expression
7917-
const unsigned int controlMask = ~(1U << control->pointer);
7926+
const unsigned int controlMask = ~static_cast<unsigned int>(qualifierBit(control->pointer));
79187927
return control->isTypeEqual(type) &&
79197928
control->sign == type->sign &&
79207929
(static_cast<unsigned int>(control->constness) & controlMask) == static_cast<unsigned int>(type->constness) &&
@@ -8668,14 +8677,14 @@ bool ValueType::isConst(nonneg int indirect) const
86688677
{
86698678
if (indirect > pointer)
86708679
return false;
8671-
return constness & (1 << (pointer - indirect));
8680+
return constness & qualifierBit(pointer - indirect);
86728681
}
86738682

86748683
bool ValueType::isVolatile(nonneg int indirect) const
86758684
{
86768685
if (indirect > pointer)
86778686
return false;
8678-
return volatileness & (1 << (pointer - indirect));
8687+
return volatileness & qualifierBit(pointer - indirect);
86798688
}
86808689

86818690
namespace {
@@ -8967,7 +8976,7 @@ ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const Va
89678976
return ValueType::MatchResult::NOMATCH; // TODO
89688977
}
89698978
if (call->pointer > 0) {
8970-
const unsigned int mask = (1U << call->pointer) - 1;
8979+
const unsigned int mask = static_cast<unsigned int>(qualifierBit(call->pointer)) - 1;
89718980
if (((call->constness | func->constness) & mask) != (func->constness & mask))
89728981
return ValueType::MatchResult::NOMATCH;
89738982
if ((call->volatileness | func->volatileness) != func->volatileness)

‎test/testsymboldatabase.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ class TestSymbolDatabase : public TestFixture {
211211
TEST_CASE(isVariablePointerToConstVolatilePointer);
212212
TEST_CASE(isVariableMultiplePointersAndQualifiers);
213213
TEST_CASE(variableVolatile);
214+
TEST_CASE(variableDeepPointer);
214215
TEST_CASE(variableConstexpr);
215216
TEST_CASE(isVariableDecltype);
216217
TEST_CASE(isVariableAlignas);
@@ -1874,6 +1875,22 @@ class TestSymbolDatabase : public TestFixture {
18741875
ASSERT(y->variable()->isVolatile());
18751876
}
18761877

1878+
void variableDeepPointer() {
1879+
// const/volatile only apply to the first pointer level
1880+
GET_SYMBOL_DB("int * const volatile ******************************** p;\n");
1881+
1882+
const Token *p = Token::findsimplematch(tokenizer.tokens(), "p");
1883+
ASSERT(p);
1884+
ASSERT(p->variable());
1885+
ASSERT(p->valueType());
1886+
ASSERT_EQUALS(33, p->valueType()->pointer);
1887+
ASSERT(!p->variable()->isConst());
1888+
ASSERT(!p->valueType()->isConst(0));
1889+
ASSERT(!p->valueType()->isVolatile(0));
1890+
ASSERT(p->valueType()->isConst(32));
1891+
ASSERT(p->valueType()->isVolatile(32));
1892+
}
1893+
18771894
void variableConstexpr() {
18781895
GET_SYMBOL_DB("constexpr int x = 16;\n");
18791896

0 commit comments

Comments
 (0)