Bug 1070689 - Change return type of dosprintf functions from int to bool (in jsprf.cpp). r=nbp
authorGokcehan Kara <gokcehankara@gmail.com>
Wed, 01 Oct 2014 20:59:15 +0200
changeset 208242 7f08e93cc2a5f66ea8b0ab0ef950a8f6d41c8c84
parent 208241 acf16a586d9f62c01ef1a6eb1b6e34461b135cdd
child 208243 b5cf9eedcfbd061a3a1e9cd82c25ce0937405c14
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersnbp
bugs1070689
milestone35.0a1
Bug 1070689 - Change return type of dosprintf functions from int to bool (in jsprf.cpp). r=nbp
js/src/jsprf.cpp
--- a/js/src/jsprf.cpp
+++ b/js/src/jsprf.cpp
@@ -35,17 +35,17 @@ using namespace js;
 #elif defined(HAVE_VA_LIST_AS_ARRAY)
 #define VARARGS_ASSIGN(foo, bar)        foo[0] = bar[0]
 #else
 #define VARARGS_ASSIGN(foo, bar)        (foo) = (bar)
 #endif
 
 struct SprintfState
 {
-    int (*stuff)(SprintfState *ss, const char *sp, size_t len);
+    bool (*stuff)(SprintfState *ss, const char *sp, size_t len);
 
     char *base;
     char *cur;
     size_t maxlen;
 
     int (*func)(void *arg, const char *sp, uint32_t len);
     void *arg;
 };
@@ -77,92 +77,85 @@ typedef mozilla::Vector<NumArgState, 20,
 #define TYPE_UNKNOWN    20
 
 #define FLAG_LEFT       0x1
 #define FLAG_SIGNED     0x2
 #define FLAG_SPACED     0x4
 #define FLAG_ZEROS      0x8
 #define FLAG_NEG        0x10
 
-inline int
+inline bool
 generic_write(SprintfState *ss, const char *src, size_t srclen)
 {
     return (*ss->stuff)(ss, src, srclen);
 }
 
-inline int
+inline bool
 generic_write(SprintfState *ss, const char16_t *src, size_t srclen)
 {
     const size_t CHUNK_SIZE = 64;
     char chunk[CHUNK_SIZE];
 
-    int rv = 0;
     size_t j = 0;
     size_t i = 0;
     while (i < srclen) {
         // FIXME: truncates characters to 8 bits
         chunk[j++] = char(src[i++]);
 
         if (j == CHUNK_SIZE || i == srclen) {
-            rv = (*ss->stuff)(ss, chunk, j);
-            if (rv != 0)
-                return rv;
+            if (!(*ss->stuff)(ss, chunk, j))
+                return false;
             j = 0;
         }
     }
-    return 0;
+    return true;
 }
 
 // Fill into the buffer using the data in src
 template <typename Char>
-static int
+static bool
 fill2(SprintfState *ss, const Char *src, int srclen, int width, int flags)
 {
     char space = ' ';
-    int rv;
 
     width -= srclen;
     if (width > 0 && (flags & FLAG_LEFT) == 0) {    // Right adjusting
         if (flags & FLAG_ZEROS)
             space = '0';
         while (--width >= 0) {
-            rv = (*ss->stuff)(ss, &space, 1);
-            if (rv < 0)
-                return rv;
+            if (!(*ss->stuff)(ss, &space, 1))
+                return false;
         }
     }
 
     // Copy out the source data
-    rv = generic_write(ss, src, srclen);
-    if (rv < 0)
-        return rv;
+    if (!generic_write(ss, src, srclen))
+        return false;
 
     if (width > 0 && (flags & FLAG_LEFT) != 0) {    // Left adjusting
         while (--width >= 0) {
-            rv = (*ss->stuff)(ss, &space, 1);
-            if (rv < 0)
-                return rv;
+            if (!(*ss->stuff)(ss, &space, 1))
+                return false;
         }
     }
-    return 0;
+    return true;
 }
 
 /*
  * Fill a number. The order is: optional-sign zero-filling conversion-digits
  */
-static int
+static bool
 fill_n(SprintfState *ss, const char *src, int srclen, int width, int prec, int type, int flags)
 {
     int zerowidth = 0;
     int precwidth = 0;
     int signwidth = 0;
     int leftspaces = 0;
     int rightspaces = 0;
     int cvtwidth;
-    int rv;
     char sign;
 
     if ((type & 1) == 0) {
         if (flags & FLAG_NEG) {
             sign = '-';
             signwidth = 1;
         } else if (flags & FLAG_SIGNED) {
             sign = '+';
@@ -195,64 +188,51 @@ fill_n(SprintfState *ss, const char *src
         }
     } else {
         if (width > cvtwidth) {
             // Space filling on the left (i.e. right adjusting)
             leftspaces = width - cvtwidth;
         }
     }
     while (--leftspaces >= 0) {
-        rv = (*ss->stuff)(ss, " ", 1);
-        if (rv < 0) {
-            return rv;
-        }
+        if (!(*ss->stuff)(ss, " ", 1))
+            return false;
     }
     if (signwidth) {
-        rv = (*ss->stuff)(ss, &sign, 1);
-        if (rv < 0) {
-            return rv;
-        }
+        if (!(*ss->stuff)(ss, &sign, 1))
+            return false;
     }
     while (--precwidth >= 0) {
-        rv = (*ss->stuff)(ss, "0", 1);
-        if (rv < 0) {
-            return rv;
-        }
+        if (!(*ss->stuff)(ss, "0", 1))
+            return false;
     }
     while (--zerowidth >= 0) {
-        rv = (*ss->stuff)(ss, "0", 1);
-        if (rv < 0) {
-            return rv;
-        }
+        if (!(*ss->stuff)(ss, "0", 1))
+            return false;
     }
-    rv = (*ss->stuff)(ss, src, uint32_t(srclen));
-    if (rv < 0) {
-        return rv;
-    }
+    if (!(*ss->stuff)(ss, src, uint32_t(srclen)))
+        return false;
     while (--rightspaces >= 0) {
-        rv = (*ss->stuff)(ss, " ", 1);
-        if (rv < 0) {
-            return rv;
-        }
+        if (!(*ss->stuff)(ss, " ", 1))
+            return false;
     }
-    return 0;
+    return true;
 }
 
 /* Convert a long into its printable form. */
-static int cvt_l(SprintfState *ss, long num, int width, int prec, int radix,
-                 int type, int flags, const char *hexp)
+static bool cvt_l(SprintfState *ss, long num, int width, int prec, int radix,
+                  int type, int flags, const char *hexp)
 {
     char cvtbuf[100];
     char *cvt;
     int digits;
 
     // according to the man page this needs to happen
-    if ((prec == 0) && (num == 0)) {
-        return 0;
-    }
+    if ((prec == 0) && (num == 0))
+        return true;
 
     // Converting decimal is a little tricky. In the unsigned case we
     // need to stop when we hit 10 digits. In the signed case, we can
     // stop when the number is zero.
     cvt = cvtbuf + sizeof(cvtbuf);
     digits = 0;
     while (num) {
         int digit = (((unsigned long)num) % radix) & 0xF;
@@ -266,22 +246,22 @@ static int cvt_l(SprintfState *ss, long 
     }
 
     // Now that we have the number converted without its sign, deal with
     // the sign and zero padding.
     return fill_n(ss, cvt, digits, width, prec, type, flags);
 }
 
 /* Convert a 64-bit integer into its printable form. */
-static int cvt_ll(SprintfState *ss, int64_t num, int width, int prec, int radix,
-                  int type, int flags, const char *hexp)
+static bool cvt_ll(SprintfState *ss, int64_t num, int width, int prec, int radix,
+                   int type, int flags, const char *hexp)
 {
     // According to the man page, this needs to happen.
     if (prec == 0 && num == 0)
-        return 0;
+        return true;
 
     // Converting decimal is a little tricky. In the unsigned case we
     // need to stop when we hit 10 digits. In the signed case, we can
     // stop when the number is zero.
     int64_t rad = int64_t(radix);
     char cvtbuf[100];
     char *cvt = cvtbuf + sizeof(cvtbuf);
     int digits = 0;
@@ -304,26 +284,26 @@ static int cvt_ll(SprintfState *ss, int6
 }
 
 /*
  * Convert a double precision floating point number into its printable
  * form.
  *
  * XXX stop using sprintf to convert floating point
  */
-static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1)
+static bool cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1)
 {
     char fin[20];
     char fout[300];
     int amount = fmt1 - fmt0;
 
     MOZ_ASSERT((amount > 0) && (amount < (int)sizeof(fin)));
     if (amount >= (int)sizeof(fin)) {
         // Totally bogus % command to sprintf. Just ignore it
-        return 0;
+        return true;
     }
     js_memcpy(fin, fmt0, (size_t)amount);
     fin[amount] = 0;
 
     // Convert floating point using the native sprintf code
 #ifdef DEBUG
     {
         const char *p = fin;
@@ -350,21 +330,21 @@ static inline size_t generic_strlen(cons
 static inline size_t generic_strlen(const char16_t *s) { return js_strlen(s); }
 
 /*
  * Convert a string into its printable form.  "width" is the output
  * width. "prec" is the maximum number of characters of "s" to output,
  * where -1 means until NUL.
  */
 template <typename Char>
-static int
+static bool
 cvt_s(SprintfState *ss, const Char *s, int width, int prec, int flags)
 {
     if (prec == 0)
-        return 0;
+        return true;
     if (!s)
         s = generic_null_str(s);
 
     // Limit string length by precision value
     int slen = int(generic_strlen(s));
     if (0 < prec && prec < slen)
         slen = prec;
 
@@ -585,17 +565,17 @@ BuildArgArray(const char *fmt, va_list a
     }
 
     return true;
 }
 
 /*
  * The workhorse sprintf code.
  */
-static int
+static bool
 dosprintf(SprintfState *ss, const char *fmt, va_list ap)
 {
     char c;
     int flags, width, prec, radix, type;
     union {
         char ch;
         char16_t wch;
         int i;
@@ -605,63 +585,61 @@ dosprintf(SprintfState *ss, const char *
         const char *s;
         const char16_t* ws;
         int *ip;
     } u;
     const char *fmt0;
     static const char hex[] = "0123456789abcdef";
     static const char HEX[] = "0123456789ABCDEF";
     const char *hexp;
-    int rv, i;
+    int i;
     char pattern[20];
     const char *dolPt = nullptr;  // in "%4$.2f", dolPt will point to '.'
 
     // Build an argument array, IF the fmt is numbered argument
     // list style, to contain the Numbered Argument list pointers.
 
     NumArgStateVector nas;
     if (!BuildArgArray(fmt, ap, nas)) {
         // the fmt contains error Numbered Argument format, jliu@netscape.com
         MOZ_ASSERT(0);
-        return rv;
+        return false;
     }
 
     while ((c = *fmt++) != 0) {
         if (c != '%') {
-            rv = (*ss->stuff)(ss, fmt - 1, 1);
-            if (rv < 0) {
-                return rv;
-            }
+            if (!(*ss->stuff)(ss, fmt - 1, 1))
+                return false;
+
             continue;
         }
         fmt0 = fmt - 1;
 
         // Gobble up the % format string. Hopefully we have handled all
         // of the strange cases!
         flags = 0;
         c = *fmt++;
         if (c == '%') {
             // quoting a % with %%
-            rv = (*ss->stuff)(ss, fmt - 1, 1);
-            if (rv < 0) {
-                return rv;
-            }
+            if (!(*ss->stuff)(ss, fmt - 1, 1))
+                return false;
+
             continue;
         }
 
         if (!nas.empty()) {
             // the fmt contains the Numbered Arguments feature
             i = 0;
             while (c && c != '$') {         // should improve error check later
                 i = (i * 10) + (c - '0');
                 c = *fmt++;
             }
 
             if (nas[i - 1].type == TYPE_UNKNOWN)
-                return -1;
+                return false;
 
             ap = nas[i - 1].ap;
             dolPt = fmt;
             c = *fmt++;
         }
 
         // Examine optional flags.  Note that we do not implement the
         // '#' flag of sprintf().  The ANSI C spec. of the '#' flag is
@@ -780,85 +758,77 @@ dosprintf(SprintfState *ss, const char *
                 if (u.l < 0) {
                     u.l = -u.l;
                     flags |= FLAG_NEG;
                 }
                 goto do_long;
               case TYPE_UINT32:
                 u.l = (long)va_arg(ap, uint32_t);
               do_long:
-                rv = cvt_l(ss, u.l, width, prec, radix, type, flags, hexp);
-                if (rv < 0) {
-                    return rv;
-                }
+                if (!cvt_l(ss, u.l, width, prec, radix, type, flags, hexp))
+                    return false;
+
                 break;
 
               case TYPE_INT64:
                 u.ll = va_arg(ap, int64_t);
                 if (u.ll < 0) {
                     u.ll = -u.ll;
                     flags |= FLAG_NEG;
                 }
                 goto do_longlong;
               case TYPE_UINT64:
                 u.ll = va_arg(ap, uint64_t);
               do_longlong:
-                rv = cvt_ll(ss, u.ll, width, prec, radix, type, flags, hexp);
-                if (rv < 0) {
-                    return rv;
-                }
+                if (!cvt_ll(ss, u.ll, width, prec, radix, type, flags, hexp))
+                    return false;
+
                 break;
             }
             break;
 
           case 'e':
           case 'E':
           case 'f':
           case 'g':
             u.d = va_arg(ap, double);
             if (!nas.empty()) {
                 i = fmt - dolPt;
                 if (i < int(sizeof(pattern))) {
                     pattern[0] = '%';
                     js_memcpy(&pattern[1], dolPt, size_t(i));
-                    rv = cvt_f(ss, u.d, pattern, &pattern[i + 1]);
+                    if (!cvt_f(ss, u.d, pattern, &pattern[i + 1]))
+                        return false;
                 }
-            } else
-                rv = cvt_f(ss, u.d, fmt0, fmt);
+            } else {
+                if (!cvt_f(ss, u.d, fmt0, fmt))
+                    return false;
+            }
 
-            if (rv < 0) {
-                return rv;
-            }
             break;
 
           case 'c':
             if ((flags & FLAG_LEFT) == 0) {
                 while (width-- > 1) {
-                    rv = (*ss->stuff)(ss, " ", 1);
-                    if (rv < 0) {
-                        return rv;
-                    }
+                    if (!(*ss->stuff)(ss, " ", 1))
+                        return false;
                 }
             }
             switch (type) {
               case TYPE_INT16:
               case TYPE_INTN:
                 u.ch = va_arg(ap, int);
-                rv = (*ss->stuff)(ss, &u.ch, 1);
+                if (!(*ss->stuff)(ss, &u.ch, 1))
+                    return false;
                 break;
             }
-            if (rv < 0) {
-                return rv;
-            }
             if (flags & FLAG_LEFT) {
                 while (width-- > 1) {
-                    rv = (*ss->stuff)(ss, " ", 1);
-                    if (rv < 0) {
-                        return rv;
-                    }
+                    if (!(*ss->stuff)(ss, " ", 1))
+                        return false;
                 }
             }
             break;
 
           case 'p':
             if (sizeof(void *) == sizeof(int32_t)) {
                 type = TYPE_UINT32;
             } else if (sizeof(void *) == sizeof(int64_t)) {
@@ -880,89 +850,85 @@ dosprintf(SprintfState *ss, const char *
             // XXX not supported I suppose
             MOZ_ASSERT(0);
             break;
 #endif
 
           case 's':
             if(type == TYPE_INT16) {
                 u.ws = va_arg(ap, const char16_t*);
-                rv = cvt_s(ss, u.ws, width, prec, flags);
+                if (!cvt_s(ss, u.ws, width, prec, flags))
+                    return false;
             } else {
                 u.s = va_arg(ap, const char*);
-                rv = cvt_s(ss, u.s, width, prec, flags);
-            }
-            if (rv < 0) {
-                return rv;
+                if (!cvt_s(ss, u.s, width, prec, flags))
+                    return false;
             }
             break;
 
           case 'n':
             u.ip = va_arg(ap, int*);
             if (u.ip) {
                 *u.ip = ss->cur - ss->base;
             }
             break;
 
           default:
             // Not a % token after all... skip it
 #if 0
             MOZ_ASSERT(0);
 #endif
-            rv = (*ss->stuff)(ss, "%", 1);
-            if (rv < 0) {
-                return rv;
-            }
-            rv = (*ss->stuff)(ss, fmt - 1, 1);
-            if (rv < 0) {
-                return rv;
-            }
+            if (!(*ss->stuff)(ss, "%", 1))
+                return false;
+            if (!(*ss->stuff)(ss, fmt - 1, 1))
+                return false;
         }
     }
 
     // Stuff trailing NUL
-    rv = (*ss->stuff)(ss, "\0", 1);
+    if (!(*ss->stuff)(ss, "\0", 1))
+        return false;
 
-    return rv;
+    return true;
 }
 
 /************************************************************************/
 
 /*
  * Stuff routine that automatically grows the js_malloc'd output buffer
  * before it overflows.
  */
-static int
+static bool
 GrowStuff(SprintfState *ss, const char *sp, size_t len)
 {
     ptrdiff_t off;
     char *newbase;
     size_t newlen;
 
     off = ss->cur - ss->base;
     if (off + len >= ss->maxlen) {
         /* Grow the buffer */
         newlen = ss->maxlen + ((len > 32) ? len : 32);
         newbase = static_cast<char *>(js_realloc(ss->base, newlen));
         if (!newbase) {
             /* Ran out of memory */
-            return -1;
+            return false;
         }
         ss->base = newbase;
         ss->maxlen = newlen;
         ss->cur = ss->base + off;
     }
 
     /* Copy data */
     while (len) {
         --len;
         *ss->cur++ = *sp++;
     }
     MOZ_ASSERT(size_t(ss->cur - ss->base) <= ss->maxlen);
-    return 0;
+    return true;
 }
 
 /*
  * sprintf into a js_malloc'd buffer
  */
 JS_PUBLIC_API(char *)
 JS_smprintf(const char *fmt, ...)
 {
@@ -983,45 +949,43 @@ JS_smprintf_free(char *mem)
 {
     js_free(mem);
 }
 
 JS_PUBLIC_API(char *)
 JS_vsmprintf(const char *fmt, va_list ap)
 {
     SprintfState ss;
-    int rv;
 
     ss.stuff = GrowStuff;
     ss.base = 0;
     ss.cur = 0;
     ss.maxlen = 0;
-    rv = dosprintf(&ss, fmt, ap);
-    if (rv < 0) {
+    if (!dosprintf(&ss, fmt, ap)) {
         js_free(ss.base);
         return 0;
     }
     return ss.base;
 }
 
 /*
  * Stuff routine that discards overflow data
  */
-static int
+static bool
 LimitStuff(SprintfState *ss, const char *sp, size_t len)
 {
     size_t limit = ss->maxlen - (ss->cur - ss->base);
 
     if (len > limit)
         len = limit;
     while (len) {
         --len;
         *ss->cur++ = *sp++;
     }
-    return 0;
+    return true;
 }
 
 /*
  * sprintf into a fixed size buffer. Make sure there is a NUL at the end
  * when finished.
  */
 JS_PUBLIC_API(uint32_t)
 JS_snprintf(char *out, uint32_t outlen, const char *fmt, ...)
@@ -1041,19 +1005,18 @@ JS_snprintf(char *out, uint32_t outlen, 
 
 JS_PUBLIC_API(uint32_t)
 JS_vsnprintf(char *out, uint32_t outlen, const char *fmt, va_list ap)
 {
     SprintfState ss;
     uint32_t n;
 
     MOZ_ASSERT(int32_t(outlen) > 0);
-    if (int32_t(outlen) <= 0) {
+    if (int32_t(outlen) <= 0)
         return 0;
-    }
 
     ss.stuff = LimitStuff;
     ss.base = out;
     ss.cur = out;
     ss.maxlen = outlen;
     (void) dosprintf(&ss, fmt, ap);
 
     /* If we added chars, and we didn't append a null, do it now. */
@@ -1075,31 +1038,29 @@ JS_sprintf_append(char *last, const char
     va_end(ap);
     return rv;
 }
 
 JS_PUBLIC_API(char *)
 JS_vsprintf_append(char *last, const char *fmt, va_list ap)
 {
     SprintfState ss;
-    int rv;
 
     ss.stuff = GrowStuff;
     if (last) {
         size_t lastlen = strlen(last);
         ss.base = last;
         ss.cur = last + lastlen;
         ss.maxlen = lastlen;
     } else {
         ss.base = 0;
         ss.cur = 0;
         ss.maxlen = 0;
     }
-    rv = dosprintf(&ss, fmt, ap);
-    if (rv < 0) {
+    if (!dosprintf(&ss, fmt, ap)) {
         js_free(ss.base);
         return 0;
     }
     return ss.base;
 }
 
 #undef TYPE_INT16
 #undef TYPE_UINT16