diff --git a/.changeset/tidy-hotels-rule.md b/.changeset/tidy-hotels-rule.md new file mode 100644 index 00000000..6644d613 --- /dev/null +++ b/.changeset/tidy-hotels-rule.md @@ -0,0 +1,5 @@ +--- +'nxjs-runtime': patch +--- + +Fix string memory leak in `Switch.readFileSync()` diff --git a/source/fs.c b/source/fs.c index f71d7c2f..0e8bb66b 100644 --- a/source/fs.c +++ b/source/fs.c @@ -163,6 +163,7 @@ JSValue nx_read_file_sync(JSContext *ctx, JSValueConst this_val, int argc, JSVal JS_FreeCString(ctx, filename); return JS_EXCEPTION; } + JS_FreeCString(ctx, filename); fseek(file, 0, SEEK_END); size_t size = ftell(file); @@ -171,7 +172,6 @@ JSValue nx_read_file_sync(JSContext *ctx, JSValueConst this_val, int argc, JSVal uint8_t *buffer = js_malloc(ctx, size); if (buffer == NULL) { - JS_FreeCString(ctx, filename); fclose(file); JS_ThrowOutOfMemory(ctx); return JS_EXCEPTION; @@ -182,7 +182,6 @@ JSValue nx_read_file_sync(JSContext *ctx, JSValueConst this_val, int argc, JSVal if (result != size) { - JS_FreeCString(ctx, filename); js_free(ctx, buffer); JS_ThrowTypeError(ctx, "Failed to read entire file. Got %lu, expected %lu", result, size); return JS_EXCEPTION;