Bug 865106 - Remove allocator mismatch when freeing asprintf allocated buffers. r=kats
authorMike Hommey <mh+mozilla@glandium.org>
Wed, 24 Apr 2013 19:35:22 +0200
changeset 129772 e38bf6995ed598df0e4c67acc2c2428fee492598
parent 129771 194300c339f42c71b869c73a296bb19daa28b325
child 129773 0921c42708f22ed87e554a1bc8573b8ec5a55876
push idunknown
push userunknown
push dateunknown
reviewerskats
bugs865106
milestone23.0a1
Bug 865106 - Remove allocator mismatch when freeing asprintf allocated buffers. r=kats
mozglue/android/NSSBridge.cpp
mozglue/android/SQLiteBridge.cpp
--- a/mozglue/android/NSSBridge.cpp
+++ b/mozglue/android/NSSBridge.cpp
@@ -8,16 +8,23 @@
 #include "APKOpen.h"
 #ifdef ANDROID
 #include <jni.h>
 #include <android/log.h>
 #endif
 
 #include "ElfLoader.h"
 
+#ifdef MOZ_MEMORY
+// libc's free().
+extern "C" void __real_free(void *);
+#else
+#define __real_free(a) free(a)
+#endif
+
 #ifdef DEBUG
 #define LOG(x...) __android_log_print(ANDROID_LOG_INFO, "GeckoJNI", x)
 #else
 #define LOG(x...)
 #endif
 
 static bool initialized = false;
 
@@ -79,17 +86,18 @@ throwError(JNIEnv* jenv, const char * fu
     char *msg;
 
     PRErrorCode perr = f_PR_GetError();
     char * errString = f_PR_ErrorToString(perr, 0);
     asprintf(&msg, "%s returned error %d: %s\n", funcString, perr, errString);
     LOG("Throwing error: %s\n", msg);
 
     JNI_Throw(jenv, "java/lang/Exception", msg);
-    free(msg);
+    // msg is allocated by asprintf, it needs to be freed by libc.
+    __real_free(msg);
     LOG("Error thrown\n");
 }
 
 extern "C" NS_EXPORT jstring JNICALL
 Java_org_mozilla_gecko_NSSBridge_nativeEncrypt(JNIEnv* jenv, jclass,
                                                jstring jPath,
                                                jstring jValue)
 {
--- a/mozglue/android/SQLiteBridge.cpp
+++ b/mozglue/android/SQLiteBridge.cpp
@@ -6,16 +6,23 @@
 #include <stdio.h>
 #include <jni.h>
 #include <android/log.h>
 #include "dlfcn.h"
 #include "APKOpen.h"
 #include "ElfLoader.h"
 #include "SQLiteBridge.h"
 
+#ifdef MOZ_MEMORY
+// libc's free().
+extern "C" void __real_free(void *);
+#else
+#define __real_free(a) free(a)
+#endif
+
 #ifdef DEBUG
 #define LOG(x...) __android_log_print(ANDROID_LOG_INFO, "GeckoJNI", x)
 #else
 #define LOG(x...)
 #endif
 
 #define SQLITE_WRAPPER_INT(name) name ## _t f_ ## name;
 
@@ -145,17 +152,18 @@ Java_org_mozilla_gecko_sqlite_SQLiteBrid
 
     dbPath = jenv->GetStringUTFChars(jDb, NULL);
     rc = f_sqlite3_open(dbPath, &db);
     jenv->ReleaseStringUTFChars(jDb, dbPath);
     if (rc != SQLITE_OK) {
         asprintf(&errorMsg, "Can't open database: %s\n", f_sqlite3_errmsg(db));
         LOG("Error in SQLiteBridge: %s\n", errorMsg);
         JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", errorMsg);
-        free(errorMsg);
+        // errorMsg is allocated by asprintf, it needs to be freed by libc.
+        __real_free(errorMsg);
     } else {
       jCursor = sqliteInternalCall(jenv, db, jQuery, jParams, jQueryRes);
     }
     f_sqlite3_close(db);
     return jCursor;
 }
 
 extern "C" NS_EXPORT jobject JNICALL
@@ -186,17 +194,18 @@ Java_org_mozilla_gecko_sqlite_SQLiteBrid
 
     dbPath = jenv->GetStringUTFChars(jDb, NULL);
     rc = f_sqlite3_open(dbPath, &db);
     jenv->ReleaseStringUTFChars(jDb, dbPath);
     if (rc != SQLITE_OK) {
         asprintf(&errorMsg, "Can't open database: %s\n", f_sqlite3_errmsg(db));
         LOG("Error in SQLiteBridge: %s\n", errorMsg);
         JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", errorMsg);
-        free(errorMsg);
+        // errorMsg is allocated by asprintf, it needs to be freed by libc.
+        __real_free(errorMsg);
     }
     return (jlong)db;
 }
 
 extern "C" NS_EXPORT void JNICALL
 Java_org_mozilla_gecko_sqlite_SQLiteBridge_closeDatabase(JNIEnv* jenv, jclass,
                                                         jlong jDb)
 {
@@ -389,11 +398,12 @@ sqliteInternalCall(JNIEnv* jenv,
         goto error_close;
     }
 
     return jCursor;
 
 error_close:
     LOG("Error in SQLiteBridge: %s\n", errorMsg);
     JNI_Throw(jenv, "org/mozilla/gecko/sqlite/SQLiteBridgeException", errorMsg);
-    free(errorMsg);
+    // errorMsg is allocated by asprintf, it needs to be freed by libc.
+    __real_free(errorMsg);
     return jCursor;
 }