-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add missing JS_FreeValue calls for early returns #468
Add missing JS_FreeValue calls for early returns #468
Conversation
75c5293
to
eea668e
Compare
} | ||
else | ||
{ | ||
// No need to free value types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe want to handle this error better ....
out += "<failed-to-stringify>"
or some such
@@ -5114,15 +5123,20 @@ DEFINE_JS_FUNCTION(JSValue, emit, JSValue raw_tx) | |||
// stringify it | |||
JSValue sdata = | |||
JS_JSONStringify(ctx, raw_tx, JS_UNDEFINED, JS_UNDEFINED); | |||
if (JS_IsException(sdata)) | |||
if (JS_IsException(sdata) || JS_IsUndefined(sdata)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just use !JS_IsString
?
} | ||
else | ||
{ | ||
out += "<could not display data>"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
matches existing "<could not display hex>"
if (JS_IsBool(as_hex) && !!JS_ToBool(ctx, as_hex))
{
auto in = FromJSIntArrayOrHexString(ctx, data, 64 * 1024);
if (in.has_value())
{
if (in->size() > 1024)
in->resize(1024);
out += strHex(*in);
}
else
out += "<could not display hex>";
}
merged manually |
📌 Pull Request Overview
🚩 Issue / Context
This PR addresses potential memory leaks and incorrect handling of return values in QuickJS-based transaction serialization (
JS_JSONStringify
calls), particularly around the correct freeing of JavaScript values and C-string conversions.✅ What Has Changed?
Explicit handling of
JS_JSONStringify
return values:JS_JSONStringify
can return:JSValue
requiring GC cleanup).JS_EXCEPTION
), represented as a special tagged union value (JS_MKVAL(JS_TAG_EXCEPTION, 0)
), which doesn't require cleanup.undefined
, represented similarly by a tagged value (JS_MKVAL(JS_TAG_UNDEFINED, 0)
), also requiring no cleanup.JS_IsException
andJS_IsUndefined
, handling these special tagged values gracefully and safely.Ensuring correct memory management:
JS_FreeCString
andJS_FreeValue
are now explicitly placed after every successful conversion to avoid leaks.🔍 Explanation of
JS_MKVAL
ValuesJS_MKVAL
creates lightweight, non-garbage-collected QuickJS "value types":JS_FreeValue
needed).JS_EXCEPTION
,JS_UNDEFINED
, and integer primitives.JS_JSONStringify
JS_JSONStringify
will explicitly return:JS_EXCEPTION
when there's an internal error (e.g., cyclic structures or invalid replacer).JS_UNDEFINED
when it can't serialize a given input (e.g.,undefined
, function, or symbol).Both are special
JS_MKVAL
values (value types), hence no GC cleanup required.🚀 Summary of PR Changes