[hackers] [scc] Limit the recursivity in declarations and expressions || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 21 Aug 2015 13:10:37 +0200 (CEST)

X-DEBUG-UPD: 1ea33a55b6d3df7fd73194f0f2a599960c67676e
commit 1ea33a55b6d3df7fd73194f0f2a599960c67676e
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Tue Aug 18 14:16:10 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Tue Aug 18 14:16:10 2015 +0200

    Limit the recursivity in declarations and expressions
    
    Recursivity can generate a segfault or a heap-stack collition,
    so it is a good idea limiting it with the values proposed by
    c89 standard (if your code have more of 32 parentheses nested
    try to use a lisp interpreter).

diff --git a/cc1/decl.c b/cc1/decl.c
index 581a146..26d4aee 100644
--- a/cc1/decl.c
+++ b/cc1/decl.c
_AT_@ -207,9 +207,14 @@ static void
 directdcl(struct declarators *dp, unsigned ns)
 {
         Symbol *sym;
+ static int nested;
 
         if (accept('(')) {
+ if (nested == NR_SUBTYPE)
+ error("too declarators nested by parentheses");
+ ++nested;
                 declarator(dp, ns);
+ --nested;
                 expect(')');
         } else {
                 if (yytoken == IDEN || yytoken == TYPEIDEN) {
_AT_@ -409,6 +414,7 @@ structdcl(void)
 {
         Symbol *sym;
         Type *tp;
+ static int nested;
 
         sym = newtag();
         tp = sym->type;
_AT_@ -419,8 +425,14 @@ structdcl(void)
                 error("redefinition of struct/union '%s'", sym->name);
         tp->defined = 1;
 
+ if (nested == NR_STRUCT_LEVEL)
+ error("too levels of nested structure or union definitions");
+
+ ++nested;
         while (!accept('}'))
                 fieldlist(tp);
+ --nested;
+
         return tp;
 }
 
diff --git a/cc1/expr.c b/cc1/expr.c
index 51031d4..a0bbeb9 100644
--- a/cc1/expr.c
+++ b/cc1/expr.c
_AT_@ -4,6 +4,7 @@
 #include <string.h>
 
 #include "../inc/cc.h"
+#include "../inc/sizes.h"
 #include "cc1.h"
 
 
_AT_@ -870,6 +871,7 @@ cast(void)
 {
         Node *lp, *rp;
         Type *tp;
+ static int nested;
 
         if (!accept('('))
                 return unary();
_AT_@ -893,6 +895,8 @@ cast(void)
                 }
                 break;
         default:
+ if (nested == NR_SUBEXPR)
+ error("too expressions nested by parentheses");
                 rp = expr();
                 expect(')');
                 rp = postfix(rp);
diff --git a/cc1/stmt.c b/cc1/stmt.c
index 75e57a4..f876452 100644
--- a/cc1/stmt.c
+++ b/cc1/stmt.c
_AT_@ -5,6 +5,7 @@
 #include <stdio.h>
 
 #include "../inc/cc.h"
+#include "../inc/sizes.h"
 #include "cc1.h"
 
 Symbol *curfun;
_AT_@ -297,14 +298,21 @@ blockit(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 void
 compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
 {
+ static int nested;
+
         pushctx();
         expect('{');
 
+ if (nested == NR_BLOCK)
+ error("too nesting levels of compound statements");
+
+ ++nested;
         for (;;) {
                 if (yytoken == '}')
                         break;
                 blockit(lbreak, lcont, lswitch);
         }
+ --nested;
 
         popctx();
         /*
Received on Fri Aug 21 2015 - 13:10:37 CEST

This archive was generated by hypermail 2.3.0 : Fri Aug 21 2015 - 13:12:16 CEST