Bug 1406746 - Fix ctypes error handling when creating FunctionInfo r=sfink
☠☠ backed out by 2a21d7780597 ☠ ☠
authorJon Coppeard <jcoppeard@mozilla.com>
Tue, 10 Oct 2017 12:07:08 +0100
changeset 427913 3b19f13cf22a8ceb6597115563ac5ace6da2c872
parent 427912 36ab7e9b2f2f7b2520569203c30206d3e8313fc8
child 427914 29e70589594f093939288a1a1aee95841c3a3539
push id97
push userfmarier@mozilla.com
push dateSat, 14 Oct 2017 01:12:59 +0000
reviewerssfink
bugs1406746
milestone58.0a1
Bug 1406746 - Fix ctypes error handling when creating FunctionInfo r=sfink
js/src/ctypes/CTypes.cpp
--- a/js/src/ctypes/CTypes.cpp
+++ b/js/src/ctypes/CTypes.cpp
@@ -6845,35 +6845,32 @@ FunctionType::BuildSymbolName(JSString* 
 
 static bool
 CreateFunctionInfo(JSContext* cx,
                    HandleObject typeObj,
                    HandleValue abiType,
                    HandleObject returnType,
                    const HandleValueArray& args)
 {
-  FunctionInfo* fninfo(cx->new_<FunctionInfo>());
+  auto fninfo = cx->make_unique<FunctionInfo>();
   if (!fninfo) {
     JS_ReportOutOfMemory(cx);
     return false;
   }
 
-  // Stash the FunctionInfo in a reserved slot.
-  JS_SetReservedSlot(typeObj, SLOT_FNINFO, PrivateValue(fninfo));
-
   ffi_abi abi;
   if (!GetABI(cx, abiType, &abi)) {
     JS_ReportErrorASCII(cx, "Invalid ABI specification");
     return false;
   }
   fninfo->mABI = abiType.toObjectOrNull();
 
   fninfo->mReturnType = returnType;
 
-  // prepare the argument types
+  // Prepare the argument types.
   if (!fninfo->mArgTypes.reserve(args.length()) ||
       !fninfo->mFFITypes.reserve(args.length())) {
     JS_ReportOutOfMemory(cx);
     return false;
   }
 
   fninfo->mIsVariadic = false;
 
@@ -6908,24 +6905,22 @@ CreateFunctionInfo(JSContext* cx,
     ffi_type* ffiType = CType::GetFFIType(cx, argType);
     if (!ffiType)
       return false;
 
     fninfo->mArgTypes.infallibleAppend(argType);
     fninfo->mFFITypes.infallibleAppend(ffiType);
   }
 
-  if (fninfo->mIsVariadic) {
-    // wait to PrepareCIF until function is called
-    return true;
-  }
-
-  if (!PrepareCIF(cx, fninfo))
-    return false;
-
+  // For variadic functions, wait to PrepareCIF until the function is called.
+  if (!fninfo->mIsVariadic && !PrepareCIF(cx, fninfo.get()))
+    return false;
+
+  // Now the FunctionInfo has been initialized, store it in a reserved slot.
+  JS_SetReservedSlot(typeObj, SLOT_FNINFO, PrivateValue(fninfo.release()));
   return true;
 }
 
 bool
 FunctionType::Create(JSContext* cx, unsigned argc, Value* vp)
 {
   // Construct and return a new FunctionType object.
   CallArgs args = CallArgsFromVp(argc, vp);