From 22b5611cdfab7b460d03abadb2951d1eeedc19dd Mon Sep 17 00:00:00 2001
From: "Sven M. Hallberg" <pesco@khjk.org>
Date: Tue, 1 Dec 2015 17:08:02 +0100
Subject: [PATCH] add oom handling to iterative LR engine

---
 src/backends/lr.c | 13 +++++++++++++
 src/t_misc.c      |  3 ++-
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/backends/lr.c b/src/backends/lr.c
index 8f2a0ea4..f2ac4956 100644
--- a/src/backends/lr.c
+++ b/src/backends/lr.c
@@ -426,6 +426,16 @@ bool h_lr_parse_chunk(HSuspendedParser* s, HInputStream *stream)
   engine->input = *stream;
 
   bool run = true;
+
+  // out-of-memory handling
+  jmp_buf except;
+  h_arena_set_except(engine->arena, &except);
+  h_arena_set_except(engine->tarena, &except);
+  if(setjmp(except)) {
+    run = false;                            // done immediately
+    assert(engine->state != HLR_SUCCESS);   // h_parse_finish will return NULL
+  }
+
   while(run) {
     // check input against table to determine which action to take
     const HLRAction *action = h_lrengine_action(engine);
@@ -441,6 +451,9 @@ bool h_lr_parse_chunk(HSuspendedParser* s, HInputStream *stream)
       break;
   }
 
+  h_arena_set_except(engine->arena, NULL);
+  h_arena_set_except(engine->tarena, NULL);
+
   *stream = engine->input;
   return !run;  // done if engine no longer running
 }
diff --git a/src/t_misc.c b/src/t_misc.c
index 47afbe4e..8cd73dcb 100644
--- a/src/t_misc.c
+++ b/src/t_misc.c
@@ -57,8 +57,9 @@ static void test_oom(void) {
   g_check_parse_failed(p, PB_LALR, "x",1);
   g_check_parse_failed(p, PB_GLR, "x",1);
 
+  //g_check_parse_chunks_failed(p, PB_REGULAR, "",0, "x",1);
   g_check_parse_chunks_failed(p, PB_LLk, "",0, "x",1);
-  //g_check_parse_chunks_failed(p, PB_LALR, "",0, "x",1);
+  g_check_parse_chunks_failed(p, PB_LALR, "",0, "x",1);
   //g_check_parse_chunks_failed(p, PB_GLR, "",0, "x",1);
 
   i = setrlimit(RLIMIT_DATA, &bak);
-- 
GitLab