Skip to content

Commit

Permalink
Fix string memory leak in Switch.readFileSync()
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 6, 2023
1 parent 2a7ce81 commit d5e032e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-hotels-rule.md
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Fix string memory leak in `Switch.readFileSync()`
3 changes: 1 addition & 2 deletions source/fs.c
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit d5e032e

Please sign in to comment.