Bug 1341017 - Fix XPCOM logging to work correctly with vsnprintf results. r=froydnj
authorHonza Bambas <honzab.moz@firemni.cz>
Wed, 01 Mar 2017 05:02:00 -0500
changeset 374357 1bea491fae965da7d65cf345f67ca5fb870d4356
parent 374356 acf42887c8a7cf23ff677d49742cd6610545a38b
child 374358 a6db4cae99904cecfc03101a7007a147aca6b1fc
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1341017
milestone54.0a1
Bug 1341017 - Fix XPCOM logging to work correctly with vsnprintf results. r=froydnj
xpcom/base/Logging.cpp
--- a/xpcom/base/Logging.cpp
+++ b/xpcom/base/Logging.cpp
@@ -356,24 +356,30 @@ public:
 
   void Print(const char* aName, LogLevel aLevel, const char* aFmt, va_list aArgs)
   {
     const size_t kBuffSize = 1024;
     char buff[kBuffSize];
 
     char* buffToWrite = buff;
 
-    // For backwards compat we need to use the NSPR format string versions
-    // of sprintf and friends and then hand off to printf.
     va_list argsCopy;
     va_copy(argsCopy, aArgs);
-    size_t charsWritten = VsprintfLiteral(buff, aFmt, argsCopy);
+    int charsWritten = VsprintfLiteral(buff, aFmt, argsCopy);
     va_end(argsCopy);
 
-    if (charsWritten == kBuffSize - 1) {
+    if (charsWritten < 0) {
+      // Print out at least something.  We must copy to the local buff,
+      // can't just assign aFmt to buffToWrite, since when
+      // buffToWrite != buff, we try to release it.
+      MOZ_ASSERT(false, "Probably incorrect format string in LOG?");
+      strncpy(buff, aFmt, kBuffSize - 1);
+      buff[kBuffSize - 1] = '\0';
+      charsWritten = strlen(buff);
+    } else if (static_cast<size_t>(charsWritten) >= kBuffSize - 1) {
       // We may have maxed out, allocate a buffer instead.
       buffToWrite = mozilla::Vsmprintf(aFmt, aArgs);
       charsWritten = strlen(buffToWrite);
     }
 
     // Determine if a newline needs to be appended to the message.
     const char* newline = "";
     if (charsWritten == 0 || buffToWrite[charsWritten - 1] != '\n') {