From f11b2a8f97549e7559a1e14dbce52703dfdadaa4 Mon Sep 17 00:00:00 2001 From: Miciah Dashiel Butler Masters Date: Wed, 25 Feb 2009 01:37:13 +0000 Subject: [PATCH] utf8_to_jsstring: Don't free mem handed over to JS In utf8_to_jsstring, do not free the string that is passed to JS_NewUCString if the latter is successful; if it is, SpiderMonkey handles the memory from then on. Use libc routines instead of ELinks's routines to allocate and free the string so that ELinks's memory debugging code does not try to keep track of it after it has been handed to SpiderMonkey. This commit fixes a bug introdued in 97d72d15a0dbde3e8c8c51eb139f270874e37fda. --- src/scripting/smjs/core.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/scripting/smjs/core.c b/src/scripting/smjs/core.c index 4582da3aa..0b2569fbb 100644 --- a/src/scripting/smjs/core.c +++ b/src/scripting/smjs/core.c @@ -4,6 +4,8 @@ #include "config.h" #endif +#include + #include "elinks.h" #include "config/home.h" @@ -205,7 +207,7 @@ utf8_to_jsstring(JSContext *ctx, const unsigned char *str, int length) utf16_alloc = in_bytes; /* Don't use fmem_alloc here because long strings could * exhaust the stack. */ - utf16 = mem_alloc(utf16_alloc * sizeof(jschar)); + utf16 = malloc(utf16_alloc * sizeof(jschar)); if (utf16 == NULL) { JS_ReportOutOfMemory(ctx); return NULL; @@ -223,18 +225,19 @@ utf8_to_jsstring(JSContext *ctx, const unsigned char *str, int length) if (needs_utf16_surrogates(unicode)) { assert(utf16_alloc - utf16_used >= 2); - if_assert_failed { mem_free(utf16); return NULL; } + if_assert_failed { free(utf16); return NULL; } utf16[utf16_used++] = get_utf16_high_surrogate(unicode); utf16[utf16_used++] = get_utf16_low_surrogate(unicode); } else { assert(utf16_alloc - utf16_used >= 1); - if_assert_failed { mem_free(utf16); return NULL; } + if_assert_failed { free(utf16); return NULL; } utf16[utf16_used++] = unicode; } } jsstr = JS_NewUCString(ctx, utf16, utf16_used); - mem_free(utf16); + if (jsstr == NULL) free(utf16); + return jsstr; }