Bug 1070689 - Change return type of dosprintf functions from int to bool (in jsprf.cpp). r=nbp

This commit is contained in:
Gokcehan Kara 2014-10-01 20:59:15 +02:00
parent 1446201028
commit e02b736a11

View File

@ -40,7 +40,7 @@ using namespace js;
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;
@ -82,19 +82,18 @@ typedef mozilla::Vector<NumArgState, 20, js::SystemAllocPolicy> NumArgStateVecto
#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) {
@ -102,53 +101,48 @@ generic_write(SprintfState *ss, const char16_t *src, size_t srclen)
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;
@ -157,7 +151,6 @@ fill_n(SprintfState *ss, const char *src, int srclen, int width, int prec, int t
int leftspaces = 0;
int rightspaces = 0;
int cvtwidth;
int rv;
char sign;
if ((type & 1) == 0) {
@ -200,54 +193,41 @@ fill_n(SprintfState *ss, const char *src, int srclen, int width, int prec, int t
}
}
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;
}
}
rv = (*ss->stuff)(ss, src, uint32_t(srclen));
if (rv < 0) {
return rv;
if (!(*ss->stuff)(ss, "0", 1))
return false;
}
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
@ -271,12 +251,12 @@ static int cvt_l(SprintfState *ss, long num, int width, int prec, int radix,
}
/* 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
@ -309,7 +289,7 @@ static int cvt_ll(SprintfState *ss, int64_t num, int width, int prec, int radix,
*
* 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];
@ -318,7 +298,7 @@ static int cvt_f(SprintfState *ss, double d, const char *fmt0, const char *fmt1)
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;
@ -355,11 +335,11 @@ static inline size_t generic_strlen(const char16_t *s) { return js_strlen(s); }
* 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);
@ -590,7 +570,7 @@ BuildArgArray(const char *fmt, va_list ap, NumArgStateVector& nas)
/*
* The workhorse sprintf code.
*/
static int
static bool
dosprintf(SprintfState *ss, const char *fmt, va_list ap)
{
char c;
@ -610,7 +590,7 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
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 '.'
@ -621,15 +601,14 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
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;
@ -640,10 +619,9 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
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;
}
@ -656,7 +634,7 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
}
if (nas[i - 1].type == TYPE_UNKNOWN)
return -1;
return false;
ap = nas[i - 1].ap;
dolPt = fmt;
@ -785,10 +763,9 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
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:
@ -801,10 +778,9 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
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;
@ -819,41 +795,35 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
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);
if (rv < 0) {
return rv;
} else {
if (!cvt_f(ss, u.d, fmt0, fmt))
return false;
}
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;
@ -885,13 +855,12 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
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;
@ -907,21 +876,18 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
#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;
}
/************************************************************************/
@ -930,7 +896,7 @@ dosprintf(SprintfState *ss, const char *fmt, va_list ap)
* 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;
@ -944,7 +910,7 @@ GrowStuff(SprintfState *ss, const char *sp, size_t len)
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;
@ -957,7 +923,7 @@ GrowStuff(SprintfState *ss, const char *sp, size_t len)
*ss->cur++ = *sp++;
}
MOZ_ASSERT(size_t(ss->cur - ss->base) <= ss->maxlen);
return 0;
return true;
}
/*
@ -988,14 +954,12 @@ 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;
}
@ -1005,7 +969,7 @@ JS_vsmprintf(const char *fmt, va_list ap)
/*
* 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);
@ -1016,7 +980,7 @@ LimitStuff(SprintfState *ss, const char *sp, size_t len)
--len;
*ss->cur++ = *sp++;
}
return 0;
return true;
}
/*
@ -1046,9 +1010,8 @@ JS_vsnprintf(char *out, uint32_t outlen, const char *fmt, va_list ap)
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;
@ -1080,7 +1043,6 @@ JS_PUBLIC_API(char *)
JS_vsprintf_append(char *last, const char *fmt, va_list ap)
{
SprintfState ss;
int rv;
ss.stuff = GrowStuff;
if (last) {
@ -1093,8 +1055,7 @@ JS_vsprintf_append(char *last, const char *fmt, va_list ap)
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;
}