fix potential memory leak (#318)

- fix memory leak in `js_std_file_printf`
- fix `errno` clobber in `js_os_stat`
This commit is contained in:
Charlie Gordon 2024-03-16 08:51:58 +01:00 committed by GitHub
parent 5d2202cad0
commit 5aef8b67b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -172,7 +172,7 @@ static JSValue js_printf_internal(JSContext *ctx,
uint8_t cbuf[UTF8_CHAR_LEN_MAX+1]; uint8_t cbuf[UTF8_CHAR_LEN_MAX+1];
JSValue res; JSValue res;
DynBuf dbuf; DynBuf dbuf;
const char *fmt_str; const char *fmt_str = NULL;
const uint8_t *fmt, *fmt_end; const uint8_t *fmt, *fmt_end;
const uint8_t *p; const uint8_t *p;
char *q; char *q;
@ -377,6 +377,7 @@ static JSValue js_printf_internal(JSContext *ctx,
return res; return res;
fail: fail:
JS_FreeCString(ctx, fmt_str);
dbuf_free(&dbuf); dbuf_free(&dbuf);
return JS_EXCEPTION; return JS_EXCEPTION;
} }
@ -2529,12 +2530,11 @@ static JSValue js_os_stat(JSContext *ctx, JSValue this_val,
else else
res = stat(path, &st); res = stat(path, &st);
#endif #endif
err = (res < 0) ? errno : 0;
JS_FreeCString(ctx, path); JS_FreeCString(ctx, path);
if (res < 0) { if (res < 0) {
err = errno;
obj = JS_NULL; obj = JS_NULL;
} else { } else {
err = 0;
obj = JS_NewObject(ctx); obj = JS_NewObject(ctx);
if (JS_IsException(obj)) if (JS_IsException(obj))
return JS_EXCEPTION; return JS_EXCEPTION;
@ -3499,11 +3499,12 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
memcpy(msg->data, data, data_len); memcpy(msg->data, data, data_len);
msg->data_len = data_len; msg->data_len = data_len;
if (sab_tab_len > 0) {
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len); msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
if (!msg->sab_tab) if (!msg->sab_tab)
goto fail; goto fail;
if (sab_tab_len > 0)
memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len); memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len);
}
msg->sab_tab_len = sab_tab_len; msg->sab_tab_len = sab_tab_len;
js_free(ctx, data); js_free(ctx, data);