Skip to content

Commit 42bb479

Browse files
committed
Fix #724 (##: valid token pastes are rejected)
When the left operand of ## is a punctuator, check the pasted result after the right operand has been expanded, instead of checking the unexpanded parameter name.
1 parent ab23e4a commit 42bb479

2 files changed

Lines changed: 57 additions & 15 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,25 @@ static bool sameline(const simplecpp::Token *tok1, const simplecpp::Token *tok2)
160160
return tok1 && tok2 && tok1->location.sameline(tok2->location);
161161
}
162162

163+
/** is s a punctuator (C11 6.4.6, C++ [lex.operators]) */
164+
static bool isPunctuator(const std::string &s)
165+
{
166+
static const std::set<std::string> punctuators{
167+
"[", "]", "(", ")", "{", "}", ".", "->", "++", "--", "&", "*", "+", "-", "~", "!",
168+
"/", "%", "<<", ">>", "<", ">", "<=", ">=", "==", "!=", "^", "|", "&&", "||",
169+
"?", ":", ";", "...", "=", "*=", "/=", "%=", "+=", "-=", "<<=", ">>=", "&=", "^=", "|=",
170+
",", "#", "##", "<:", ":>", "<%", "%>", "%:", "%:%:", "::", ".*", "->*", "<=>"
171+
};
172+
return punctuators.find(s) != punctuators.end();
173+
}
174+
175+
/** is s a valid result when a punctuator is pasted with ## */
176+
static bool isValidPunctuatorPaste(const std::string &s)
177+
{
178+
// a pp-number can start with '.'
179+
return isPunctuator(s) || (s.size() >= 2 && s[0] == '.' && std::isdigit(static_cast<unsigned char>(s[1])));
180+
}
181+
163182
static bool isAlternativeBinaryOp(const simplecpp::Token *tok, const std::string &alt)
164183
{
165184
return (tok->name &&
@@ -2458,17 +2477,17 @@ namespace simplecpp {
24582477
if (!sameline(tok, tok->next) || !sameline(tok, tok->next->next))
24592478
throw invalidHashHash::unexpectedNewline(tok->location, name());
24602479

2461-
const bool canBeConcatenatedWithEqual = A->isOneOf("+-*/%&|^") || A->str() == "<<" || A->str() == ">>";
24622480
const bool canBeConcatenatedStringOrChar = isStringLiteral(A->str()) || isCharLiteral(A->str());
2463-
const bool unexpectedA = (!A->name && !A->number && !A->str().empty() && !canBeConcatenatedWithEqual && !canBeConcatenatedStringOrChar);
2481+
// A is a punctuator; the result is checked when B has been expanded
2482+
const bool punctuatorA = (!A->name && !A->number && !A->str().empty() && !canBeConcatenatedStringOrChar);
24642483

24652484
const Token * const B = tok->next->next;
2466-
if (!B->name && !B->number && B->op && !B->isOneOf("#="))
2467-
throw invalidHashHash::unexpectedToken(tok->location, name(), B);
2468-
2469-
if ((canBeConcatenatedWithEqual && B->op != '=') ||
2470-
(!canBeConcatenatedWithEqual && B->op == '='))
2471-
throw invalidHashHash::cannotCombine(tok->location, name(), A, B);
2485+
if (!punctuatorA) {
2486+
if (!B->name && !B->number && B->op && !B->isOneOf("#="))
2487+
throw invalidHashHash::unexpectedToken(tok->location, name(), B);
2488+
if (B->op == '=')
2489+
throw invalidHashHash::cannotCombine(tok->location, name(), A, B);
2490+
}
24722491

24732492
// Superficial check; more in-depth would in theory be possible _after_ expandArg
24742493
if (canBeConcatenatedStringOrChar && (B->number || !B->name))
@@ -2500,16 +2519,15 @@ namespace simplecpp {
25002519
else if (varargs && A->op == ',') {
25012520
strAB = ",";
25022521
}
2503-
else if (varargs && unexpectedA) {
2504-
throw invalidHashHash::unexpectedToken(tok->location, name(), A);
2505-
}
25062522
else {
2523+
if (punctuatorA && !isValidPunctuatorPaste(A->str() + tokensB.cfront()->str()))
2524+
throw invalidHashHash::cannotCombine(tok->location, name(), A, tokensB.cfront());
25072525
strAB = A->str() + tokensB.cfront()->str();
25082526
tokensB.deleteToken(tokensB.front());
25092527
}
25102528
} else {
2511-
if (unexpectedA)
2512-
throw invalidHashHash::unexpectedToken(tok->location, name(), A);
2529+
if (punctuatorA && !isValidPunctuatorPaste(A->str() + B->str()))
2530+
throw invalidHashHash::cannotCombine(tok->location, name(), A, B);
25132531
strAB = A->str() + B->str();
25142532
}
25152533

‎test.cpp‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ static void hashhash_invalid_1()
21342134
const char code[] = "#define f(a) (##x)\nf(1)\n";
21352135
simplecpp::OutputList outputList;
21362136
ASSERT_EQUALS("", preprocess(code, &outputList));
2137-
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'f', Invalid ## usage when expanding 'f': Unexpected token '('\n", toString(outputList));
2137+
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'f', Invalid ## usage when expanding 'f': Combining '(' and 'x' yields an invalid token.\n", toString(outputList));
21382138
}
21392139

21402140
static void hashhash_invalid_2()
@@ -2188,14 +2188,36 @@ static void hashhash_empty_va_args()
21882188
ASSERT_EQUALS("\n\n\n( 2 )", preprocess(code));
21892189
}
21902190

2191+
static void hashhash_punctuator() // #724
2192+
{
2193+
const char code[] =
2194+
"#define cat(a,b) a##b\n"
2195+
"#define MINUS_GT -##>\n"
2196+
"#define VA(...) <##__VA_ARGS__\n"
2197+
"cat(+,=) cat(<<,=) cat(-,>) cat(&,&) cat(|,|) cat(+,+) cat(<,=) cat(.,5) MINUS_GT VA(<) VA()\n";
2198+
simplecpp::OutputList outputList;
2199+
ASSERT_EQUALS("\n\n\n+= <<= -> && || ++ <= .5 -> << <", preprocess(code, &outputList));
2200+
ASSERT_EQUALS("", toString(outputList));
2201+
}
2202+
2203+
static void hashhash_punctuator_invalid()
2204+
{
2205+
const char code[] =
2206+
"#define cat(a,b) a##b\n"
2207+
"cat(+,x)\n";
2208+
simplecpp::OutputList outputList;
2209+
ASSERT_EQUALS("", preprocess(code, &outputList));
2210+
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'cat', Invalid ## usage when expanding 'cat': Combining '+' and 'x' yields an invalid token.\n", toString(outputList));
2211+
}
2212+
21912213
static void hashhash_va_args_unexpected()
21922214
{
21932215
const char code[] =
21942216
"#define C(...)!##__VA_ARGS__\n"
21952217
"C(1)\n";
21962218
simplecpp::OutputList outputList;
21972219
preprocess(code, simplecpp::DUI(), &outputList);
2198-
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'C', Invalid ## usage when expanding 'C': Unexpected token '!'\n", toString(outputList));
2220+
ASSERT_EQUALS("file0,1,syntax_error,failed to expand 'C', Invalid ## usage when expanding 'C': Combining '!' and '1' yields an invalid token.\n", toString(outputList));
21992221
}
22002222

22012223
static void hashhash_universal_character()
@@ -4942,6 +4964,8 @@ static void runTests(int argc, char **argv, Input input)
49424964
TEST_CASE(hashhash_invalid_missing_args);
49434965
TEST_CASE(hashhash_null_stmt);
49444966
TEST_CASE(hashhash_empty_va_args);
4967+
TEST_CASE(hashhash_punctuator);
4968+
TEST_CASE(hashhash_punctuator_invalid);
49454969
TEST_CASE(hashhash_va_args_unexpected);
49464970

49474971
// C standard, 5.1.1.2, paragraph 4:

0 commit comments

Comments
 (0)