1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

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
97d72d15a0.
This commit is contained in:
Miciah Dashiel Butler Masters 2009-02-25 01:37:13 +00:00
parent 3e8f774659
commit 50ff8fd835

View File

@ -4,6 +4,8 @@
#include "config.h" #include "config.h"
#endif #endif
#include <stdlib.h>
#include "elinks.h" #include "elinks.h"
#include "config/home.h" #include "config/home.h"
@ -205,7 +207,7 @@ utf8_to_jsstring(JSContext *ctx, const unsigned char *str, int length)
utf16_alloc = in_bytes; utf16_alloc = in_bytes;
/* Don't use fmem_alloc here because long strings could /* Don't use fmem_alloc here because long strings could
* exhaust the stack. */ * exhaust the stack. */
utf16 = mem_alloc(utf16_alloc * sizeof(jschar)); utf16 = malloc(utf16_alloc * sizeof(jschar));
if (utf16 == NULL) { if (utf16 == NULL) {
JS_ReportOutOfMemory(ctx); JS_ReportOutOfMemory(ctx);
return NULL; return NULL;
@ -223,18 +225,19 @@ utf8_to_jsstring(JSContext *ctx, const unsigned char *str, int length)
if (needs_utf16_surrogates(unicode)) { if (needs_utf16_surrogates(unicode)) {
assert(utf16_alloc - utf16_used >= 2); 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_high_surrogate(unicode);
utf16[utf16_used++] = get_utf16_low_surrogate(unicode); utf16[utf16_used++] = get_utf16_low_surrogate(unicode);
} else { } else {
assert(utf16_alloc - utf16_used >= 1); 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; utf16[utf16_used++] = unicode;
} }
} }
jsstr = JS_NewUCString(ctx, utf16, utf16_used); jsstr = JS_NewUCString(ctx, utf16, utf16_used);
mem_free(utf16); if (jsstr == NULL) free(utf16);
return jsstr; return jsstr;
} }