Bug 521937 - Crash using js-ctypes open method with a wrong path [@PR_UnloadLibrary | mozilla::ctypes::Library::Finalize]. r=dwitte
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Wed, 14 Oct 2009 12:57:42 +0900
changeset 33827 f1644b493ff525d61c2754e522fc3e72e0fcc984
parent 33826 6ff04bb6429bf1603851301e2de6760bce138846
child 33828 ba3c885034956e516d28735c2ca289d0ad4604c1
push idunknown
push userunknown
push dateunknown
reviewersdwitte
bugs521937
milestone1.9.3a1pre
Bug 521937 - Crash using js-ctypes open method with a wrong path [@PR_UnloadLibrary | mozilla::ctypes::Library::Finalize]. r=dwitte
js/ctypes/Library.cpp
js/ctypes/tests/unit/test_jsctypes.js.in
--- a/js/ctypes/Library.cpp
+++ b/js/ctypes/Library.cpp
@@ -68,16 +68,24 @@ static JSFunctionSpec sLibraryFunctions[
 
 JSObject*
 Library::Create(JSContext* cx, jsval aPath)
 {
   JSObject* libraryObj = JS_NewObject(cx, &sLibraryClass, NULL, NULL);
   if (!libraryObj)
     return NULL;
 
+  // initialize the library
+  if (!JS_SetReservedSlot(cx, libraryObj, 0, PRIVATE_TO_JSVAL(NULL)))
+    return NULL;
+
+  // initialize our Function list to empty
+  if (!JS_SetReservedSlot(cx, libraryObj, 1, PRIVATE_TO_JSVAL(NULL)))
+    return NULL;
+
   // attach API functions
   if (!JS_DefineFunctions(cx, libraryObj, sLibraryFunctions))
     return NULL;
 
   nsresult rv;
   nsCOMPtr<nsILocalFile> localFile;
 
   // get the path argument. we accept either an nsILocalFile or a string path.
@@ -112,20 +120,16 @@ Library::Create(JSContext* cx, jsval aPa
   rv = localFile->Load(&library);
   if (NS_FAILED(rv))
     return NULL;
 
   // stash the library
   if (!JS_SetReservedSlot(cx, libraryObj, 0, PRIVATE_TO_JSVAL(library)))
     return NULL;
 
-  // initialize our Function list to empty
-  if (!JS_SetReservedSlot(cx, libraryObj, 1, PRIVATE_TO_JSVAL(NULL)))
-    return NULL;
-
   return libraryObj;
 }
 
 PRLibrary*
 Library::GetLibrary(JSContext* cx, JSObject* obj)
 {
   JS_ASSERT(JS_GET_CLASS(cx, obj) == &sLibraryClass);
 
--- a/js/ctypes/tests/unit/test_jsctypes.js.in
+++ b/js/ctypes/tests/unit/test_jsctypes.js.in
@@ -104,16 +104,19 @@ function run_test()
   do_check_throws(function () { obj.close(); }, Error);
 
   // test that functions work as properties of other objects
   var test_i8 = library.declare("test_i8", ctypes.default_abi, ctypes.int8_t);
   do_check_eq(test_i8(), 123);
   obj.t = test_i8;
   do_check_eq(test_i8(), 123);
   do_check_eq(obj.t(), 123);
+
+  // bug 521937
+  do_check_throws(function () { var nolib = ctypes.open("notfoundlibrary.dll"); nolib.close(); }, Error);
 }
 
 function run_void_tests(library) {
   var test_v = library.declare("test_v", ctypes.default_abi, ctypes.void_t);
   do_check_eq(test_v(), undefined);
 }
 
 function run_int8_tests(library) {