Skip to content

Commit 007944d

Browse files
committed
Adding error checking to JsCreateWeakReference
If a tagged value or a non-RecyclableObject is passed in, we will now report an error rather than crashing non-deterministically in a future GC.
1 parent 8bc3d1a commit 007944d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/Jsrt/ChakraCommon.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ typedef unsigned short uint16_t;
223223
/// </summary>
224224
JsErrorModuleParsed,
225225
/// <summary>
226+
/// Argument passed to JsCreateWeakReference is a primitive that is not managed by the GC.
227+
/// No weak reference is required, the value will never be collected.
228+
/// </summary>
229+
JsNoWeakRefRequired,
230+
/// <summary>
226231
/// Category of errors that relates to errors occurring within the engine itself.
227232
/// </summary>
228233
JsErrorCategoryEngine = 0x20000,

lib/Jsrt/Jsrt.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4612,6 +4612,15 @@ CHAKRA_API JsCreateWeakReference(
46124612
PARAM_NOT_NULL(weakRef);
46134613
*weakRef = nullptr;
46144614

4615+
if (Js::TaggedNumber::Is(value))
4616+
{
4617+
return JsNoWeakRefRequired;
4618+
}
4619+
else if (!Js::RecyclableObject::Is(value))
4620+
{
4621+
return JsErrorInvalidArgument;
4622+
}
4623+
46154624
return GlobalAPIWrapper_NoRecord([&]() -> JsErrorCode {
46164625
ThreadContext* threadContext = ThreadContext::GetContextForCurrentThread();
46174626
if (threadContext == nullptr)

0 commit comments

Comments
 (0)