out of memory handling and leak fixes in regex backend
This commit is contained in:
parent
9ef70f2f2d
commit
e8b1962005
1 changed files with 33 additions and 12 deletions
|
|
@ -56,6 +56,15 @@ 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);
|
||||||
|
|
@ -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,
|
||||||
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) {
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue