Bug 1341880 - do not emit trailing \0 in PrintfState; r?glandium draft
authorTom Tromey <tom@tromey.com>
Mon, 27 Feb 2017 13:29:54 -0700
changeset 491941 9b9431059b412fb1068843eb7fe26346fb21950a
parent 491940 2492756574ea770ad1759d69698747213352e92c
child 491946 b91745281a538a98a687742163e9abcca3c6929d
push id47464
push userbmo:ttromey@mozilla.com
push dateThu, 02 Mar 2017 14:23:17 +0000
reviewersglandium
bugs1341880
milestone54.0a1
Bug 1341880 - do not emit trailing \0 in PrintfState; r?glandium 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,36 @@ 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) {
+        // The "" here has a single \0 character, which is what we're
+        // trying to append.
+        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;
 };