Bug 1341880 - do not emit trailing \0 in PrintfState; r?froydnj draft
authorTom Tromey <tom@tromey.com>
Mon, 27 Feb 2017 13:29:54 -0700
changeset 490227 4b2dd7e01a660d27b6b1c9c65ef01fea066f59fc
parent 490226 370364bfc2b5a266e67b63fab5eb9f800220601b
child 547198 27631992473083475c199ef0b703e0de645b14f4
push id47030
push userbmo:ttromey@mozilla.com
push dateMon, 27 Feb 2017 20:32:32 +0000
reviewersfroydnj
bugs1341880
milestone54.0a1
Bug 1341880 - do not emit trailing \0 in PrintfState; r?froydnj MozReview-Commit-ID: 2di7CsDCWLF
mozglue/misc/Printf.cpp
mozglue/misc/Printf.h
xpcom/base/nsDebugImpl.cpp
xpcom/string/nsTSubstring.cpp
--- a/mozglue/misc/Printf.cpp
+++ b/mozglue/misc/Printf.cpp
@@ -875,20 +875,16 @@ mozilla::PrintfTarget::vprint(const char
 #endif
             if (!emit("%", 1))
                 return false;
             if (!emit(fmt - 1, 1))
                 return false;
         }
     }
 
-    // Stuff trailing NUL
-    if (!emit("\0", 1))
-        return false;
-
     return true;
 }
 
 /************************************************************************/
 
 bool
 mozilla::PrintfTarget::print(const char* format, ...)
 {
--- a/mozglue/misc/Printf.h
+++ b/mozglue/misc/Printf.h
@@ -100,30 +100,34 @@ private:
     bool cvt_l(long num, int width, int prec, int radix, int type, int flags, const char* hxp);
     bool cvt_ll(int64_t num, int width, int prec, int radix, int type, int flags, const char* hexp);
     bool cvt_f(double d, const char* fmt0, const char* fmt1);
     bool cvt_s(const char* s, int width, int prec, int flags);
 };
 
 // Used in the implementation of Smprintf et al.
 template<typename AllocPolicy>
-class MOZ_STACK_CLASS SprintfState final : public mozilla::PrintfTarget, private AllocPolicy
+class MOZ_STACK_CLASS SprintfState final : private mozilla::PrintfTarget, private AllocPolicy
 {
  public:
     explicit SprintfState(char* base)
         : mMaxlen(base ? strlen(base) : 0)
         , mBase(base)
         , mCur(base ? base + mMaxlen : 0)
     {
     }
 
     ~SprintfState() {
         this->free_(mBase);
     }
 
+    bool vprint(const char* format, va_list ap_list) {
+        return mozilla::PrintfTarget::vprint(format, ap_list) && append("", 1);
+    }
+
     char* release() {
         char* result = mBase;
         mBase = nullptr;
         return result;
     }
 
  protected:
 
--- a/xpcom/base/nsDebugImpl.cpp
+++ b/xpcom/base/nsDebugImpl.cpp
@@ -260,17 +260,17 @@ GetAssertBehavior()
   if (!strcmp(assertString, "stack-and-abort")) {
     return gAssertBehavior = NS_ASSERT_STACK_AND_ABORT;
   }
 
   fprintf(stderr, "Unrecognized value of XPCOM_DEBUG_BREAK\n");
   return gAssertBehavior;
 }
 
-struct FixedBuffer : public mozilla::PrintfTarget
+struct FixedBuffer final : public mozilla::PrintfTarget
 {
   FixedBuffer() : curlen(0)
   {
     buffer[0] = '\0';
   }
 
   char buffer[500];
   uint32_t curlen;
@@ -280,21 +280,16 @@ struct FixedBuffer : public mozilla::Pri
 
 bool
 FixedBuffer::append(const char* aBuf, size_t aLen)
 {
   if (!aLen) {
     return true;
   }
 
-  // strip the trailing null, we add it again later
-  if (aBuf[aLen - 1] == '\0') {
-    --aLen;
-  }
-
   if (curlen + aLen >= sizeof(buffer)) {
     aLen = sizeof(buffer) - curlen - 1;
   }
 
   if (aLen) {
     memcpy(buffer + curlen, aBuf, aLen);
     curlen += aLen;
     buffer[curlen] = '\0';
--- a/xpcom/string/nsTSubstring.cpp
+++ b/xpcom/string/nsTSubstring.cpp
@@ -926,21 +926,16 @@ struct MOZ_STACK_CLASS PrintfAppend_Char
   {
   }
 
   bool append(const char* aStr, size_t aLen) override {
     if (aLen == 0) {
       return true;
     }
 
-    // Printf sends us the final null terminator even though we don't want it
-    if (aStr[aLen - 1] == '\0') {
-      --aLen;
-    }
-
     mString->AppendASCII(aStr, aLen);
     return true;
   }
 
 private:
 
   nsTSubstring_CharT* mString;
 };