Skip to content
Snippets Groups Projects
Commit e8b19620 authored by Sven M. Hallberg's avatar Sven M. Hallberg
Browse files

out of memory handling and leak fixes in regex backend

parent 9ef70f2f
No related branches found
No related tags found
No related merge requests found
...@@ -56,7 +56,16 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_ ...@@ -56,7 +56,16 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_
*heads_p = h_sarray_new(mm__, prog->length); *heads_p = h_sarray_new(mm__, prog->length);
HRVMTrace *ret_trace = NULL; HRVMTrace *ret_trace = NULL;
HParseResult *ret = NULL;
// out of memory handling
if(!arena || !heads_n || !heads_p)
goto end;
jmp_buf except;
h_arena_set_except(arena, &except);
if(setjmp(except))
goto end;
uint8_t *insn_seen = a_new(uint8_t, prog->length); // 0 -> not seen, 1->processed, 2->queued uint8_t *insn_seen = a_new(uint8_t, prog->length); // 0 -> not seen, 1->processed, 2->queued
HRVMThread *ip_queue = a_new(HRVMThread, prog->length); HRVMThread *ip_queue = a_new(HRVMThread, prog->length);
size_t ipq_top; size_t ipq_top;
...@@ -164,18 +173,19 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_ ...@@ -164,18 +173,19 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_
} }
// No accept was reached. // No accept was reached.
match_fail: match_fail:
if (ret_trace == NULL) {
// No match found; definite failure. h_arena_set_except(arena, NULL); // there should be no more allocs from this
h_delete_arena(arena); if (ret_trace) {
return NULL; // Invert the direction of the trace linked list.
ret_trace = invert_trace(ret_trace);
ret = run_trace(mm__, prog, ret_trace, input, len);
// NB: ret is in its own arena
} }
// Invert the direction of the trace linked list. end:
if (arena) h_delete_arena(arena);
ret_trace = invert_trace(ret_trace); if (heads_n) h_sarray_free(heads_n);
HParseResult *ret = run_trace(mm__, prog, ret_trace, input, len); if (heads_p) h_sarray_free(heads_p);
// ret is in its own arena
h_delete_arena(arena);
return ret; return ret;
} }
#undef PUSH_SVM #undef PUSH_SVM
...@@ -203,6 +213,14 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace, ...@@ -203,6 +213,14 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace,
ctx.stack_capacity = 16; ctx.stack_capacity = 16;
ctx.stack = h_new(HParsedToken*, ctx.stack_capacity); ctx.stack = h_new(HParsedToken*, ctx.stack_capacity);
// out of memory handling
if(!arena || !ctx.stack)
goto fail;
jmp_buf except;
h_arena_set_except(arena, &except);
if(setjmp(except))
goto fail;
HParsedToken *tmp_res; HParsedToken *tmp_res;
HRVMTrace *cur; HRVMTrace *cur;
for (cur = trace; cur; cur = cur->next) { for (cur = trace; cur; cur = cur->next) {
...@@ -242,7 +260,7 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace, ...@@ -242,7 +260,7 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace,
break; break;
case SVM_ACCEPT: case SVM_ACCEPT:
assert(ctx.stack_count <= 1); assert(ctx.stack_count <= 1);
HParseResult *res = a_new(HParseResult, 1); HParseResult *res = a_new(HParseResult, 1);
if (ctx.stack_count == 1) { if (ctx.stack_count == 1) {
res->ast = ctx.stack[0]; res->ast = ctx.stack[0];
} else { } else {
...@@ -250,11 +268,14 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace, ...@@ -250,11 +268,14 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace,
} }
res->bit_length = cur->input_pos * 8; res->bit_length = cur->input_pos * 8;
res->arena = arena; res->arena = arena;
h_arena_set_except(arena, NULL);
h_free(ctx.stack);
return res; return res;
} }
} }
fail: fail:
h_delete_arena(arena); if (arena) h_delete_arena(arena);
if (ctx.stack) h_free(ctx.stack);
return NULL; return NULL;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment