From 090a5b8bbf90003d42ed7c828201bc4a46d758c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Wed, 2 Dec 2026 15:48:49 +0100 Subject: [PATCH 1/5] test --- test/testtokenize.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index dcd1534a84f..cfe867b2ae3 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -491,6 +491,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(unknownMacroBeforeReturn); TEST_CASE(unknownMacroBracedArgument); + TEST_CASE(unknownMacroTooLargeInt); TEST_CASE(cppKeywordInCSource); @@ -8860,6 +8861,15 @@ class TestTokenizer : public TestFixture { "There is an unknown macro here somewhere. Configuration is required. If F is a macro then please configure it."); } + void unknownMacroTooLargeInt() { + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = STR(123456789012345678901234567890123456789);\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it."); + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = 123456789012345678901234567890123456789;\n"), + SYNTAX, + "syntax error: integer literal too large"); + } + void cppKeywordInCSource() { ASSERT_NO_THROW(tokenizeAndStringify("int throw() {}\n", dinit(TokenizeOptions, $.cpp = false))); From 3395e6093971dcdfc4ef83332045b8a710aabac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Wed, 2 Dec 2026 15:48:54 +0100 Subject: [PATCH 2/5] fix --- lib/tokenize.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4dbd696f36a..de94b100f24 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -8976,6 +8976,15 @@ void Tokenizer::findGarbageCode() const return false; }; + const auto isTooLargeInt = [](const Token *tok) { + try { + (void)MathLib::toBigUNumber(tok); + return false; + } catch (const InternalError & /*e*/) { + return true; + } + }; + for (const Token *tok = tokens(); tok; tok = tok->next()) { // initialization: = { if (Token::simpleMatch(tok, "= {") && Token::simpleMatch(tok->linkAt(1), "} (")) @@ -9038,6 +9047,19 @@ void Tokenizer::findGarbageCode() const unknownMacroError(par->previous()); } + // Invalid integer literal as argument of a possible macro + if (tok->isNumber() && MathLib::isInt(tok->str()) && isTooLargeInt(tok)) { + const Token *prev = tok->previous(); + while (prev && !Token::Match(prev, "(|;|{|}")) { + if (Token::Match(prev, ")|]")) + prev = prev->link(); + prev = prev->previous(); + } + if (prev && prev->str() == "(" && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword()) + unknownMacroError(prev->previous()); + syntaxError(tok, "integer literal too large"); + } + if (tok->isControlFlowKeyword()) { if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... } if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|)")) { From 1d0d3f443677df42d10f10b844064206e995821c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Thu, 3 Dec 2026 13:39:03 +0100 Subject: [PATCH 3/5] update cli test --- test/cli/other_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/other_test.py b/test/cli/other_test.py index 17893df9106..ab79ecd550d 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -2947,7 +2947,7 @@ def test_internal_error_loc_int(tmp_path): assert exitcode == 0, stdout assert stdout.splitlines() == [] assert stderr.splitlines() == [ - '{}:3:13: error: Internal Error. MathLib::toBigUNumber: out_of_range: 0x10000000000000000 [internalError]'.format(test_file) + '{}:3:13: error: syntax error: integer literal too large [syntaxError]'.format(test_file) ] From f6b719bfceac5dc80172181409ef28630b8da442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Thu, 3 Dec 2026 15:39:04 +0100 Subject: [PATCH 4/5] add/rename tests --- test/cli/other_test.py | 2 +- test/testmathlib.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/test/cli/other_test.py b/test/cli/other_test.py index ab79ecd550d..78ba223862a 100644 --- a/test/cli/other_test.py +++ b/test/cli/other_test.py @@ -2928,7 +2928,7 @@ def test_outputfile(tmp_path): # #14051 assert out_text.splitlines() == out_exp -def test_internal_error_loc_int(tmp_path): +def test_syntax_error_loc_int(tmp_path): test_file = tmp_path / 'test.c' with open(test_file, 'wt') as f: f.write( diff --git a/test/testmathlib.cpp b/test/testmathlib.cpp index 800334926e5..df92d7f5e74 100644 --- a/test/testmathlib.cpp +++ b/test/testmathlib.cpp @@ -589,6 +589,16 @@ class TestMathLib : public TestFixture { tok->str("invalid"); ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber(tok), INTERNAL, "Internal Error. MathLib::toBigUNumber: invalid_argument: invalid"); ASSERT_THROW_INTERNAL_EQUALS(MathLib::toBigUNumber("invalid", tok), INTERNAL, "Internal Error. MathLib::toBigUNumber: invalid_argument: invalid"); + + // the error location is the token (#13488) + tok->str("0x10000000000000000"); + try { + (void)MathLib::toBigUNumber(tok); + assertThrowFail(__FILE__, __LINE__); + } catch (const InternalError &e) { + ASSERT_EQUALS("Internal Error. MathLib::toBigUNumber: out_of_range: 0x10000000000000000", e.errorMessage); + ASSERT(e.token == tok); + } TokenList::deleteTokens(tok); } From 2a7b431d01a072359931305f68c64169f95a565f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludvig=20Gunne=20Lindstr=C3=B6m?= Date: Fri, 4 Dec 2026 11:23:18 +0100 Subject: [PATCH 5/5] move check, handle ; and { in macro arg --- lib/tokenize.cpp | 28 +++++++++++++++------------- test/testtokenize.cpp | 8 ++++++++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index de94b100f24..b6696d836eb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9047,19 +9047,6 @@ void Tokenizer::findGarbageCode() const unknownMacroError(par->previous()); } - // Invalid integer literal as argument of a possible macro - if (tok->isNumber() && MathLib::isInt(tok->str()) && isTooLargeInt(tok)) { - const Token *prev = tok->previous(); - while (prev && !Token::Match(prev, "(|;|{|}")) { - if (Token::Match(prev, ")|]")) - prev = prev->link(); - prev = prev->previous(); - } - if (prev && prev->str() == "(" && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword()) - unknownMacroError(prev->previous()); - syntaxError(tok, "integer literal too large"); - } - if (tok->isControlFlowKeyword()) { if (Token::Match(tok, "if|while|for|switch")) { // if|while|for|switch (EXPR) { ... } if (tok->previous() && !Token::Match(tok->previous(), "%name%|:|;|{|}|)")) { @@ -9509,6 +9496,21 @@ void Tokenizer::findGarbageCode() const if (Token::Match(tok, "[;{}] [ %name% %name% ] ;")) syntaxError(tok->next()); } + + // Integer literal too large: argument of a possible macro, or invalid code. + for (const Token *tok = tokens(); tok; tok = tok->next()) { + if (!tok->isNumber() || !MathLib::isInt(tok->str()) || !isTooLargeInt(tok)) + continue; + const Token *prev = tok->previous(); + while (prev && prev->str() != "(") { + if (Token::Match(prev, ")|]|}")) + prev = prev->link(); + prev = prev->previous(); + } + if (prev && Token::Match(prev->previous(), "%name% (") && !prev->previous()->isKeyword()) + unknownMacroError(prev->previous()); + syntaxError(tok, "integer literal too large"); + } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index cfe867b2ae3..322650f7674 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -8868,6 +8868,14 @@ class TestTokenizer : public TestFixture { ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = 123456789012345678901234567890123456789;\n"), SYNTAX, "syntax error: integer literal too large"); + // more specific errors take priority + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("struct S s = MAC(.a = { 123456789012345678901234567890123456789 });\n", dinit(TokenizeOptions, $.cpp = false)), + SYNTAX, + "syntax error: ( . a"); + // macro argument with braces + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("int x = STR({ 123456789012345678901234567890123456789 });\n"), + UNKNOWN_MACRO, + "There is an unknown macro here somewhere. Configuration is required. If STR is a macro then please configure it."); } void cppKeywordInCSource() {