Fix a leak in returning HTTP responses.

This commit is contained in:
Cory McWilliams 2024-02-25 19:38:00 -05:00
parent f4697fe7f7
commit 40f3da6a65

View File

@ -80,11 +80,13 @@ static JSValue _httpd_response_end(JSContext* context, JSValueConst this_val, in
{ {
tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id); tf_http_request_t* request = JS_GetOpaque(this_val, _httpd_request_class_id);
size_t length = 0; size_t length = 0;
const char* cstring = NULL;
const void* data = NULL; const void* data = NULL;
JSValue buffer = JS_UNDEFINED; JSValue buffer = JS_UNDEFINED;
if (JS_IsString(argv[0])) if (JS_IsString(argv[0]))
{ {
data = JS_ToCStringLen(context, &length, argv[0]); cstring = JS_ToCStringLen(context, &length, argv[0]);
data = cstring;
} }
else if ((data = tf_util_try_get_array_buffer(context, &length, argv[0])) != 0) else if ((data = tf_util_try_get_array_buffer(context, &length, argv[0])) != 0)
{ {
@ -117,6 +119,10 @@ static JSValue _httpd_response_end(JSContext* context, JSValueConst this_val, in
JS_FreeCString(context, headers[i]); JS_FreeCString(context, headers[i]);
} }
JS_FreeValue(context, buffer); JS_FreeValue(context, buffer);
if (cstring)
{
JS_FreeCString(context, cstring);
}
return JS_UNDEFINED; return JS_UNDEFINED;
} }