Skip to content

Commit adf2521

Browse files
dmcppcheckclaude
andcommitted
HideSetExpander: an unterminated macro call is not expanded
The tokens after the macro name are scanned again, like in the old expansion. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 195279c commit adf2521

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,7 +3127,12 @@ namespace simplecpp {
31273127
continue;
31283128
}
31293129

3130-
const MacroCall call = readMacroCall(macro, tok, input);
3130+
MacroCall call;
3131+
if (!readMacroCall(call, macro, tok, input)) {
3132+
// unterminated macro call: the name is not expanded
3133+
output.push_back(tok);
3134+
continue;
3135+
}
31313136
TokenList result(files);
31323137
substitute(result, macro, call);
31333138
input.pushFront(result);
@@ -3136,21 +3141,28 @@ namespace simplecpp {
31363141

31373142
/**
31383143
* Read a macro call: the name, and "( ... )" for a function-like macro.
3144+
* @param call the macro call
31393145
* @param macro the macro
3140-
* @param nameTok the macro name, it is deleted
3146+
* @param nameTok the macro name, it is deleted unless the call is unterminated
31413147
* @param input the input after the name
3148+
* @return false if the call is not terminated; the tokens after the name are put back in input
31423149
* @throws wrongNumberOfParameters if a function-like macro has the wrong number of arguments
31433150
*/
3144-
MacroCall readMacroCall(const Macro &macro, Token *nameTok, Input &input) {
3145-
MacroCall call;
3151+
bool readMacroCall(MacroCall &call, const Macro &macro, Token *nameTok, Input &input) {
31463152
if (!macro.functionLike()) {
31473153
call.hideSet = nameTok->hideSet();
31483154
call.whitespaceAfter = nameTok->whitespaceahead;
31493155
delete nameTok;
31503156
} else {
31513157
TokenList tokens(files);
3152-
tokens.push_back(nameTok);
3153-
readCall(tokens, input);
3158+
tokens.push_back(new Token(*nameTok));
3159+
if (!readCall(tokens, input)) {
3160+
// give the tokens after the name back
3161+
tokens.deleteToken(tokens.front());
3162+
input.pushFront(tokens);
3163+
return false;
3164+
}
3165+
delete nameTok;
31543166
const std::vector<const Token *> delimiters = macro.getMacroParameters(tokens.cfront(), false);
31553167
// the name and the ) have the hide sets of the macro calls that this call
31563168
// was produced by, the arguments are not affected by them
@@ -3162,19 +3174,20 @@ namespace simplecpp {
31623174
call.whitespaceAfter = delimiters.back()->whitespaceahead;
31633175
}
31643176
call.hideSet.insert(macro.name());
3165-
return call;
3177+
return true;
31663178
}
31673179

3168-
/** Read "( ... )" after the macro name in call */
3169-
static void readCall(TokenList &call, Input &input) {
3180+
/** Read "( ... )" after the macro name in call; false if there is no ) */
3181+
static bool readCall(TokenList &call, Input &input) {
31703182
int par = 0;
31713183
while (Token * const tok = input.read()) {
31723184
call.push_back(tok);
31733185
if (tok->op == '(')
31743186
++par;
31753187
else if (tok->op == ')' && --par == 0)
3176-
return;
3188+
return true;
31773189
}
3190+
return false;
31783191
}
31793192

31803193
/** Each argument of the call, as written in the call */

‎test.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,6 @@ static void define_define_13() // issue #49 - empty macro
13701370

13711371
static void define_define_14() // issue #58 - endless recursion
13721372
{
1373-
HIDESET_EXPANSION_TODO(); // unterminated macro call
13741373
const char code[] = "#define z f(w\n"
13751374
"#define f()\n"
13761375
"#define w f(z\n"

0 commit comments

Comments
 (0)