Bug 1636318 - Use 16-bit string type for strings passed to nsTextFormatter::ssprintf. r=mkmelin
authorJorg K <jorgk@jorgk.com>
Tue, 12 May 2020 16:30:34 +0200
changeset 39123 d58c5ca03202c9d27ae5ccdfdb867ee442ff411d
parent 39122 8d4c676d5d53986359ff4214141f7f50015f04ef
child 39124 03bb86addb5dfce9e5756c712075f9aaa531a9c6
push id402
push userclokep@gmail.com
push dateMon, 29 Jun 2020 20:48:04 +0000
reviewersmkmelin
bugs1636318
Bug 1636318 - Use 16-bit string type for strings passed to nsTextFormatter::ssprintf. r=mkmelin
mailnews/compose/src/nsSmtpProtocol.cpp
--- a/mailnews/compose/src/nsSmtpProtocol.cpp
+++ b/mailnews/compose/src/nsSmtpProtocol.cpp
@@ -76,18 +76,18 @@ using namespace mozilla::mailnews;
 nsresult nsExplainErrorDetails(nsISmtpUrl *aSmtpUrl, nsresult aCode,
                                const char *arg1, const char *arg2) {
   NS_ENSURE_ARG(aSmtpUrl);
 
   nsCOMPtr<nsIPrompt> dialog;
   aSmtpUrl->GetPrompt(getter_AddRefs(dialog));
   NS_ENSURE_TRUE(dialog, NS_ERROR_FAILURE);
 
-  nsString msg;
-  nsString eMsg;
+  nsAutoString msg;
+  nsAutoString eMsg;
   nsCOMPtr<nsIStringBundleService> bundleService =
       mozilla::services::GetStringBundleService();
   NS_ENSURE_TRUE(bundleService, NS_ERROR_UNEXPECTED);
   nsCOMPtr<nsIStringBundle> bundle;
   nsresult rv = bundleService->CreateBundle(
       "chrome://messenger/locale/messengercompose/composeMsgs.properties",
       getter_AddRefs(bundle));
   NS_ENSURE_SUCCESS(rv, rv);
@@ -95,19 +95,16 @@ nsresult nsExplainErrorDetails(nsISmtpUr
   const char *exitString;
 #ifdef __GNUC__
 // Temporary workaround until bug 783526 is fixed.
 #  pragma GCC diagnostic push
 #  pragma GCC diagnostic ignored "-Wswitch"
 #endif
   switch (aCode) {
     case NS_ERROR_ILLEGAL_LOCALPART:
-      bundle->GetStringFromName("errorIllegalLocalPart", eMsg);
-      nsTextFormatter::ssprintf(msg, eMsg.get(), arg1, arg2);
-      break;
     case NS_ERROR_SMTP_SERVER_ERROR:
     case NS_ERROR_SMTP_SEND_NOT_ALLOWED:
     case NS_ERROR_SMTP_TEMP_SIZE_EXCEEDED:
     case NS_ERROR_SMTP_PERM_SIZE_EXCEEDED_1:
     case NS_ERROR_SMTP_PERM_SIZE_EXCEEDED_2:
     case NS_ERROR_SENDING_FROM_COMMAND:
     case NS_ERROR_SENDING_RCPT_COMMAND:
     case NS_ERROR_SENDING_DATA_COMMAND:
@@ -117,19 +114,25 @@ nsresult nsExplainErrorDetails(nsISmtpUr
     case NS_ERROR_CLIENTID_PERMISSION:
       exitString = errorStringNameForErrorCode(aCode);
       bundle->GetStringFromName(exitString, eMsg);
       if (aCode == NS_ERROR_SMTP_PERM_SIZE_EXCEEDED_1) {
         // Convert the error message argument back to integer since the error
         // message string smtpPermSizeExceeded1 contains a %d.
         // (The special case can be removed if that string ever changes, then
         // %d should be changed to %S.)
-        nsTextFormatter::ssprintf(msg, eMsg.get(), atoi(arg1), arg2);
+        nsTextFormatter::ssprintf(msg, eMsg.get(), atoi(arg1), (void *)nullptr);
       } else {
-        nsTextFormatter::ssprintf(msg, eMsg.get(), arg1, arg2);
+        // Some error strings contain %s. We're supplying 16-bit strings,
+        // so replace those with %S. It's a bit hacky. but it saves
+        // us tweaking some pretty old message strings.
+        eMsg.ReplaceSubstring(NS_LITERAL_STRING("%s"), NS_LITERAL_STRING("%S"));
+        nsTextFormatter::ssprintf(
+            msg, eMsg.get(), NS_ConvertUTF8toUTF16(arg1).get(),
+            arg2 ? NS_ConvertUTF8toUTF16(arg2).get() : nullptr);
       }
       break;
     default:
       NS_WARNING("falling to default error code");
       bundle->GetStringFromName("communicationsError", eMsg);
       nsTextFormatter::ssprintf(msg, eMsg.get(), static_cast<uint32_t>(aCode));
       break;
   }