Bug 791428 - Remove unnecessary malloc from number format function. r=roc
authorAnthony Jones <ajones@mozilla.com>
Mon, 22 Oct 2012 22:22:27 -0400
changeset 111241 9e1b8da160a290d32432560eb7c551aaf5c5ec5a
parent 111240 d587ce3af2f357775ac939e346fc1de374a264a3
child 111242 ab22c6590cfdc2099d98f7355e5cc20d62b44d84
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersroc
bugs791428
milestone19.0a1
Bug 791428 - Remove unnecessary malloc from number format function. r=roc
xpcom/string/src/nsTSubstring.cpp
--- a/xpcom/string/src/nsTSubstring.cpp
+++ b/xpcom/string/src/nsTSubstring.cpp
@@ -786,63 +786,58 @@ void nsTSubstring_CharT::AppendPrintf( c
 #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?!?
  */
 static void 
-Modified_cnvtf(char *buf, int bufsz, int prcsn, double fval)
+Modified_cnvtf(char (& buf)[40], int prcsn, double fval)
 {
   int decpt, sign, numdigits;
-  char *num, *nump;
+  char num[40];
+  char *nump;
   char *bufp = buf;
   char *endnum;
 
-  /* If anything fails, we store an empty string in 'buf' */
-  num = (char*)malloc(bufsz);
-  if (num == NULL) {
+  if (PR_dtoa(fval, 2, prcsn, &decpt, &sign, &endnum, num, sizeof(num))
+      == PR_FAILURE) {
     buf[0] = '\0';
     return;
   }
-  if (PR_dtoa(fval, 2, prcsn, &decpt, &sign, &endnum, num, bufsz)
-      == PR_FAILURE) {
-    buf[0] = '\0';
-    goto done;
-  }
   numdigits = endnum - num;
   nump = num;
 
   /*
    * The NSPR code had a fancy way of checking that we weren't dealing
    * with -0.0 or -NaN, but I'll just use < instead.
    * XXX Should we check !isnan(fval) as well?  Is it portable?  We
    * probably don't need to bother since NAN isn't portable.
    */
   if (sign && fval < 0.0f) {
     *bufp++ = '-';
   }
 
   if (decpt == 9999) {
     while ((*bufp++ = *nump++) != 0) {} /* nothing to execute */
-    goto done;
+    return;
   }
 
   if (decpt > (prcsn+1) || decpt < -(prcsn-1) || decpt < -5) {
     *bufp++ = *nump++;
     if (numdigits != 1) {
       *bufp++ = '.';
     }
 
     while (*nump != '\0') {
       *bufp++ = *nump++;
     }
     *bufp++ = 'e';
-    PR_snprintf(bufp, bufsz - (bufp - buf), "%+d", decpt-1);
+    PR_snprintf(bufp, sizeof(num) - (bufp - buf), "%+d", decpt-1);
   }
   else if (decpt >= 0) {
     if (decpt == 0) {
       *bufp++ = '0';
     }
     else {
       while (decpt--) {
         if (*nump != '\0') {
@@ -868,28 +863,26 @@ Modified_cnvtf(char *buf, int bufsz, int
       *bufp++ = '0';
     }
 
     while (*nump != '\0') {
       *bufp++ = *nump++;
     }
     *bufp++ = '\0';
   }
-done:
-  free(num);
 }
 #endif /* CharT_is_PRUnichar */
 
 void
 nsTSubstring_CharT::DoAppendFloat( double aFloat, int digits )
 {
   char buf[40];
   // Use Modified_cnvtf, which is locale-insensitive, instead of the
   // locale-sensitive PR_snprintf or sprintf(3)
-  Modified_cnvtf(buf, sizeof(buf), digits, aFloat);
+  Modified_cnvtf(buf, digits, aFloat);
   AppendASCII(buf);
 }
 
 size_t
 nsTSubstring_CharT::SizeOfExcludingThisMustBeUnshared(
     nsMallocSizeOfFun mallocSizeOf) const
 {
   if (mFlags & F_SHARED) {