From 306f8ef412d32273bcc5eecab02443182c2e3f95 Mon Sep 17 00:00:00 2001 From: "nhotta%netscape.com" Date: Mon, 19 Apr 1999 19:15:51 +0000 Subject: [PATCH] Bug fix #3994, realloc if the converter's estimate length is incorrect. --- mailnews/compose/src/msgCompGlue.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/mailnews/compose/src/msgCompGlue.cpp b/mailnews/compose/src/msgCompGlue.cpp index 11a5afb2e85..393cddf33e2 100644 --- a/mailnews/compose/src/msgCompGlue.cpp +++ b/mailnews/compose/src/msgCompGlue.cpp @@ -107,9 +107,24 @@ nsresult ConvertFromUnicode(const nsString aCharset, // allocale an output buffer *outCString = (char *) PR_Malloc(dstLength + 1); if (*outCString != nsnull) { + PRInt32 originalLength = unicharLength; + PRInt32 estimatedLength = dstLength; // convert from unicode res = encoder->Convert(unichars, &unicharLength, *outCString, &dstLength); - (*outCString)[dstLength] = '\0'; + // buffer was too small, reallocate buffer and try again + if (unicharLength < originalLength) { + PR_Free(*outCString); + unicharLength = originalLength; + dstLength = estimatedLength * 4; // estimation was not correct + *outCString = (char *) PR_Malloc(dstLength + 1); + if (*outCString != nsnull) { + res = encoder->Convert(unichars, &unicharLength, *outCString, &dstLength); + NS_ASSERTION(unicharLength == originalLength, "unicharLength != originalLength"); + } + } + if (*outCString != nsnull) { + (*outCString)[dstLength] = '\0'; + } } else { res = NS_ERROR_OUT_OF_MEMORY;