diff --git a/intl/strres/src/nsStringBundle.cpp b/intl/strres/src/nsStringBundle.cpp index 6ebb5c9ca504..13da09708036 100644 --- a/intl/strres/src/nsStringBundle.cpp +++ b/intl/strres/src/nsStringBundle.cpp @@ -385,7 +385,7 @@ nsStringBundle::FormatString(const PRUnichar *aFormatStr, // 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, @@ -397,7 +397,21 @@ nsStringBundle::FormatString(const PRUnichar *aFormatStr, 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)