Skip to content

Commit

Permalink
fix potential memory leak (bellard#318)
Browse files Browse the repository at this point in the history
- fix memory leak in `js_std_file_printf`
- fix `errno` clobber in `js_os_stat`
  • Loading branch information
chqrlie authored Mar 16, 2024
1 parent 5d2202c commit 5aef8b6
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ static JSValue js_printf_internal(JSContext *ctx,
uint8_t cbuf[UTF8_CHAR_LEN_MAX+1];
JSValue res;
DynBuf dbuf;
const char *fmt_str;
const char *fmt_str = NULL;
const uint8_t *fmt, *fmt_end;
const uint8_t *p;
char *q;
Expand Down Expand Up @@ -377,6 +377,7 @@ static JSValue js_printf_internal(JSContext *ctx,
return res;

fail:
JS_FreeCString(ctx, fmt_str);
dbuf_free(&dbuf);
return JS_EXCEPTION;
}
Expand Down Expand Up @@ -2529,12 +2530,11 @@ static JSValue js_os_stat(JSContext *ctx, JSValue this_val,
else
res = stat(path, &st);
#endif
err = (res < 0) ? errno : 0;
JS_FreeCString(ctx, path);
if (res < 0) {
err = errno;
obj = JS_NULL;
} else {
err = 0;
obj = JS_NewObject(ctx);
if (JS_IsException(obj))
return JS_EXCEPTION;
Expand Down Expand Up @@ -3499,11 +3499,12 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValue this_val,
memcpy(msg->data, data, data_len);
msg->data_len = data_len;

msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
if (!msg->sab_tab)
goto fail;
if (sab_tab_len > 0)
if (sab_tab_len > 0) {
msg->sab_tab = malloc(sizeof(msg->sab_tab[0]) * sab_tab_len);
if (!msg->sab_tab)
goto fail;
memcpy(msg->sab_tab, sab_tab, sizeof(msg->sab_tab[0]) * sab_tab_len);
}
msg->sab_tab_len = sab_tab_len;

js_free(ctx, data);
Expand Down

0 comments on commit 5aef8b6

Please sign in to comment.