From cb1cf59312f0694027c0c3ce79293ed19e8b9335 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Mon, 19 Nov 2007 12:26:50 -0800 Subject: [PATCH] BR 812417: Deadman counter for macro expansion Per BR 812417, certain macro expansions can hang NASM. Allow a deadman counter (currently set to 2^20) to fail out if it gets ridiculous. --- preproc.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/preproc.c b/preproc.c index 7af9fdc2..f9c4de36 100644 --- a/preproc.c +++ b/preproc.c @@ -2981,6 +2981,8 @@ static Token *expand_mmac_params(Token * tline) * Tokens from input to output a lot of the time, rather than * actually bothering to destroy and replicate.) */ +#define DEADMAN_LIMIT (1 << 20) + static Token *expand_smacro(Token * tline) { Token *t, *tt, *mstart, **tail, *thead; @@ -2992,6 +2994,7 @@ static Token *expand_smacro(Token * tline) Token *org_tline = tline; Context *ctx; char *mname; + int deadman = 0; /* * Trick: we should avoid changing the start token pointer since it can @@ -3008,11 +3011,16 @@ static Token *expand_smacro(Token * tline) org_tline->text = NULL; } - again: +again: tail = &thead; thead = NULL; while (tline) { /* main token loop */ + if (++deadman > DEADMAN_LIMIT) { + error(ERR_NONFATAL, "interminable macro recursion"); + break; + } + if ((mname = tline->text)) { /* if this token is a local macro, look in local context */ if (tline->type == TOK_ID || tline->type == TOK_PREPROC_ID) -- 2.11.4.GIT