Skip to content
Open
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/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void Token::update_property_info()
if ((MathLib::isInt(mStr) || MathLib::isFloat(mStr)) && mStr.find('_') == std::string::npos)
tokType(eNumber);
else
tokType(eName); // assume it is a user defined literal
tokType(eLiteral); // assume it is a user defined literal
} else if (mStr == "=" || mStr == "<<=" || mStr == ">>=" ||
(mStr.size() == 2U && mStr[1] == '=' && std::strchr("+-*/%&^|", mStr[0])))
tokType(eAssignmentOp);
Expand Down
3 changes: 2 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8869,6 +8869,7 @@ void Tokenizer::findGarbageCode() const
!Token::simpleMatch(tok->previous(), ".") &&
!Token::simpleMatch(tok->next(), ".") &&
!Token::Match(tok->previous(), "{|, . %name% =|.|[|{") &&
!(tok->previous() && tok->previous()->isLiteral()) &&
!Token::Match(tok->previous(), ", . %name%")) {
if (!Token::Match(tok->previous(), "%name%|)|]|>|}"))
syntaxError(tok, tok->strAt(-1) + " " + tok->str() + " " + tok->strAt(1));
Expand Down Expand Up @@ -9847,7 +9848,7 @@ void Tokenizer::simplifyAsm()
Token *endasm = tok->next();
const Token *firstSemiColon = nullptr;
int comment = 0;
while (Token::Match(endasm, "%num%|%name%|,|:|;") || (endasm && endasm->linenr() == comment)) {
while (Token::Match(endasm, "%num%|%name%|,|:|;") || (endasm && (endasm->isLiteral() || endasm->linenr() == comment))) {
if (Token::Match(endasm, "_asm|__asm|__endasm"))
break;
if (endasm->str() == ";") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_ 1p;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is garbage code and ideally I think we would throw a syntaxError. That might prevent further crashes if the fuzzer tries more such garbage..

I don't know .. a + name literal statement might be garbage always?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the plus is just from the diff, and maybe the underscore could be an unknown macro? Either way i believe it should be a syntax error, afaik the suffix needs to begin with underscore, but maybe that should be its own ticket?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well the plus is just from the diff

ok understood.

Either way i believe it should be a syntax error, afaik the suffix needs to begin with underscore, but maybe that should be its own ticket?

as I understand it, user defined literals must start with underscore however library defined literals i.e. sv does not have to start with underscore.

Copy link
Collaborator

@firewave firewave Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon it is just invalid. It also happens with variables names that start with a number - see https://trac.cppcheck.net/ticket/14152.

11 changes: 11 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(dumpFriend); // Check if isFriend added to dump file

TEST_CASE(smartPointerLookupCtor); // #13719);

TEST_CASE(userDefinedLiteral);
}

void array() {
Expand Down Expand Up @@ -11317,6 +11319,15 @@ class TestSymbolDatabase : public TestFixture {

ASSERT(db);
}

void userDefinedLiteral() {
GET_SYMBOL_DB("_ 1p;");
const Token *x = Token::findsimplematch(tokenizer.tokens(), "1p");
ASSERT(x);
ASSERT(!x->varId());
ASSERT(!x->variable());
}

};

REGISTER_TEST(TestSymbolDatabase)
2 changes: 1 addition & 1 deletion test/testtoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ class TestToken : public TestFixture {
assert_tok("0.0", Token::Type::eNumber);
assert_tok("0x0.3p10", Token::Type::eNumber);
assert_tok("0z", Token::Type::eNumber); // TODO: not a valid number
assert_tok("0_km", Token::Type::eName); // user literal
assert_tok("0_km", Token::Type::eLiteral); // user literal
assert_tok("=", Token::Type::eAssignmentOp);
assert_tok("<<=", Token::Type::eAssignmentOp);
assert_tok(">>=", Token::Type::eAssignmentOp);
Expand Down