Bug 751262 - Guard against leaking memory if NewDirectByteBuffer fails. r=blassey a=mfinkle
authorKartikaya Gupta <kgupta@mozilla.com>
Thu, 10 May 2012 13:15:56 -0400
changeset 95740 6b1cf014bfd58078f76d63289e0caece8b90c026
parent 95739 ff46c92d32e601cd13e02aa2609eac49fe30b759
child 95741 8de48951c0ac6c75d7879b6e58a13bd3ae74c94b
push idunknown
push userunknown
push dateunknown
reviewersblassey, mfinkle
bugs751262
milestone14.0a2
Bug 751262 - Guard against leaking memory if NewDirectByteBuffer fails. r=blassey a=mfinkle
mozglue/android/nsGeckoUtils.cpp
--- a/mozglue/android/nsGeckoUtils.cpp
+++ b/mozglue/android/nsGeckoUtils.cpp
@@ -61,17 +61,24 @@ Java_org_mozilla_gecko_GeckoAppShell_put
     jenv->ReleaseStringUTFChars(map, str);
 }
 
 extern "C"
 __attribute__ ((visibility("default")))
 jobject JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_allocateDirectBuffer(JNIEnv *jenv, jclass, jlong size)
 {
-    return jenv->NewDirectByteBuffer(malloc(size), size);
+    jobject buffer = NULL;
+    void* mem = malloc(size);
+    if (mem) {
+        buffer = jenv->NewDirectByteBuffer(mem, size);
+        if (!buffer)
+            free(mem);
+    }
+    return buffer;
 }
 
 extern "C"
 __attribute__ ((visibility("default")))
 void JNICALL
 Java_org_mozilla_gecko_GeckoAppShell_freeDirectBuffer(JNIEnv *jenv, jclass, jobject buf)
 {
     free(jenv->GetDirectBufferAddress(buf));
@@ -102,9 +109,9 @@ Java_org_mozilla_gecko_GeckoAppShell_unl
 
     // Release any existing lock in the file
     if (result != -1 && lock.l_type == F_WRLCK) {
         lock.l_type = F_UNLCK;
         fcntl(fd, F_SETLK, &lock);
     }
 
     close(fd);
-}
\ No newline at end of file
+}