Bug 592009. Fix AppendPrintf to support more than 31 characters. r=bsmedberg
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Thu, 25 Aug 2011 10:43:49 -0400
changeset 75892 548e34a4a9c2dc58f0d15dfb0450162723593609
parent 75891 2146a29d0bcccc25a360b919528378ed94ec9d5b
child 75893 625a18d70e9ee4943e2c53bc63c0058ab2177943
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersbsmedberg
bugs592009
milestone9.0a1
Bug 592009. Fix AppendPrintf to support more than 31 characters. r=bsmedberg We can use vsmprintf to have the formatting code do the allocation for us.
xpcom/string/public/nsTSubstring.h
xpcom/string/src/nsTSubstring.cpp
--- a/xpcom/string/public/nsTSubstring.h
+++ b/xpcom/string/public/nsTSubstring.h
@@ -382,56 +382,57 @@ class nsTSubstring_CharT
 
       void Append( char_type c )                                                                 { Replace(mLength, 0, c); }
       void Append( const char_type* data, size_type length = size_type(-1) )                     { Replace(mLength, 0, data, length); }
       void Append( const self_type& str )                                                        { Replace(mLength, 0, str); }
       void Append( const substring_tuple_type& tuple )                                           { Replace(mLength, 0, tuple); }
 
       void AppendASCII( const char* data, size_type length = size_type(-1) )                     { ReplaceASCII(mLength, 0, data, length); }
 
-    // AppendPrintf truncates output to 31 ASCII characters
       void AppendPrintf( const char* format, ... );
       void AppendInt( PRInt32 aInteger )
-                 { AppendPrintf( "%d", aInteger ); }
+                 { AppendPrintf31( "%d", aInteger ); }
       void AppendInt( PRInt32 aInteger, int aRadix )
         {
           const char *fmt = aRadix == 10 ? "%d" : aRadix == 8 ? "%o" : "%x";
-          AppendPrintf( fmt, aInteger );
+          AppendPrintf31( fmt, aInteger );
         }
       void AppendInt( PRUint32 aInteger )
-                 { AppendPrintf( "%u", aInteger ); }
+                 { AppendPrintf31( "%u", aInteger ); }
       void AppendInt( PRUint32 aInteger, int aRadix )
         {
           const char *fmt = aRadix == 10 ? "%u" : aRadix == 8 ? "%o" : "%x";
-          AppendPrintf( fmt, aInteger );
+          AppendPrintf31( fmt, aInteger );
         }
       void AppendInt( PRInt64 aInteger )
-                 { AppendPrintf( "%lld", aInteger ); }
+                 { AppendPrintf31( "%lld", aInteger ); }
       void AppendInt( PRInt64 aInteger, int aRadix )
         {
           const char *fmt = aRadix == 10 ? "%lld" : aRadix == 8 ? "%llo" : "%llx";
-          AppendPrintf( fmt, aInteger );
+          AppendPrintf31( fmt, aInteger );
         }
       void AppendInt( PRUint64 aInteger )
-                 { AppendPrintf( "%llu", aInteger ); }
+                 { AppendPrintf31( "%llu", aInteger ); }
       void AppendInt( PRUint64 aInteger, int aRadix )
         {
           const char *fmt = aRadix == 10 ? "%llu" : aRadix == 8 ? "%llo" : "%llx";
-          AppendPrintf( fmt, aInteger );
+          AppendPrintf31( fmt, aInteger );
         }
 
       /**
        * Append the given float to this string 
        */
       void AppendFloat( float aFloat )
                       { DoAppendFloat(aFloat, 6); }
       void AppendFloat( double aFloat )
                       { DoAppendFloat(aFloat, 15); }
   private:
       void NS_FASTCALL DoAppendFloat( double aFloat, int digits );
+      // AppendPrintf31 truncates output to 31 ASCII characters
+      void AppendPrintf31( const char* format, ... );
   public:
 
     // AppendLiteral must ONLY be applied to an actual literal string.
     // Do not attempt to use it with a regular char* pointer, or with a char
     // array variable. Use AppendASCII for those.
 #ifdef NS_DISABLE_LITERAL_TEMPLATE
       void AppendLiteral( const char* str )
                   { AppendASCII(str); }
--- a/xpcom/string/src/nsTSubstring.cpp
+++ b/xpcom/string/src/nsTSubstring.cpp
@@ -730,26 +730,37 @@ nsTSubstring_CharT::StripChars( const ch
           // Not stripped, copy this char.
           *to++ = theChar;
         }
       }
     *to = char_type(0); // add the null
     mLength = to - mData;
   }
 
-void nsTSubstring_CharT::AppendPrintf( const char* format, ...)
+void nsTSubstring_CharT::AppendPrintf31( const char* format, ...)
   {
     char buf[32];
     va_list ap;
     va_start(ap, format);
     PRUint32 len = PR_vsnprintf(buf, sizeof(buf), format, ap);
     AppendASCII(buf, len);
     va_end(ap);
   }
 
+void nsTSubstring_CharT::AppendPrintf( const char* format, ...)
+  {
+    char *buf;
+    va_list ap;
+    va_start(ap, format);
+    buf = PR_vsmprintf(format, ap);
+    AppendASCII(buf);
+    PR_smprintf_free(buf);
+    va_end(ap);
+  }
+
 
 /* hack to make sure we define Modified_cnvtf only once */
 #ifdef CharT_is_PRUnichar
 /**
  * This is a copy of |PR_cnvtf| with a bug fixed.  (The second argument
  * of PR_dtoa is 2 rather than 1.)
  *
  * XXX(darin): if this is the right thing, then why wasn't it fixed in NSPR?!?