Skip to content

Commit 7163210

Browse files
committed
Fixed #8605 (Segmentation fault below TemplateSimplifier::simplifyCalculations)
1 parent fe8cab1 commit 7163210

2 files changed

Lines changed: 16 additions & 21 deletions

File tree

lib/templatesimplifier.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,10 +1150,12 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
11501150
{
11511151
bool ret = false;
11521152
// (1-2)
1153-
while (tok->tokAt(4) && tok->next()->isNumber() && tok->tokAt(3)->isNumber()) { // %any% %num% %any% %num% %any%
1154-
const Token *before = tok;
1155-
const Token* op = tok->tokAt(2);
1156-
const Token* after = tok->tokAt(4);
1153+
while (tok->tokAt(3) && tok->isNumber() && tok->tokAt(2)->isNumber()) { // %any% %num% %any% %num% %any%
1154+
const Token *before = tok->previous();
1155+
if (!before)
1156+
break;
1157+
const Token* op = tok->next();
1158+
const Token* after = tok->tokAt(3);
11571159
const std::string &num1 = op->previous()->str();
11581160
const std::string &num2 = op->next()->str();
11591161
if (Token::Match(before, "* %num% /") && (num2 != "0") && num1 == MathLib::multiply(num2, MathLib::divide(num1, num2))) {
@@ -1169,25 +1171,23 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
11691171
(op->str() == "||" && isLowerThanLogicalAnd(before) && isLowerThanLogicalAnd(after))))
11701172
break;
11711173

1172-
tok = tok->next();
1173-
11741174
// Don't simplify "%num% / 0"
11751175
if (Token::Match(op, "[/%] 0"))
1176-
continue;
1176+
break;
11771177

11781178
// Integer operations
11791179
if (Token::Match(op, ">>|<<|&|^|%or%")) {
11801180
// Don't simplify if operand is negative, shifting with negative
11811181
// operand is UB. Bitmasking with negative operand is implementation
11821182
// defined behaviour.
1183-
if (MathLib::isNegative(tok->str()) || MathLib::isNegative(tok->strAt(2)))
1184-
continue;
1183+
if (MathLib::isNegative(num1) || MathLib::isNegative(num2))
1184+
break;
11851185

11861186
const MathLib::value v1(num1);
11871187
const MathLib::value v2(num2);
11881188

11891189
if (!v1.isInt() || !v2.isInt())
1190-
continue;
1190+
break;
11911191

11921192
switch (op->str()[0]) {
11931193
case '<':
@@ -1230,7 +1230,6 @@ bool TemplateSimplifier::simplifyNumericCalculations(Token *tok)
12301230
}
12311231

12321232
tok->deleteNext(2);
1233-
tok = tok->previous();
12341233

12351234
ret = true;
12361235
}
@@ -1269,17 +1268,13 @@ bool TemplateSimplifier::simplifyCalculations(Token *_tokens)
12691268
}
12701269

12711270
if (tok->isNumber()) {
1272-
if (simplifyNumericCalculations(tok->previous())) {
1271+
if (simplifyNumericCalculations(tok)) {
12731272
ret = true;
1274-
tok = tok->previous();
1275-
while (Token::Match(tok->tokAt(-2), "%cop%|,|( %num% %cop% %num% %cop%|,|)")) {
1276-
Token *before = tok->tokAt(-2);
1277-
if (simplifyNumericCalculations(before))
1278-
tok = before;
1279-
else
1280-
break;
1273+
Token *prev = tok->tokAt(-2);
1274+
while (prev && simplifyNumericCalculations(prev)) {
1275+
tok = prev;
1276+
prev = prev->tokAt(-2);
12811277
}
1282-
tok = tok->next();
12831278
}
12841279

12851280
// Remove redundant conditions (0&&x) (1||x)

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3648,7 +3648,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
36483648
const Token * const end = tok;
36493649
for (tok = lt; tok != end; tok = tok->next()) {
36503650
if (tok->isNumber())
3651-
TemplateSimplifier::simplifyNumericCalculations(tok->previous());
3651+
TemplateSimplifier::simplifyNumericCalculations(tok);
36523652
}
36533653
lt = tok->next();
36543654
}

0 commit comments

Comments
 (0)