Fix file read leaks.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3885 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-06-02 09:40:19 +00:00
parent 3bc428a83e
commit 58f459fb3b

View File

@ -72,7 +72,7 @@ static void _file_read_read_callback(uv_fs_t* req)
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
if (req->result >= 0)
{
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (uint8_t*)(req + 1), req->result);
JSValue arrayBuffer = JS_NewArrayBufferCopy(context, (const uint8_t*)fsreq->buffer, req->result);
JSValue global = JS_GetGlobalObject(context);
JSValue constructor = JS_GetPropertyStr(context, global, "Uint8Array");
JSValue typedArray = JS_CallConstructor(context, constructor, 1, &arrayBuffer);
@ -89,25 +89,30 @@ static void _file_read_read_callback(uv_fs_t* req)
int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0)
{
free(req);
free(fsreq);
}
}
static void _file_read_open_callback(uv_fs_t* req)
{
uv_fs_req_cleanup(req);
fs_req_t* fsreq = (fs_req_t*)req;
tf_task_t* task = req->loop->data;
JSContext* context = tf_task_get_context(task);
promiseid_t promise = (promiseid_t)(intptr_t)req->data;
if (req->result >= 0)
{
uv_buf_t buf = { .base = (char*)(req + 1), .len = k_file_read_max };
uv_file file = req->result;
int result = uv_fs_read(req->loop, req, file, &buf, 1, 0, _file_read_read_callback);
uv_buf_t buf = { .base = fsreq->buffer, .len = fsreq->size };
fsreq->file = req->result;
int result = uv_fs_read(req->loop, req, fsreq->file, &buf, 1, 0, _file_read_read_callback);
if (result < 0)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
result = uv_fs_close(req->loop, req, file, _file_async_close_callback);
result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0)
{
free(fsreq);
}
}
}
else
@ -131,6 +136,7 @@ static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int ar
{
.data = (void*)(intptr_t)promise,
},
.size = k_file_read_max,
};
int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_RDONLY, 0, _file_read_open_callback);
if (result < 0)
@ -157,7 +163,11 @@ static void _file_write_write_callback(uv_fs_t* req)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(req->result)));
}
uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0)
{
free(fsreq);
}
}
static void _file_write_open_callback(uv_fs_t* req)
@ -176,6 +186,10 @@ static void _file_write_open_callback(uv_fs_t* req)
{
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, uv_strerror(result)));
result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0)
{
free(fsreq);
}
}
}
else