Skip to content

Commit

Permalink
Fix #12453 fuzzing timeout in getUnsafeFunction() (#6020)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Feb 26, 2024
1 parent 91f15aa commit eb2dbd3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 10 deletions.
6 changes: 6 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,12 @@ std::pair<const Token *, const Token *> Token::findExpressionStartEndTokens() co
end = goToRightParenthesis(start, end);
if (Token::simpleMatch(end, "{"))
end = end->link();

if (precedes(top, start))
throw InternalError(start, "Cannot find start of expression");
if (succeeds(top, end))
throw InternalError(end, "Cannot find end of expression");

return std::pair<const Token *, const Token *>(start,end);
}

Expand Down
13 changes: 10 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10025,12 +10025,19 @@ void Tokenizer::simplifyOperatorName()
}
}

const bool returnsRef = Token::simpleMatch(par, "( & (") && tok->next()->isName();
if (par && !op.empty()) {
tok->str("operator" + op);
Token::eraseTokens(tok, par);
if (returnsRef) {
par->next()->insertToken("operator" + op)->isOperatorKeyword(true);
tok->deleteThis();
}
else {
tok->str("operator" + op);
Token::eraseTokens(tok, par);
}
}

if (!op.empty())
if (!op.empty() && !returnsRef)
tok->isOperatorKeyword(true);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ static void compileUnaryOp(Token *&tok, AST_state& state, void (*f)(Token *&tok,
state.depth--;
}

if (!state.op.empty()) {
if (!state.op.empty() && (!precedes(state.op.top(), unaryop) || unaryop->isIncDecOp() || Token::Match(unaryop, "[({[]"))) { // nullary functions, empty lists/arrays
unaryop->astOperand1(state.op.top());
state.op.pop();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d f(*){*:b&&}
10 changes: 5 additions & 5 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2906,11 +2906,11 @@ class TestSimplifyTypedef : public TestFixture {
const char code[] = "class c {\n"
" typedef char foo[4];\n"
" foo _a;\n"
" constexpr operator foo &() const noexcept { return _a; }\n"
" constexpr operator foo &() noexcept { return _a; }\n"
"};";

const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char actual[] = "class c { char _a [ 4 ] ; constexpr char ( & operatorchar ( ) noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; char ( & operatorchar ( ) noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
TODO_ASSERT_EQUALS(exp, actual, tok(code, false));
}

Expand All @@ -2921,8 +2921,8 @@ class TestSimplifyTypedef : public TestFixture {
" constexpr operator const foo &() const noexcept { return _a; }\n"
"};";

const char actual[] = "class c { char _a [ 4 ] ; constexpr operatorconstchar ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const operator const char ( & ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char actual[] = "class c { char _a [ 4 ] ; constexpr const char ( & operatorconstchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
const char exp[] = "class c { char _a [ 4 ] ; const char ( & operatorconstchar ( ) const noexcept ( true ) ) [ 4 ] { return _a ; } } ;";
TODO_ASSERT_EQUALS(exp, actual, tok(code, false));
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5101,7 +5101,7 @@ class TestTokenizer : public TestFixture {
" operator A& () { return x_; }\n"
" A x_;\n"
"};";
ASSERT_EQUALS("template < typename T >\nstruct B {\n\noperatorT ( & ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code));
ASSERT_EQUALS("template < typename T >\nstruct B {\n\nT ( & operatorT ( ) ) [ 3 ] { return x_ ; }\nT x_ [ 3 ] ;\n} ;", tokenizeAndStringify(code));
ASSERT_EQUALS("", errout.str());
}

Expand Down Expand Up @@ -6586,6 +6586,7 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS("abc;(", testAst("a(b;c)"));
ASSERT_EQUALS("x{( forbc;;(", testAst("x({ for(a;b;c){} });"));
ASSERT_EQUALS("PT.(", testAst("P->~T();")); // <- The "T" token::function() will be a destructor
ASSERT_EQUALS("double&(4[", testAst("void f(double(&)[4]) {}"));
}

void asttemplate() { // uninstantiated templates will have <,>,etc..
Expand Down

0 comments on commit eb2dbd3

Please sign in to comment.