This adds a configuration option to set the recursion limit. I've set it to
a (conservative) 1000 by default. In addition, there is an option to turn
it off for a very minor space savings and performance increase.

Signed-off-by: Sean Anderson <sean...@gmail.com>
---
Do we need this? Perhaps it should default to 0.

 cmd/Kconfig      |  8 ++++++++
 common/cli_lil.c | 10 ++--------
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 28a387b380..7c8962cfc2 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -53,6 +53,14 @@ config LIL_POOLS
          use more memory but will cause considerably less memory fragmentation
          and improve the script execution performance.
 
+config LIL_RECLIMIT
+       int "LIL function recursion limit"
+       default 1000
+       help
+         Enable limiting recursive calls to lil_parse - this can be used to
+         avoid call stack overflows and is also useful when running through an
+         automated fuzzer like AFL. Set to 0 to disable the recursion limit.
+
 endif
 
 endif
diff --git a/common/cli_lil.c b/common/cli_lil.c
index 750a085f63..6c05531441 100644
--- a/common/cli_lil.c
+++ b/common/cli_lil.c
@@ -17,10 +17,6 @@
 #include <stdio.h>
 #include <string.h>
 
-/* Enable limiting recursive calls to lil_parse - this can be used to avoid 
call stack
- * overflows and is also useful when running through an automated fuzzer like 
AFL */
-/*#define LIL_ENABLE_RECLIMIT 10000*/
-
 #define HASHMAP_CELLS 256
 #define HASHMAP_CELLMASK 0xFF
 
@@ -1198,12 +1194,10 @@ struct lil_value *lil_parse(struct lil *lil, const char 
*code, size_t codelen,
 
        lil_skip_spaces(lil);
        lil->parse_depth++;
-#ifdef LIL_ENABLE_RECLIMIT
-       if (lil->parse_depth > LIL_ENABLE_RECLIMIT) {
-               lil_set_error(lil, LIL_ERR_DEPTH, "Too many recursive calls");
+       if (CONFIG_LIL_RECLIMIT && lil->parse_depth > CONFIG_LIL_RECLIMIT) {
+               lil_set_error(lil, LIL_ERR_DEPTH, "recursion limit reached");
                goto cleanup;
        }
-#endif
 
        if (lil->parse_depth == 1)
                lil->err = LIL_ERR_NONE;
-- 
2.32.0

Reply via email to