Skip to content

Commit

Permalink
Fix memory leak in TCP write() function
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Oct 25, 2023
1 parent bae9f60 commit 8cffbfa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-humans-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nxjs-runtime': patch
---

Fix memory leak in TCP `write()` function
16 changes: 13 additions & 3 deletions source/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef struct
{
JSContext *context;
JSValue callback;
JSValue buffer;
} nx_js_callback_t;

void nx_on_connect(nx_poll_t *p, nx_connect_t *req)
Expand Down Expand Up @@ -54,16 +55,20 @@ JSValue nx_js_tcp_connect(JSContext *ctx, JSValueConst this_val, int argc, JSVal
nx_js_callback_t *req_cb = malloc(sizeof(nx_js_callback_t));
req_cb->context = ctx;
req_cb->callback = JS_DupValue(ctx, argv[0]);
req_cb->buffer = JS_UNDEFINED;
req->opaque = req_cb;

nx_tcp_connect(&nx_ctx->poll, req, ip, port, nx_on_connect);
JS_FreeCString(ctx, ip);

return JS_UNDEFINED;
}

void nx_on_read(nx_poll_t *p, nx_read_t *req)
{
nx_js_callback_t *req_cb = (nx_js_callback_t *)req->opaque;
JS_FreeValue(req_cb->context, req_cb->buffer);

JSValue args[] = {JS_UNDEFINED, JS_UNDEFINED};

if (req->err)
Expand Down Expand Up @@ -91,11 +96,11 @@ void nx_on_read(nx_poll_t *p, nx_read_t *req)
JSValue nx_js_tcp_read(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
size_t buffer_size;
uint8_t *buffer = JS_GetArrayBuffer(ctx, &buffer_size, argv[2]);
JSValue buffer_value = JS_DupValue(ctx, argv[2]);
uint8_t *buffer = JS_GetArrayBuffer(ctx, &buffer_size, buffer_value);
int fd;
if (!buffer || JS_ToInt32(ctx, &fd, argv[1]))
{
JS_ThrowTypeError(ctx, "invalid input");
return JS_EXCEPTION;
}

Expand All @@ -104,6 +109,7 @@ JSValue nx_js_tcp_read(JSContext *ctx, JSValueConst this_val, int argc, JSValueC
nx_js_callback_t *req_cb = malloc(sizeof(nx_js_callback_t));
req_cb->context = ctx;
req_cb->callback = JS_DupValue(ctx, argv[0]);
req_cb->buffer = buffer_value;
req->opaque = req_cb;

nx_read(&nx_ctx->poll, req, fd, buffer, buffer_size, nx_on_read);
Expand All @@ -114,12 +120,15 @@ JSValue nx_js_tcp_read(JSContext *ctx, JSValueConst this_val, int argc, JSValueC
void nx_on_write(nx_poll_t *p, nx_write_t *req)
{
nx_js_callback_t *req_cb = (nx_js_callback_t *)req->opaque;
JS_FreeValue(req_cb->context, req_cb->buffer);

JSValue args[] = {JS_UNDEFINED, JS_UNDEFINED};

if (req->err)
{
/* Error during read. */
/* Error during write. */
// printf("Error occurred during connect: %s\n", strerror(so_error));
args[0] = JS_NewError(req_cb->context);
}
else
{
Expand Down Expand Up @@ -155,6 +164,7 @@ JSValue nx_js_tcp_write(JSContext *ctx, JSValueConst this_val, int argc, JSValue
nx_js_callback_t *req_cb = malloc(sizeof(nx_js_callback_t));
req_cb->context = ctx;
req_cb->callback = JS_DupValue(ctx, argv[0]);
req_cb->buffer = buffer_val;
req->opaque = req_cb;

nx_write(&nx_ctx->poll, req, fd, buffer, buffer_size, nx_on_write);
Expand Down

0 comments on commit 8cffbfa

Please sign in to comment.