Bug 1392529 - Port bug 1385172 to mailnews: Replace nsEscapeHTML{,2}() with new nsAppendEscapedHTML() function. rs=bustage-fix
authorJorg K <jorgk@jorgk.com>
Tue, 22 Aug 2017 11:03:28 +0200
changeset 22013 80e43346d487e28f199f948c27f434fca905d227
parent 22012 62701153a5506529538c0e99fbd4d3b6a3ce47ba
child 22014 29da01df42f29352ffbe2bab951b2abcca6cbc94
push id13437
push usermozilla@jorgk.com
push dateTue, 22 Aug 2017 09:50:08 +0000
treeherdercomm-central@80e43346d487 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbustage-fix
bugs1392529, 1385172
Bug 1392529 - Port bug 1385172 to mailnews: Replace nsEscapeHTML{,2}() with new nsAppendEscapedHTML() function. rs=bustage-fix
mailnews/base/search/src/nsMsgFilter.cpp
mailnews/base/src/nsSpamSettings.cpp
mailnews/base/util/nsMsgUtils.h
mailnews/compose/src/nsMsgCompose.cpp
mailnews/compose/src/nsMsgComposeService.cpp
mailnews/mime/emitters/nsMimeBaseEmitter.cpp
mailnews/mime/emitters/nsMimeHtmlEmitter.cpp
mailnews/mime/emitters/nsMimeXmlEmitter.cpp
mailnews/mime/src/mimedrft.cpp
mailnews/mime/src/mimeebod.cpp
mailnews/mime/src/mimei.cpp
--- a/mailnews/base/search/src/nsMsgFilter.cpp
+++ b/mailnews/base/search/src/nsMsgFilter.cpp
@@ -666,23 +666,21 @@ nsMsgFilter::LogRuleHitGeneric(nsIMsgRul
 
     rv = logStream->Write(timestampString.get(), timestampString.Length(), &writeCount);
     NS_ENSURE_SUCCESS(rv,rv);
     NS_ASSERTION(writeCount == timestampString.Length(), "failed to write out timestamp");
 
     // HTML-escape the log for security reasons.
     // We don't want someone to send us a message with a subject with
     // HTML tags, especially <script>.
-    char *escapedBuffer = MsgEscapeHTML(buffer.get());
-    if (!escapedBuffer)
-      return NS_ERROR_OUT_OF_MEMORY;
+    nsCString escapedBuffer;
+    nsAppendEscapedHTML(buffer, escapedBuffer);
 
-    uint32_t escapedBufferLen = strlen(escapedBuffer);
-    rv = logStream->Write(escapedBuffer, escapedBufferLen, &writeCount);
-    PR_Free(escapedBuffer);
+    uint32_t escapedBufferLen = escapedBuffer.Length();
+    rv = logStream->Write(escapedBuffer.get(), escapedBufferLen, &writeCount);
     NS_ENSURE_SUCCESS(rv,rv);
     NS_ASSERTION(writeCount == escapedBufferLen, "failed to write out log hit");
 
     rv = logStream->Write(LOG_ENTRY_END_TAG, LOG_ENTRY_END_TAG_LEN, &writeCount);
     NS_ENSURE_SUCCESS(rv,rv);
     NS_ASSERTION(writeCount == LOG_ENTRY_END_TAG_LEN, "failed to write out end log tag");
     return NS_OK;
 }
--- a/mailnews/base/src/nsSpamSettings.cpp
+++ b/mailnews/base/src/nsSpamSettings.cpp
@@ -741,23 +741,21 @@ NS_IMETHODIMP nsSpamSettings::LogJunkStr
 
   rv = logStream->Write(timestampString.get(), timestampString.Length(), &writeCount);
   NS_ENSURE_SUCCESS(rv,rv);
   NS_ASSERTION(writeCount == timestampString.Length(), "failed to write out timestamp");
 
   // HTML-escape the log for security reasons.
   // We don't want someone to send us a message with a subject with
   // HTML tags, especially <script>.
-  char *escapedBuffer = MsgEscapeHTML(string);
-  if (!escapedBuffer)
-    return NS_ERROR_OUT_OF_MEMORY;
+  nsCString escapedBuffer;
+  nsAppendEscapedHTML(nsDependentCString(string), escapedBuffer);
 
-  uint32_t escapedBufferLen = strlen(escapedBuffer);
-  rv = logStream->Write(escapedBuffer, escapedBufferLen, &writeCount);
-  PR_Free(escapedBuffer);
+  uint32_t escapedBufferLen = escapedBuffer.Length();
+  rv = logStream->Write(escapedBuffer.get(), escapedBufferLen, &writeCount);
   NS_ENSURE_SUCCESS(rv,rv);
   NS_ASSERTION(writeCount == escapedBufferLen, "failed to write out log hit");
 
   rv = logStream->Write(LOG_ENTRY_END_TAG, LOG_ENTRY_END_TAG_LEN, &writeCount);
   NS_ENSURE_SUCCESS(rv,rv);
   NS_ASSERTION(writeCount == LOG_ENTRY_END_TAG_LEN, "failed to write out end log tag");
   return NS_OK;
 }
--- a/mailnews/base/util/nsMsgUtils.h
+++ b/mailnews/base/util/nsMsgUtils.h
@@ -332,20 +332,16 @@ ConvertBufToPlainText(nsString &aConBuf,
  * using the internal API we can simply redirect the calls appropriately.
  */
 #define MsgLowerCaseEqualsLiteral(str, l) \
         (str).LowerCaseEqualsLiteral(l)
 #define MsgRFindChar(str, ch, len) \
         (str).RFindChar(ch, len)
 #define MsgCompressWhitespace(str) \
         (str).CompressWhitespace()
-#define MsgEscapeHTML(str) \
-        nsEscapeHTML(str)
-#define MsgEscapeHTML2(buffer, len) \
-        nsEscapeHTML2(buffer, len)
 #define MsgReplaceSubstring(str, what, replacement) \
         (str).ReplaceSubstring(what, replacement)
 #define MsgIsUTF8(str) \
         IsUTF8(str)
 #define MsgNewInterfaceRequestorAggregation(aFirst, aSecond, aResult) \
         NS_NewInterfaceRequestorAggregation(aFirst, aSecond, aResult)
 #define MsgNewNotificationCallbacksAggregation(aCallbacks, aLoadGroup, aResult) \
         NS_NewNotificationCallbacksAggregation(aCallbacks, aLoadGroup, aResult)
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -4126,24 +4126,20 @@ nsMsgCompose::ConvertTextToHTML(nsIFile 
 
   rv = LoadDataFromFile(aSigFile, origBuf);
   if (NS_FAILED(rv))
     return rv;
 
   // Ok, once we are here, we need to escape the data to make sure that
   // we don't do HTML stuff with plain text sigs.
   //
-  char16_t *escaped = MsgEscapeHTML2(origBuf.get(), origBuf.Length());
-  if (escaped)
-  {
-    aSigData.Append(escaped);
-    NS_Free(escaped);
-  }
-  else
-    aSigData.Append(origBuf);
+  nsCString escapedUTF8;
+  nsAppendEscapedHTML(NS_ConvertUTF16toUTF8(origBuf), escapedUTF8);
+  aSigData.Append(NS_ConvertUTF8toUTF16(escapedUTF8));
+
   return NS_OK;
 }
 
 nsresult
 nsMsgCompose::LoadDataFromFile(nsIFile *file, nsString &sigData,
                                bool aAllowUTF8, bool aAllowUTF16)
 {
   int32_t       readSize;
@@ -4521,24 +4517,19 @@ nsMsgCompose::ProcessSignature(nsIMsgIde
       if (htmlSig)
         ConvertBufToPlainText(prefSigText, false, false, true, true);
       sigData.Append(prefSigText);
     }
     else
     {
       if (!htmlSig)
       {
-        char16_t* escaped = MsgEscapeHTML2(prefSigText.get(), prefSigText.Length());
-        if (escaped)
-        {
-          sigData.Append(escaped);
-          NS_Free(escaped);
-        }
-        else
-          sigData.Append(prefSigText);
+        nsCString escapedUTF8;
+        nsAppendEscapedHTML(NS_ConvertUTF16toUTF8(prefSigText), escapedUTF8);
+        sigData.Append(NS_ConvertUTF8toUTF16(escapedUTF8));
       }
       else {
         ReplaceFileURLs(prefSigText);
         sigData.Append(prefSigText);
       }
     }
   }
 
--- a/mailnews/compose/src/nsMsgComposeService.cpp
+++ b/mailnews/compose/src/nsMsgComposeService.cpp
@@ -514,22 +514,19 @@ NS_IMETHODIMP nsMsgComposeService::GetPa
       // empty, but we are composing in HTML because of the user's prefs, the
       // 'body' param needs to be escaped, since it's supposed to be plain
       // text, but it then doesn't need to sanitized.
       nsString rawBody;
       if (HTMLBodyPart.IsEmpty())
       {
         if (composeHTMLFormat)
         {
-          char *escaped = MsgEscapeHTML(bodyPart.get());
-          if (!escaped)
-            return NS_ERROR_OUT_OF_MEMORY;
-
-          CopyUTF8toUTF16(nsDependentCString(escaped), sanitizedBody);
-          free(escaped);
+          nsCString escaped;
+          nsAppendEscapedHTML(bodyPart, escaped);
+          CopyUTF8toUTF16(escaped, sanitizedBody);
         }
         else
           CopyUTF8toUTF16(bodyPart, rawBody);
       }
       else
         CopyUTF8toUTF16(HTMLBodyPart, rawBody);
 
       if (!rawBody.IsEmpty() && composeHTMLFormat)
--- a/mailnews/mime/emitters/nsMimeBaseEmitter.cpp
+++ b/mailnews/mime/emitters/nsMimeBaseEmitter.cpp
@@ -770,17 +770,17 @@ nsMimeBaseEmitter::GetLocalizedDateStrin
     i18nValue = strdup(dateString);
 
   return i18nValue;
 }
 
 nsresult
 nsMimeBaseEmitter::WriteHeaderFieldHTML(const char *field, const char *value)
 {
-  char *newValue = nullptr;
+  nsCString newValue;
   char *i18nValue = nullptr;
 
   if ( (!field) || (!value) )
     return NS_OK;
 
   //
   // This is a check to see what the pref is for header display. If
   // We should only output stuff that corresponds with that setting.
@@ -800,28 +800,28 @@ nsMimeBaseEmitter::WriteHeaderFieldHTML(
   if ( (mUnicodeConverter) && (mFormat != nsMimeOutput::nsMimeMessageSaveAs) )
   {
     nsCString tValue;
 
     // we're going to need a converter to convert
     nsresult rv = mUnicodeConverter->DecodeMimeHeaderToUTF8(
       nsDependentCString(i18nValue), nullptr, false, true, tValue);
     if (NS_SUCCEEDED(rv) && !tValue.IsEmpty())
-      newValue = MsgEscapeHTML(tValue.get());
+      nsAppendEscapedHTML(tValue, newValue);
     else
-      newValue = MsgEscapeHTML(i18nValue);
+      nsAppendEscapedHTML(nsDependentCString(i18nValue), newValue);
   }
   else
   {
-    newValue = MsgEscapeHTML(i18nValue);
+    nsAppendEscapedHTML(nsDependentCString(i18nValue), newValue);
   }
 
   free(i18nValue);
 
-  if (!newValue)
+  if (newValue.IsEmpty())
     return NS_OK;
 
   mHTMLHeaders.Append("<tr>");
   mHTMLHeaders.Append("<td>");
 
   if (mFormat == nsMimeOutput::nsMimeMessageSaveAs)
     mHTMLHeaders.Append("<b>");
   else
@@ -852,17 +852,16 @@ nsMimeBaseEmitter::WriteHeaderFieldHTML(
 
   // Now write out the actual value itself and move on!
   //
   mHTMLHeaders.Append(newValue);
   mHTMLHeaders.Append("</td>");
 
   mHTMLHeaders.Append("</tr>");
 
-  PR_FREEIF(newValue);
   return NS_OK;
 }
 
 nsresult
 nsMimeBaseEmitter::WriteHeaderFieldHTMLPrefix(const nsACString &name)
 {
   if (
       ( (mFormat == nsMimeOutput::nsMimeMessageSaveAs) && (mFirstHeaders) ) ||
@@ -870,17 +869,17 @@ nsMimeBaseEmitter::WriteHeaderFieldHTMLP
      )
      /* DO NOTHING */ ;   // rhp: Do nothing...leaving the conditional like this so its
                           //      easier to see the logic of what is going on.
   else {
     mHTMLHeaders.Append("<br><fieldset class=\"mimeAttachmentHeader\">");
     if (!name.IsEmpty()) {
       mHTMLHeaders.Append("<legend class=\"mimeAttachmentHeaderName\">");
       nsCString escapedName;
-      escapedName.Adopt(MsgEscapeHTML(nsCString(name).get()));
+      nsAppendEscapedHTML(name, escapedName);
       mHTMLHeaders.Append(escapedName);
       mHTMLHeaders.Append("</legend>");
     }
     mHTMLHeaders.Append("</fieldset>");
   }
 
   mFirstHeaders = false;
   return NS_OK;
--- a/mailnews/mime/emitters/nsMimeHtmlEmitter.cpp
+++ b/mailnews/mime/emitters/nsMimeHtmlEmitter.cpp
@@ -306,28 +306,20 @@ nsMimeHtmlDisplayEmitter::EndHeader(cons
   if (mDocHeader && (mFormat != nsMimeOutput::nsMimeMessageFilterSniffer))
   {
     UtilityWriteCRLF("<html>");
     UtilityWriteCRLF("<head>");
 
     const char * val = GetHeaderValue(HEADER_SUBJECT); // do not free this value
     if (val)
     {
-      char * subject = MsgEscapeHTML(val);
-      if (subject)
-      {
-        int32_t bufLen = strlen(subject) + 16;
-        char *buf = new char[bufLen];
-        if (!buf)
-          return NS_ERROR_OUT_OF_MEMORY;
-        PR_snprintf(buf, bufLen, "<title>%s</title>", subject);
-        UtilityWriteCRLF(buf);
-        delete [] buf;
-        free(subject);
-      }
+      nsCString subject("<title>");
+      nsAppendEscapedHTML(nsDependentCString(val), subject);
+      subject.AppendLiteral("</title>");
+      UtilityWriteCRLF(subject.get());
     }
 
     // Stylesheet info!
     UtilityWriteCRLF("<link rel=\"important stylesheet\" href=\"chrome://messagebody/skin/messageBody.css\">");
 
     UtilityWriteCRLF("</head>");
     UtilityWriteCRLF("<body>");
   }
@@ -437,17 +429,17 @@ nsMimeHtmlDisplayEmitter::StartAttachmen
                                    getter_AddRefs(bundle));
       NS_ENSURE_SUCCESS(rv, rv);
 
       nsString attachmentsHeader;
       bundle->GetStringFromName("attachmentsPrintHeader", attachmentsHeader);
 
       UtilityWrite("<legend class=\"mimeAttachmentHeaderName\">");
       nsCString escapedName;
-      escapedName.Adopt(MsgEscapeHTML(NS_ConvertUTF16toUTF8(attachmentsHeader).get()));
+      nsAppendEscapedHTML(NS_ConvertUTF16toUTF8(attachmentsHeader), escapedName);
       UtilityWrite(escapedName.get());
       UtilityWrite("</legend>");
     }
     UtilityWrite("</fieldset>");
     UtilityWrite("<div class=\"mimeAttachmentWrap\">");
     UtilityWrite("<table class=\"mimeAttachmentTable\">");
   }
 
--- a/mailnews/mime/emitters/nsMimeXmlEmitter.cpp
+++ b/mailnews/mime/emitters/nsMimeXmlEmitter.cpp
@@ -49,43 +49,40 @@ nsMimeXmlEmitter::Complete()
 }
 
 nsresult
 nsMimeXmlEmitter::WriteXMLHeader(const char *msgID)
 {
   if ( (!msgID) || (!*msgID) )
     msgID = "none";
 
-  char  *newValue = MsgEscapeHTML(msgID);
-  if (!newValue)
-    return NS_ERROR_OUT_OF_MEMORY;
+  nsCString newValue;
+  nsAppendEscapedHTML(nsDependentCString(msgID), newValue);
 
   UtilityWrite("<?xml version=\"1.0\"?>");
 
   UtilityWriteCRLF("<?xml-stylesheet href=\"chrome://messagebody/skin/messageBody.css\" type=\"text/css\"?>");
 
   UtilityWrite("<message id=\"");
-  UtilityWrite(newValue);
+  UtilityWrite(newValue.get());
   UtilityWrite("\">");
 
   mXMLHeaderStarted = true;
-  PR_FREEIF(newValue);
   return NS_OK;
 }
 
 nsresult
 nsMimeXmlEmitter::WriteXMLTag(const char *tagName, const char *value)
 {
   if ( (!value) || (!*value) )
     return NS_OK;
 
   char  *upCaseTag = NULL;
-  char  *newValue = MsgEscapeHTML(value);
-  if (!newValue)
-    return NS_OK;
+  nsCString newValue;
+  nsAppendEscapedHTML(nsDependentCString(value), newValue);
 
   nsCString newTagName(tagName);
   newTagName.StripWhitespace();
   ToUpperCase(newTagName);
   upCaseTag = ToNewCString(newTagName);
 
   UtilityWrite("<header field=\"");
   UtilityWrite(upCaseTag);
@@ -105,21 +102,20 @@ nsMimeXmlEmitter::WriteXMLTag(const char
   }
   PR_FREEIF(l10nTagName);
 
   UtilityWrite(": ");
   UtilityWrite("</headerdisplayname>");
 
   // Now write out the actual value itself and move on!
   //
-  UtilityWrite(newValue);
+  UtilityWrite(newValue.get());
   UtilityWrite("</header>");
 
   NS_Free(upCaseTag);
-  PR_FREEIF(newValue);
 
   return NS_OK;
 }
 
 // Header handling routines.
 nsresult
 nsMimeXmlEmitter::StartHeader(bool rootMailHeader, bool headerOnly, const char *msgID,
                            const char *outCharset)
--- a/mailnews/mime/src/mimedrft.cpp
+++ b/mailnews/mime/src/mimedrft.cpp
@@ -573,21 +573,23 @@ mime_intl_insert_message_header_1(char  
   }
   else
     NS_MsgSACat(body, ": ");
 
   // MIME decode header
   nsAutoCString utf8Value;
   MIME_DecodeMimeHeader(hdr_value, mailcharset, false, true, utf8Value);
   if (!utf8Value.IsEmpty()) {
-      char *escaped = nullptr;
-      if (htmlEdit)
-          escaped = MsgEscapeHTML(utf8Value.get());
-      NS_MsgSACat(body, escaped ? escaped : utf8Value.get());
-      NS_Free(escaped);
+      if (htmlEdit) {
+        nsCString escaped;
+        nsAppendEscapedHTML(utf8Value, escaped);
+        NS_MsgSACat(body, escaped.get());
+      } else {
+        NS_MsgSACat(body, utf8Value.get());
+      }
   } else {
       NS_MsgSACat(body, hdr_value); // raw MIME encoded string
   }
 
   if (htmlEdit)
       NS_MsgSACat(body, HEADER_END_JUNK);
 }
 
@@ -1086,21 +1088,22 @@ mime_insert_forwarded_message_headers(ch
     break;
   }
 }
 
 static void
 convert_plaintext_body_to_html(char **body, uint32_t bodyLen)
 {
   // We need to convert the plain/text to HTML in order to escape any HTML markup
-  char *escapedBody = MsgEscapeHTML(*body);
-  if (escapedBody)
+  nsCString escapedBody;
+  nsAppendEscapedHTML(nsDependentCString(*body), escapedBody);
+  if (!escapedBody.IsEmpty())
   {
     PR_Free(*body);
-    *body = escapedBody;
+    *body = strdup(escapedBody.get());
     bodyLen = strlen(*body);
   }
 
    // +13 chars for <pre> & </pre> tags and CRLF
   uint32_t newbodylen = bodyLen + 14;
   char* newbody = (char *)PR_MALLOC (newbodylen);
   if (newbody)
   {
--- a/mailnews/mime/src/mimeebod.cpp
+++ b/mailnews/mime/src/mimeebod.cpp
@@ -375,43 +375,41 @@ MimeExternalBody_parse_eof (MimeObject *
     all_headers_p = true;  /* #### just do this all the time? */
 
     if (bod->body && all_headers_p)
     {
       char *s = bod->body;
       while (IS_SPACE(*s)) s++;
       if (*s)
       {
-        char *s2;
         const char *pre = "<P><PRE>";
         const char *suf = "</PRE>";
         int32_t i;
         for(i = strlen(s)-1; i >= 0 && IS_SPACE(s[i]); i--)
           s[i] = 0;
-        s2 = MsgEscapeHTML(s);
-        if (!s2) goto FAIL;
-        body = (char *) PR_MALLOC(strlen(pre) + strlen(s2) +
+        nsCString s2;
+        nsAppendEscapedHTML(nsDependentCString(s), s2);
+        body = (char *) PR_MALLOC(strlen(pre) + s2.Length() +
                                   strlen(suf) + 1);
         if (!body)
         {
-          NS_Free(s2);
           goto FAIL;
         }
         PL_strcpy(body, pre);
-        PL_strcat(body, s2);
+        PL_strcat(body, s2.get());
         PL_strcat(body, suf);
       }
     }
 
     newopt->fancy_headers_p = true;
     newopt->headers = (all_headers_p ? MimeHeadersAll : MimeHeadersSome);
 
 FAIL:
-      if (hdrs)
-        MimeHeaders_free(hdrs);
+    if (hdrs)
+      MimeHeaders_free(hdrs);
     PR_FREEIF(h);
     PR_FREEIF(lname);
     PR_FREEIF(lurl);
     PR_FREEIF(body);
     PR_FREEIF(ct);
     PR_FREEIF(at);
     PR_FREEIF(lexp);
     PR_FREEIF(size);
--- a/mailnews/mime/src/mimei.cpp
+++ b/mailnews/mime/src/mimei.cpp
@@ -1710,17 +1710,17 @@ MimeOptions_write(MimeHeaders *hdrs, Mim
 
       if (!name.IsEmpty()) {
           sep = "<LEGEND CLASS=\"mimeAttachmentHeaderName\">";
           lstatus = opt->output_fn(sep, strlen(sep), closure);
           opt->state->separator_suppressed_p = false;
           if (lstatus < 0) return lstatus;
 
           nsCString escapedName;
-          escapedName.Adopt(MsgEscapeHTML(name.get()));
+          nsAppendEscapedHTML(name, escapedName);
 
           lstatus = opt->output_fn(escapedName.get(),
                                    escapedName.Length(), closure);
           opt->state->separator_suppressed_p = false;
           if (lstatus < 0) return lstatus;
 
           sep = "</LEGEND>";
           lstatus = opt->output_fn(sep, strlen(sep), closure);