From 68acb60984c7d61368fbd87772cd8b8e73c0f298 Mon Sep 17 00:00:00 2001 From: Penner Date: Tue, 6 May 2025 18:48:14 +0800 Subject: [PATCH] fixed realloc(0) may alloc min unit memory on some allocator --- libregexp.c | 2 +- libregexp.h | 1 + quickjs.c | 7 +++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libregexp.c b/libregexp.c index 8c47389..e94e652 100644 --- a/libregexp.c +++ b/libregexp.c @@ -2448,7 +2448,7 @@ int lre_exec(uint8_t **capture, stack_buf = alloca(alloca_size); ret = lre_exec_backtrack(s, capture, stack_buf, 0, bc_buf + RE_HEADER_LEN, cbuf + (cindex << cbuf_type), FALSE); - lre_realloc(s->opaque, s->state_stack, 0); + lre_free(s->opaque, s->state_stack); return ret; } diff --git a/libregexp.h b/libregexp.h index 7475bbe..e0b9bd8 100644 --- a/libregexp.h +++ b/libregexp.h @@ -56,5 +56,6 @@ int lre_check_stack_overflow(void *opaque, size_t alloca_size); /* must be provided by the user, return non zero if time out */ int lre_check_timeout(void *opaque); void *lre_realloc(void *opaque, void *ptr, size_t size); +void lre_free(void *opaque, void *ptr); #endif /* LIBREGEXP_H */ diff --git a/quickjs.c b/quickjs.c index 6119975..2401c71 100644 --- a/quickjs.c +++ b/quickjs.c @@ -44594,6 +44594,13 @@ void *lre_realloc(void *opaque, void *ptr, size_t size) return js_realloc_rt(ctx->rt, ptr, size); } +void lre_free(void *opaque, void *ptr) +{ + JSContext *ctx = opaque; + /* No JS exception is raised here */ + js_free_rt(ctx->rt, ptr); +} + static JSValue js_regexp_escape(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {