[PATCH] parser: set recursion deapth limit for parens

Shivang Upadhyay <[email protected]> Mon, 1 Jun 2026 19:32:58 +0530
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
Setting 5000 as limit for recursive parenthesis '()'
or braces '{}'.

Signed-off-by: Shivang Upadhyay <[email protected]>
---
 src/parser.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/parser.c b/src/parser.c
index 412e876..5198dc4 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -110,7 +110,9 @@ struct nodelist *backquotelist;
 union node *redirnode;
 struct heredoc *heredoc;
 int quoteflag;			/* set if (part of) last token was quoted */
+static int paren_depth;		/* track parenthesis nesting depth */
 
+#define MAX_PAREN_DEPTH 5000
 
 STATIC union node *list(int);
 STATIC union node *andor(void);
@@ -164,6 +166,7 @@ parsecmd(int interact)
 	if (doprompt)
 		setprompt(doprompt);
 	needprompt = 0;
+	paren_depth = 0;
 	return list(1);
 }
 
@@ -471,15 +474,21 @@ next_case:
 		*cpp = NULL;
 		goto redir;
 	case TLP:
+		if (++paren_depth > MAX_PAREN_DEPTH)
+			synerror("too many nested parentheses");
 		n1 = (union node *)stalloc(sizeof (struct nredir));
 		n1->type = NSUBSHELL;
 		n1->nredir.linno = savelinno;
 		n1->nredir.n = list(0);
+		paren_depth--;
 		n1->nredir.redirect = NULL;
 		t = TRP;
 		break;
 	case TBEGIN:
+		if (++paren_depth > MAX_PAREN_DEPTH)
+			synerror("too many nested braces");
 		n1 = list(0);
+		paren_depth--;
 		t = TEND;
 		break;
 	case TWORD:
-- 
2.53.0