Bug 384803 nsStringBundle::FormatStringFromID should return nsMemory allocated strings
authortimeless@mozdev.org
Sun, 01 Jul 2007 12:29:43 -0700
changeset 3003 36bff944e2a7645ea01431f540e8de8f7624f404
parent 3002 00916f74e437bc2cdd4667725a0e6bd827dc8a03
child 3004 cf070903c1c1a06a13f9728c0e81db19c8da42d4
push id1
push userbsmedberg@mozilla.com
push dateThu, 20 Mar 2008 16:49:24 +0000
treeherdermozilla-central@61007906a1f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs384803
milestone1.9a6pre
Bug 384803 nsStringBundle::FormatStringFromID should return nsMemory allocated strings patch by prasad@medhas.org r=timeless r=smontagu sr=biesi
intl/strres/src/nsStringBundle.cpp
--- a/intl/strres/src/nsStringBundle.cpp
+++ b/intl/strres/src/nsStringBundle.cpp
@@ -380,29 +380,43 @@ nsStringBundle::FormatString(const PRUni
   NS_ENSURE_ARG(aLength <= 10); // enforce 10-parameter limit
 
   // implementation note: you would think you could use vsmprintf
   // to build up an arbitrary length array.. except that there
   // is no way to build up a va_list at runtime!
   // Don't believe me? See:
   //   http://www.eskimo.com/~scs/C-faq/q15.13.html
   // -alecf
-  *aResult = 
+  PRUnichar *text = 
     nsTextFormatter::smprintf(aFormatStr,
                               aLength >= 1 ? aParams[0] : nsnull,
                               aLength >= 2 ? aParams[1] : nsnull,
                               aLength >= 3 ? aParams[2] : nsnull,
                               aLength >= 4 ? aParams[3] : nsnull,
                               aLength >= 5 ? aParams[4] : nsnull,
                               aLength >= 6 ? aParams[5] : nsnull,
                               aLength >= 7 ? aParams[6] : nsnull,
                               aLength >= 8 ? aParams[7] : nsnull,
                               aLength >= 9 ? aParams[8] : nsnull,
                               aLength >= 10 ? aParams[9] : nsnull);
-  return NS_OK;
+
+  if (!text) {
+    *aResult = nsnull;
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
+  // nsTextFormatter does not use the shared nsMemory allocator.
+  // Instead it is required to free the memory it allocates using
+  // nsTextFormatter::smprintf_free.  Let's instead use nsMemory based
+  // allocation for the result that we give out and free the string
+  // returned by smprintf ourselves!
+  *aResult = NS_strdup(text);
+  nsTextFormatter::smprintf_free(text);
+
+  return *aResult ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
 }
 
 NS_IMPL_ISUPPORTS1(nsExtensibleStringBundle, nsIStringBundle)
 
 nsExtensibleStringBundle::nsExtensibleStringBundle()
 {
   mLoaded = PR_FALSE;
 }