From 5f59a34ab747dde8ede7357f3431bf06bd6002fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Sun, 8 Mar 2026 17:28:33 +0100 Subject: New upstream version 1.4.2 --- lib/vasnprintf.c | 656 ++++++++++++++++++++----------------------------------- 1 file changed, 231 insertions(+), 425 deletions(-) (limited to 'lib/vasnprintf.c') diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 614da318..12bdd45b 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -1,5 +1,5 @@ /* vsprintf with automatic memory allocation. - Copyright (C) 1999, 2002-2025 Free Software Foundation, Inc. + Copyright (C) 1999, 2002-2026 Free Software Foundation, Inc. This file is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -134,6 +134,16 @@ # include "fpucw.h" #endif +#if HAVE_SAME_LONG_DOUBLE_AS_DOUBLE +/* Override macros from fpucw.h. */ +# undef DECL_LONG_DOUBLE_ROUNDING +# define DECL_LONG_DOUBLE_ROUNDING +# undef BEGIN_LONG_DOUBLE_ROUNDING +# define BEGIN_LONG_DOUBLE_ROUNDING() +# undef END_LONG_DOUBLE_ROUNDING +# define END_LONG_DOUBLE_ROUNDING() +#endif + /* Default parameters. */ #ifndef VASNPRINTF # if WIDE_CHAR_VERSION @@ -739,19 +749,18 @@ multiply (mpn_t src1, mpn_t src2, mpn_t *dest) /* Here 1 <= len1 <= len2. */ size_t dlen; mp_limb_t *dp; - size_t k, i, j; dlen = len1 + len2; dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t)); if (dp == NULL) return NOMEM_PTR; - for (k = len2; k > 0; ) + for (size_t k = len2; k > 0; ) dp[--k] = 0; - for (i = 0; i < len1; i++) + for (size_t i = 0; i < len1; i++) { mp_limb_t digit1 = p1[i]; mp_twolimb_t carry = 0; - for (j = 0; j < len2; j++) + for (size_t j = 0; j < len2; j++) { mp_limb_t digit2 = p2[j]; carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2; @@ -835,7 +844,6 @@ divide (mpn_t a, mpn_t b, mpn_t *q) size_t a_len = a.nlimbs; const mp_limb_t *b_ptr = b.limbs; size_t b_len = b.nlimbs; - mp_limb_t *roomptr; mp_limb_t *tmp_roomptr = NULL; mp_limb_t *q_ptr; size_t q_len; @@ -845,7 +853,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) /* Allocate room for a_len+2 digits. (Need a_len+1 digits for the real division and 1 more digit for the final rounding of q.) */ - roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t)); + mp_limb_t *roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t)); if (roomptr == NULL) return NOMEM_PTR; @@ -887,8 +895,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) mp_limb_t remainder = 0; const mp_limb_t *sourceptr = a_ptr + a_len; mp_limb_t *destptr = q_ptr + a_len; - size_t count; - for (count = a_len; count > 0; count--) + for (size_t count = a_len; count > 0; count--) { mp_twolimb_t num = ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr; @@ -989,8 +996,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) const mp_limb_t *sourceptr = b_ptr; mp_limb_t *destptr = tmp_roomptr; mp_twolimb_t accu = 0; - size_t count; - for (count = b_len; count > 0; count--) + for (size_t count = b_len; count > 0; count--) { accu += (mp_twolimb_t) *sourceptr++ << s; *destptr++ = (mp_limb_t) accu; @@ -1017,8 +1023,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) const mp_limb_t *sourceptr = a_ptr; mp_limb_t *destptr = r_ptr; mp_twolimb_t accu = 0; - size_t count; - for (count = a_len; count > 0; count--) + for (size_t count = a_len; count > 0; count--) { accu += (mp_twolimb_t) *sourceptr++ << s; *destptr++ = (mp_limb_t) accu; @@ -1094,8 +1099,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) const mp_limb_t *sourceptr = b_ptr; mp_limb_t *destptr = r_ptr + j; mp_twolimb_t carry = 0; - size_t count; - for (count = b_len; count > 0; count--) + for (size_t count = b_len; count > 0; count--) { /* Here 0 <= carry <= q*. */ carry = @@ -1119,8 +1123,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) const mp_limb_t *sourceptr = b_ptr; mp_limb_t *destptr = r_ptr + j; mp_limb_t carry = 0; - size_t count; - for (count = b_len; count > 0; count--) + for (size_t count = b_len; count > 0; count--) { mp_limb_t source1 = *sourceptr++; mp_limb_t source2 = *destptr; @@ -1152,8 +1155,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) { mp_limb_t ptr = r_ptr + r_len; mp_twolimb_t accu = 0; - size_t count; - for (count = r_len; count > 0; count--) + for (size_t count = r_len; count > 0; count--) { accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS; accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s); @@ -1168,29 +1170,25 @@ divide (mpn_t a, mpn_t b, mpn_t *q) /* Compare r << 1 with b. */ if (r_len > b_len) goto increment_q; - { - size_t i; - for (i = b_len;;) - { - mp_limb_t r_i = - (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0) - | (i < r_len ? r_ptr[i] << 1 : 0); - mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0); - if (r_i > b_i) - goto increment_q; - if (r_i < b_i) - goto keep_q; - if (i == 0) - break; - i--; - } - } + for (size_t i = b_len;;) + { + mp_limb_t r_i = + (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0) + | (i < r_len ? r_ptr[i] << 1 : 0); + mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0); + if (r_i > b_i) + goto increment_q; + if (r_i < b_i) + goto keep_q; + if (i == 0) + break; + i--; + } if (q_len > 0 && ((q_ptr[0] & 1) != 0)) /* q is odd. */ increment_q: { - size_t i; - for (i = 0; i < q_len; i++) + for (size_t i = 0; i < q_len; i++) if (++(q_ptr[i]) != 0) goto keep_q; q_ptr[q_len++] = 1; @@ -1236,8 +1234,7 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes) /* Divide a by 10^9, in-place. */ mp_limb_t remainder = 0; mp_limb_t *ptr = a_ptr + a_len; - size_t count; - for (count = a_len; count > 0; count--) + for (size_t count = a_len; count > 0; count--) { mp_twolimb_t num = ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr; @@ -1245,7 +1242,7 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes) remainder = num % 1000000000; } /* Store the remainder as 9 decimal digits. */ - for (count = 9; count > 0; count--) + for (size_t count = 9; count > 0; count--) { *d_ptr++ = '0' + (remainder % 10); remainder = remainder / 10; @@ -1279,18 +1276,15 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes) static void * decode_long_double (long double x, int *ep, mpn_t *mp) { - mpn_t m; - int exp; - long double y; - size_t i; - /* Allocate memory for result. */ + mpn_t m; m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); if (m.limbs == NULL) return NULL; /* Split into exponential part and mantissa. */ - y = safe_frexpl (x, &exp); + int exp; + long double y = safe_frexpl (x, &exp); if (!(y >= 0.0L && y < 1.0L)) abort (); /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the @@ -1328,7 +1322,7 @@ decode_long_double (long double x, int *ep, mpn_t *mp) } # endif # endif - for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) + for (size_t i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) { mp_limb_t hi, lo; y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); @@ -1367,18 +1361,15 @@ decode_long_double (long double x, int *ep, mpn_t *mp) static void * decode_double (double x, int *ep, mpn_t *mp) { - mpn_t m; - int exp; - double y; - size_t i; - /* Allocate memory for result. */ + mpn_t m; m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); if (m.limbs == NULL) return NULL; /* Split into exponential part and mantissa. */ - y = frexp (x, &exp); + int exp; + double y = frexp (x, &exp); if (!(y >= 0.0 && y < 1.0)) abort (); /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the @@ -1416,7 +1407,7 @@ decode_double (double x, int *ep, mpn_t *mp) } # endif # endif - for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) + for (size_t i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) { mp_limb_t hi, lo; y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); @@ -1451,24 +1442,11 @@ decode_double (double x, int *ep, mpn_t *mp) static char * scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) { - int s; - size_t extra_zeroes; - unsigned int abs_n; - unsigned int abs_s; - mp_limb_t *pow5_ptr; - size_t pow5_len; - unsigned int s_limbs; - unsigned int s_bits; - mpn_t pow5; - mpn_t z; - void *z_memory; - char *digits; - /* x = 2^e * m, hence y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m) = round (2^s * 5^n * m). */ - s = e + n; - extra_zeroes = 0; + int s = e + n; + size_t extra_zeroes = 0; /* Factor out a common power of 10 if possible. */ if (s > 0 && n > 0) { @@ -1481,11 +1459,12 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) z = round (2^s * 5^n * m). */ /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same sign. 2.322 is slightly larger than log(5)/log(2). */ - abs_n = (n >= 0 ? n : -n); - abs_s = (s >= 0 ? s : -s); - pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1 - + abs_s / GMP_LIMB_BITS + 1) - * sizeof (mp_limb_t)); + unsigned int abs_n = (n >= 0 ? n : -n); + unsigned int abs_s = (s >= 0 ? s : -s); + mp_limb_t *pow5_ptr = + (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1 + + abs_s / GMP_LIMB_BITS + 1) + * sizeof (mp_limb_t)); if (pow5_ptr == NULL) { free (memory); @@ -1493,7 +1472,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) } /* Initialize with 1. */ pow5_ptr[0] = 1; - pow5_len = 1; + size_t pow5_len = 1; /* Multiply with 5^|n|. */ if (abs_n > 0) { @@ -1502,13 +1481,11 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625, 1220703125 }; - unsigned int n13; - for (n13 = 0; n13 <= abs_n; n13 += 13) + for (unsigned int n13 = 0; n13 <= abs_n; n13 += 13) { mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13]; - size_t j; mp_twolimb_t carry = 0; - for (j = 0; j < pow5_len; j++) + for (size_t j = 0; j < pow5_len; j++) { mp_limb_t digit2 = pow5_ptr[j]; carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2; @@ -1519,8 +1496,11 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) pow5_ptr[pow5_len++] = (mp_limb_t) carry; } } - s_limbs = abs_s / GMP_LIMB_BITS; - s_bits = abs_s % GMP_LIMB_BITS; + unsigned int s_limbs = abs_s / GMP_LIMB_BITS; + unsigned int s_bits = abs_s % GMP_LIMB_BITS; + mpn_t pow5; + mpn_t z; + void *z_memory; if (n >= 0 ? s >= 0 : s <= 0) { /* Multiply with 2^|s|. */ @@ -1528,8 +1508,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) { mp_limb_t *ptr = pow5_ptr; mp_twolimb_t accu = 0; - size_t count; - for (count = pow5_len; count > 0; count--) + for (size_t count = pow5_len; count > 0; count--) { accu += (mp_twolimb_t) *ptr << s_bits; *ptr++ = (mp_limb_t) accu; @@ -1543,13 +1522,12 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) } if (s_limbs > 0) { - size_t count; - for (count = pow5_len; count > 0;) + for (size_t count = pow5_len; count > 0;) { count--; pow5_ptr[s_limbs + count] = pow5_ptr[count]; } - for (count = s_limbs; count > 0;) + for (size_t count = s_limbs; count > 0;) { count--; pow5_ptr[count] = 0; @@ -1590,8 +1568,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) /* Construct 2^|s|. */ { mp_limb_t *ptr = pow5_ptr + pow5_len; - size_t i; - for (i = 0; i < s_limbs; i++) + for (size_t i = 0; i < s_limbs; i++) ptr[i] = 0; ptr[s_limbs] = (mp_limb_t) 1 << s_bits; denominator.limbs = ptr; @@ -1616,17 +1593,13 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) } { mp_limb_t *destptr = num_ptr; - { - size_t i; - for (i = 0; i < s_limbs; i++) - *destptr++ = 0; - } + for (size_t i = 0; i < s_limbs; i++) + *destptr++ = 0; if (s_bits > 0) { const mp_limb_t *sourceptr = m.limbs; mp_twolimb_t accu = 0; - size_t count; - for (count = m.nlimbs; count > 0; count--) + for (size_t count = m.nlimbs; count > 0; count--) { accu += (mp_twolimb_t) *sourceptr++ << s_bits; *destptr++ = (mp_limb_t) accu; @@ -1638,8 +1611,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) else { const mp_limb_t *sourceptr = m.limbs; - size_t count; - for (count = m.nlimbs; count > 0; count--) + for (size_t count = m.nlimbs; count > 0; count--) *destptr++ = *sourceptr++; } numerator.limbs = num_ptr; @@ -1656,7 +1628,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) if (z_memory == NOMEM_PTR) return NULL; - digits = convert_to_decimal (z, extra_zeroes); + char *digits = convert_to_decimal (z, extra_zeroes); free (z_memory); return digits; } @@ -1711,13 +1683,9 @@ scale10_round_decimal_double (double x, int n) static int floorlog10l (long double x) { - int exp; - long double y; - double z; - double l; - /* Split into exponential part and mantissa. */ - y = safe_frexpl (x, &exp); + int exp; + long double y = safe_frexpl (x, &exp); if (!(y >= 0.0L && y < 1.0L)) abort (); if (y == 0.0L) @@ -1758,8 +1726,8 @@ floorlog10l (long double x) if (!(y >= 0.5L && y < 1.0L)) abort (); /* Compute an approximation for l = log2(x) = exp + log2(y). */ - l = exp; - z = y; + double l = exp; + double z = y; if (z < 0.70710678118654752444) { z *= 1.4142135623730950488; @@ -1802,13 +1770,9 @@ floorlog10l (long double x) static int floorlog10 (double x) { - int exp; - double y; - double z; - double l; - /* Split into exponential part and mantissa. */ - y = frexp (x, &exp); + int exp; + double y = frexp (x, &exp); if (!(y >= 0.0 && y < 1.0)) abort (); if (y == 0.0) @@ -1849,8 +1813,8 @@ floorlog10 (double x) if (!(y >= 0.5 && y < 1.0)) abort (); /* Compute an approximation for l = log2(x) = exp + log2(y). */ - l = exp; - z = y; + double l = exp; + double z = y; if (z < 0.70710678118654752444) { z *= 1.4142135623730950488; @@ -2513,21 +2477,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto fail_1_with_EINVAL; { - size_t buf_neededlength; - TCHAR_T *buf; - TCHAR_T *buf_malloced; - const FCHAR_T *cp; - size_t di; - DIRECTIVE *dp; - /* Output string accumulator. */ - DCHAR_T *result; - size_t allocated; - size_t length; - /* Allocate a small buffer that will hold a directive passed to sprintf or snprintf. */ - buf_neededlength = + size_t buf_neededlength = xsum4 (7, d.max_width_length, d.max_precision_length, 6); + TCHAR_T *buf; + TCHAR_T *buf_malloced; #if HAVE_ALLOCA if (buf_neededlength < 4000 / sizeof (TCHAR_T)) { @@ -2546,9 +2501,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, buf_malloced = buf; } - result = resultbuf; - allocated = (resultbuf != NULL ? *lengthp : 0); - length = 0; + /* Output string accumulator. */ + DCHAR_T *result = resultbuf; + size_t allocated = (resultbuf != NULL ? *lengthp : 0); + size_t length = 0; + /* Invariants: result is either == resultbuf or malloc-allocated. If result == NULL, resultbuf is == NULL as well. @@ -2559,15 +2516,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, #define ENSURE_ALLOCATION_ELSE(needed, oom_statement) \ if ((needed) > allocated) \ { \ - size_t memory_size; \ - DCHAR_T *memory; \ - \ allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \ if ((needed) > allocated) \ allocated = (needed); \ - memory_size = xtimes (allocated, sizeof (DCHAR_T)); \ + size_t memory_size = xtimes (allocated, sizeof (DCHAR_T)); \ if (size_overflow_p (memory_size)) \ oom_statement \ + DCHAR_T *memory; \ if (result == resultbuf) \ memory = (DCHAR_T *) malloc (memory_size); \ else \ @@ -2581,6 +2536,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, #define ENSURE_ALLOCATION(needed) \ ENSURE_ALLOCATION_ELSE((needed), goto out_of_memory; ) + const FCHAR_T *cp; + size_t di; + DIRECTIVE *dp; for (cp = format, di = 0, dp = &d.dir[0]; ; cp = dp->dir_end, di++, dp++) { if (cp != dp->dir_start) @@ -2681,22 +2639,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { arg_type type = a.arg[dp->arg_index].type; int flags = dp->flags; - int has_width; - size_t width; - int has_precision; - size_t precision; - has_width = 0; - width = 0; + int has_width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -2719,17 +2671,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, has_width = 1; } - has_precision = 0; - precision = 0; + int has_precision = 0; + size_t precision = 0; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -3074,22 +3024,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, than INT_MAX characters, whence snprintf or sprintf would fail to process it. */ int flags = dp->flags; - int has_width; - size_t width; - int has_precision; - size_t precision; - has_width = 0; - width = 0; + int has_width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -3112,17 +3056,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, has_width = 1; } - has_precision = 0; - precision = 6; + int has_precision = 0; + size_t precision = 6; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -3144,18 +3086,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { const char *arg = a.arg[dp->arg_index].a.a_string; - size_t bytes; -# if ENABLE_UNISTDIO && DCHAR_IS_TCHAR - size_t characters; -# endif -# if !DCHAR_IS_TCHAR - /* This code assumes that TCHAR_T is 'char'. */ - static_assert (sizeof (TCHAR_T) == 1); - DCHAR_T *tmpdst; - size_t tmpdst_len; -# endif - size_t w; + size_t bytes; if (has_precision) { /* Use only at most PRECISION bytes, from the left. */ @@ -3169,6 +3101,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } # if ENABLE_UNISTDIO && DCHAR_IS_TCHAR + size_t characters; if (has_width) characters = mbsnlen (arg, bytes); else @@ -3180,6 +3113,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif # if !DCHAR_IS_TCHAR + /* This code assumes that TCHAR_T is 'char'. */ + static_assert (sizeof (TCHAR_T) == 1); + DCHAR_T *tmpdst; + size_t tmpdst_len; /* Convert from TCHAR_T[] to DCHAR_T[]. */ tmpdst = DCHAR_CONV_FROM_ENCODING (locale_charset (), @@ -3191,6 +3128,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto fail_with_errno; # endif + size_t w; if (has_width) { # if ENABLE_UNISTDIO @@ -3260,18 +3198,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, convert back the result from a char[] to a wchar_t[]. Instead, just copy the argument wchar_t[] to the result. */ int flags = dp->flags; - size_t width; - width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -3300,20 +3235,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (dp->conversion == 's') { - int has_precision; - size_t precision; - - has_precision = 0; - precision = 6; + int has_precision = 0; + size_t precision = 6; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -3339,9 +3269,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { /* Use only at most PRECISION wide characters, from the left. */ - const wchar_t *ls_arg_end; - - ls_arg_end = ls_arg; + const wchar_t *ls_arg_end = ls_arg; characters = 0; for (; precision > 0; precision--) { @@ -3413,22 +3341,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, such bugs, we implement the entire processing of the 's' directive ourselves. */ int flags = dp->flags; - int has_width; - size_t width; - int has_precision; - size_t precision; - has_width = 0; - width = 0; + int has_width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -3451,17 +3373,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, has_width = 1; } - has_precision = 0; - precision = 6; + int has_precision = 0; + size_t precision = 6; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -3563,13 +3483,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (has_precision || has_width) { /* We know the number of wide characters in advance. */ - size_t remaining; # if HAVE_MBRTOWC mbstate_t state; mbszero (&state); # endif ENSURE_ALLOCATION (xsum (length, characters)); - for (remaining = characters; remaining > 0; remaining--) + for (size_t remaining = characters; remaining > 0; remaining--) { wchar_t wc; int count; @@ -3655,13 +3574,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif while (precision > 0) { - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; - if (*arg_end == 0) /* Found the terminating null wide character. */ break; - count = local_wcrtomb (cbuf, *arg_end, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, *arg_end, &state); if (count < 0) /* Cannot convert. */ goto fail_with_EILSEQ; @@ -3694,13 +3611,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif for (;;) { - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; - if (*arg_end == 0) /* Found the terminating null wide character. */ break; - count = local_wcrtomb (cbuf, *arg_end, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, *arg_end, &state); if (count < 0) /* Cannot convert. */ goto fail_with_EILSEQ; @@ -3727,27 +3642,22 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # if !DCHAR_IS_TCHAR { - TCHAR_T *tmpsrc; - /* Convert the string into a piece of temporary memory. */ - tmpsrc = (TCHAR_T *) malloc (bytes * sizeof (TCHAR_T)); + TCHAR_T *tmpsrc = (TCHAR_T *) malloc (bytes * sizeof (TCHAR_T)); if (tmpsrc == NULL) goto out_of_memory; { TCHAR_T *tmpptr = tmpsrc; - size_t remaining; # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t mbstate_t state; mbszero (&state); # endif - for (remaining = bytes; remaining > 0; ) + for (size_t remaining = bytes; remaining > 0; ) { - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; - if (*arg == 0) abort (); - count = local_wcrtomb (cbuf, *arg, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, *arg, &state); if (count <= 0) /* Inconsistency. */ abort (); @@ -3814,20 +3724,17 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (has_precision || has_width) { /* We know the number of bytes in advance. */ - size_t remaining; # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t mbstate_t state; mbszero (&state); # endif ENSURE_ALLOCATION (xsum (length, bytes)); - for (remaining = bytes; remaining > 0; ) + for (size_t remaining = bytes; remaining > 0; ) { - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; - if (*arg == 0) abort (); - count = local_wcrtomb (cbuf, *arg, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, *arg, &state); if (count <= 0) /* Inconsistency. */ abort (); @@ -3847,12 +3754,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif while (arg < arg_end) { - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; - if (*arg == 0) abort (); - count = local_wcrtomb (cbuf, *arg, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, *arg, &state); if (count <= 0) /* Cannot convert. */ goto fail_with_EILSEQ; @@ -3889,20 +3794,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, a correct behaviour for the null wint_t argument and/or the fallback that avoids EILSEQ. */ int flags = dp->flags; - int has_width; - size_t width; - has_width = 0; - width = 0; + int has_width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -3945,14 +3846,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif { /* Count the number of bytes. */ - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t mbstate_t state; mbszero (&state); # endif - count = local_wcrtomb (cbuf, arg, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, arg, &state); if (count < 0) /* Cannot convert. */ goto fail_with_EILSEQ; @@ -3980,14 +3880,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Convert the string into a piece of temporary memory. */ if (bytes > 0) { - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t mbstate_t state; mbszero (&state); # endif - count = local_wcrtomb (cbuf, arg, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, arg, &state); if (count <= 0) /* Inconsistency. */ abort (); @@ -4047,13 +3946,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, ENSURE_ALLOCATION (xsum (length, bytes)); if (bytes > 0) { - int count; # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t mbstate_t state; mbszero (&state); # endif - count = local_wcrtomb (result + length, arg, &state); + int count = local_wcrtomb (result + length, arg, &state); if (count <= 0) /* Inconsistency. */ abort (); @@ -4062,14 +3960,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } else { - char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ - int count; # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t mbstate_t state; mbszero (&state); # endif - count = local_wcrtomb (cbuf, arg, &state); + char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */ + int count = local_wcrtomb (cbuf, arg, &state); if (count < 0) /* Cannot convert. */ goto fail_with_EILSEQ; @@ -4102,18 +3999,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Implement the 'c' directive ourselves, in order to avoid EILSEQ in the "C" locale. */ int flags = dp->flags; - size_t width; - width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -4139,9 +4033,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { char arg = (char) a.arg[dp->arg_index].a.a_char; mbstate_t state; - wchar_t wc; - mbszero (&state); + + wchar_t wc; int count = mbrtowc (&wc, &arg, 1, &state); if (count < 0) /* Invalid or incomplete multibyte character. */ @@ -4182,30 +4076,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { arg_type type = a.arg[dp->arg_index].type; int flags = dp->flags; - int has_width; - size_t width; - int has_precision; - size_t precision; - size_t tmp_length; - size_t count; - DCHAR_T tmpbuf[700]; - DCHAR_T *tmp; - DCHAR_T *tmp_end; - DCHAR_T *tmp_start; - DCHAR_T *pad_ptr; - DCHAR_T *p; - has_width = 0; - width = 0; + int has_width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -4228,17 +4108,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, has_width = 1; } - has_precision = 0; - precision = 1; + int has_precision = 0; + size_t precision = 1; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -4259,6 +4137,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } /* Allocate a temporary buffer of sufficient size. */ + size_t tmp_length; switch (type) { default: @@ -4325,6 +4204,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (tmp_length < width) tmp_length = width; + DCHAR_T tmpbuf[700]; + DCHAR_T *tmp; if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) tmp = tmpbuf; else @@ -4340,7 +4221,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto out_of_memory; } - tmp_end = tmp + tmp_length; + DCHAR_T *tmp_end = tmp + tmp_length; unsigned long long arg; switch (type) @@ -4389,7 +4270,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } int need_prefix = ((flags & FLAG_ALT) && arg != 0); - p = tmp_end; + DCHAR_T *p = tmp_end; /* "The result of converting a zero value with a precision of zero is no characters." */ if (!(has_precision && precision == 0 && arg == 0)) @@ -4409,7 +4290,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, *--p = '0'; } - pad_ptr = p; + DCHAR_T *pad_ptr = p; if (need_prefix) { @@ -4422,12 +4303,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif *--p = '0'; } - tmp_start = p; + DCHAR_T *tmp_start = p; /* The generated string now extends from tmp_start to tmp_end, with the zero padding insertion point being at pad_ptr, tmp_start <= pad_ptr <= tmp_end. */ - count = tmp_end - tmp_start; + size_t count = tmp_end - tmp_start; if (count < width) { @@ -4503,26 +4384,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { arg_type type = a.arg[dp->arg_index].type; int flags = dp->flags; - size_t width; - int has_precision; - size_t precision; - size_t tmp_length; - size_t count; - DCHAR_T tmpbuf[700]; - DCHAR_T *tmp; - DCHAR_T *pad_ptr; - DCHAR_T *p; - width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -4544,17 +4414,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto overflow; } - has_precision = 0; - precision = 0; + int has_precision = 0; + size_t precision = 0; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -4575,6 +4443,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } /* Allocate a temporary buffer of sufficient size. */ + size_t tmp_length; if (type == TYPE_LONGDOUBLE) tmp_length = (unsigned int) ((LDBL_DIG + 1) @@ -4597,6 +4466,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ + DCHAR_T tmpbuf[700]; + DCHAR_T *tmp; if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) tmp = tmpbuf; else @@ -4612,8 +4483,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto out_of_memory; } - pad_ptr = NULL; - p = tmp; + DCHAR_T *pad_ptr = NULL; + DCHAR_T *p = tmp; if (type == TYPE_LONGDOUBLE) { # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || (NEED_WPRINTF_DIRECTIVE_LA && WIDE_CHAR_VERSION) @@ -4632,11 +4503,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } else { - int sign = 0; DECL_LONG_DOUBLE_ROUNDING BEGIN_LONG_DOUBLE_ROUNDING (); + int sign = 0; if (signbit (arg)) /* arg < 0.0L or negative zero */ { sign = -1; @@ -4665,7 +4536,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { int exponent; long double mantissa; - if (arg > 0.0L) mantissa = printf_frexpl (arg, &exponent); else @@ -4679,9 +4549,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { /* Round the mantissa. */ long double tail = mantissa; - size_t q; - for (q = precision; ; q--) + for (size_t q = precision; ; q--) { int digit = (int) tail; tail -= digit; @@ -4696,7 +4565,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, tail *= 16.0L; } if (tail != 0.0L) - for (q = precision; q > 0; q--) + for (size_t q = precision; q > 0; q--) tail *= 0.0625L; mantissa += tail; } @@ -4705,9 +4574,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, *p++ = dp->conversion - 'A' + 'X'; pad_ptr = p; { - int digit; - - digit = (int) mantissa; + int digit = (int) mantissa; mantissa -= digit; *p++ = '0' + digit; if ((flags & FLAG_ALT) @@ -4754,9 +4621,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { char expbuf[6 + 1]; - const char *ep; sprintf (expbuf, "%+d", exponent); - for (ep = expbuf; (*p = *ep) != '\0'; ep++) + for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++) p++; } # endif @@ -4787,7 +4653,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { int sign = 0; - if (signbit (arg)) /* arg < 0.0 or negative zero */ { sign = -1; @@ -4816,7 +4681,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { int exponent; double mantissa; - if (arg > 0.0) mantissa = printf_frexp (arg, &exponent); else @@ -4830,9 +4694,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { /* Round the mantissa. */ double tail = mantissa; - size_t q; - for (q = precision; ; q--) + for (size_t q = precision; ; q--) { int digit = (int) tail; tail -= digit; @@ -4847,7 +4710,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, tail *= 16.0; } if (tail != 0.0) - for (q = precision; q > 0; q--) + for (size_t q = precision; q > 0; q--) tail *= 0.0625; mantissa += tail; } @@ -4856,9 +4719,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, *p++ = dp->conversion - 'A' + 'X'; pad_ptr = p; { - int digit; - - digit = (int) mantissa; + int digit = (int) mantissa; mantissa -= digit; *p++ = '0' + digit; if ((flags & FLAG_ALT) @@ -4905,9 +4766,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { char expbuf[6 + 1]; - const char *ep; sprintf (expbuf, "%+d", exponent); - for (ep = expbuf; (*p = *ep) != '\0'; ep++) + for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++) p++; } # endif @@ -4920,7 +4780,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* The generated string now extends from tmp to p, with the zero padding insertion point being at pad_ptr. */ - count = p - tmp; + size_t count = p - tmp; if (count < width) { @@ -5011,26 +4871,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, arg_type type = a.arg[dp->arg_index].type; # endif int flags = dp->flags; - size_t width; - size_t count; - int has_precision; - size_t precision; - size_t tmp_length; - DCHAR_T tmpbuf[700]; - DCHAR_T *tmp; - DCHAR_T *pad_ptr; - DCHAR_T *p; - width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -5052,17 +4901,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto overflow; } - has_precision = 0; - precision = 0; + int has_precision = 0; + size_t precision = 0; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -5091,6 +4938,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, precision = 6; /* Allocate a temporary buffer of sufficient size. */ + size_t tmp_length; # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1); # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE @@ -5155,6 +5003,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ + DCHAR_T tmpbuf[700]; + DCHAR_T *tmp; if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) tmp = tmpbuf; else @@ -5170,8 +5020,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto out_of_memory; } - pad_ptr = NULL; - p = tmp; + DCHAR_T *pad_ptr = NULL; + DCHAR_T *p = tmp; # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE @@ -5193,11 +5043,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } else { - int sign = 0; DECL_LONG_DOUBLE_ROUNDING BEGIN_LONG_DOUBLE_ROUNDING (); + int sign = 0; if (signbit (arg)) /* arg < 0.0L or negative zero */ { sign = -1; @@ -5229,17 +5079,14 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (dp->conversion == 'f' || dp->conversion == 'F') { - char *digits; - size_t ndigits; - - digits = + char *digits = scale10_round_decimal_long_double (arg, precision); if (digits == NULL) { END_LONG_DOUBLE_ROUNDING (); goto out_of_memory; } - ndigits = strlen (digits); + size_t ndigits = strlen (digits); if (ndigits > precision) { @@ -5355,12 +5202,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { /* arg > 0.0L. */ - int adjusted; + exponent = floorlog10l (arg); + int adjusted = 0; char *digits; size_t ndigits; - - exponent = floorlog10l (arg); - adjusted = 0; for (;;) { digits = @@ -5451,9 +5296,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { char expbuf[6 + 1]; - const char *ep; sprintf (expbuf, "%+.2d", exponent); - for (ep = expbuf; (*p = *ep) != '\0'; ep++) + for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++) p++; } # endif @@ -5489,14 +5333,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { /* arg > 0.0L. */ - int exponent; - int adjusted; + int exponent = floorlog10l (arg); + int adjusted = 0; char *digits; size_t ndigits; - size_t nzeroes; - - exponent = floorlog10l (arg); - adjusted = 0; for (;;) { digits = @@ -5556,7 +5396,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Determine the number of trailing zeroes that have to be dropped. */ - nzeroes = 0; + size_t nzeroes = 0; if ((flags & FLAG_ALT) == 0) while (nzeroes < ndigits && digits[nzeroes] == '0') @@ -5706,9 +5546,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { char expbuf[6 + 1]; - const char *ep; sprintf (expbuf, "%+.2d", exponent); - for (ep = expbuf; (*p = *ep) != '\0'; ep++) + for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++) p++; } # endif @@ -5808,7 +5647,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { int sign = 0; - if (signbit (arg)) /* arg < 0.0 or negative zero */ { sign = -1; @@ -5840,14 +5678,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (dp->conversion == 'f' || dp->conversion == 'F') { - char *digits; - size_t ndigits; - - digits = + char *digits = scale10_round_decimal_double (arg, precision); if (digits == NULL) goto out_of_memory; - ndigits = strlen (digits); + size_t ndigits = strlen (digits); if (ndigits > precision) { @@ -5963,12 +5798,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { /* arg > 0.0. */ - int adjusted; + exponent = floorlog10 (arg); + int adjusted = 0; char *digits; size_t ndigits; - - exponent = floorlog10 (arg); - adjusted = 0; for (;;) { digits = @@ -6070,9 +5903,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { char expbuf[6 + 1]; - const char *ep; sprintf (expbuf, decimal_format, exponent); - for (ep = expbuf; (*p = *ep) != '\0'; ep++) + for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++) p++; } } @@ -6109,14 +5941,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { /* arg > 0.0. */ - int exponent; - int adjusted; + int exponent = floorlog10 (arg); + int adjusted = 0; char *digits; size_t ndigits; - size_t nzeroes; - - exponent = floorlog10 (arg); - adjusted = 0; for (;;) { digits = @@ -6172,7 +6000,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Determine the number of trailing zeroes that have to be dropped. */ - nzeroes = 0; + size_t nzeroes = 0; if ((flags & FLAG_ALT) == 0) while (nzeroes < ndigits && digits[nzeroes] == '0') @@ -6337,9 +6165,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, else { char expbuf[6 + 1]; - const char *ep; sprintf (expbuf, decimal_format, exponent); - for (ep = expbuf; (*p = *ep) != '\0'; ep++) + for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++) p++; } } @@ -6409,7 +6236,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* The generated string now extends from tmp to p, with the zero padding insertion point being at pad_ptr. */ - count = p - tmp; + size_t count = p - tmp; if (count < width) { @@ -6473,16 +6300,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { arg_type type = a.arg[dp->arg_index].type; int flags = dp->flags; -#if (WIDE_CHAR_VERSION && MUSL_LIBC) || NEED_PRINTF_FLAG_LEFTADJUST || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION || NEED_PRINTF_FLAG_GROUPING || NEED_PRINTF_FLAG_GROUPING_INT - int has_width; -#endif -#if !USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_FLAG_LEFTADJUST || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION || NEED_PRINTF_FLAG_GROUPING || NEED_PRINTF_FLAG_GROUPING_INT - size_t width; -#endif -#if !USE_SNPRINTF || (WIDE_CHAR_VERSION && DCHAR_IS_TCHAR) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (WIDE_CHAR_VERSION && MUSL_LIBC) || NEED_PRINTF_FLAG_LEFTADJUST || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION || NEED_PRINTF_FLAG_GROUPING || NEED_PRINTF_FLAG_GROUPING_INT - int has_precision; - size_t precision; -#endif #if NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION int prec_ourselves; #else @@ -6511,19 +6328,17 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, #endif #if (WIDE_CHAR_VERSION && MUSL_LIBC) || NEED_PRINTF_FLAG_LEFTADJUST || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION || NEED_PRINTF_FLAG_GROUPING || NEED_PRINTF_FLAG_GROUPING_INT - has_width = 0; + int has_width = 0; #endif #if !USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_FLAG_LEFTADJUST || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION || NEED_PRINTF_FLAG_GROUPING || NEED_PRINTF_FLAG_GROUPING_INT - width = 0; + size_t width = 0; if (dp->width_start != dp->width_end) { if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -6551,17 +6366,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, #endif #if !USE_SNPRINTF || (WIDE_CHAR_VERSION && DCHAR_IS_TCHAR) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (WIDE_CHAR_VERSION && MUSL_LIBC) || NEED_PRINTF_FLAG_LEFTADJUST || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION || NEED_PRINTF_FLAG_GROUPING || NEED_PRINTF_FLAG_GROUPING_INT - has_precision = 0; - precision = 6; + int has_precision = 0; + size_t precision = 6; if (dp->precision_start != dp->precision_end) { if (dp->precision_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->precision_arg_index].a.a_int; + int arg = a.arg[dp->precision_arg_index].a.a_int; /* "A negative precision is taken as if the precision were omitted." */ if (arg >= 0) @@ -6730,11 +6543,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, quick check anyway. */ if (dp->width_arg_index != ARG_NONE) { - int arg; - if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) abort (); - arg = a.arg[dp->width_arg_index].a.a_int; + int arg = a.arg[dp->width_arg_index].a.a_int; width = arg; if (arg < 0) { @@ -6959,7 +6770,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* The following platforms forbid %n: - On glibc2 systems from 2004-10-18 or newer, the use of %n in format strings in writable memory may crash the - program (if compiled with _FORTIFY_SOURCE=2). + program (if compiled with _FORTIFY_SOURCE >= 2). - On macOS 10.13 or newer, the use of %n in format strings in writable memory by default crashes the program. @@ -7659,16 +7470,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, ) { /* The result string is not guaranteed to be ASCII. */ - const TCHAR_T *tmpsrc; - DCHAR_T *tmpdst; - size_t tmpdst_len; /* This code assumes that TCHAR_T is 'char'. */ static_assert (sizeof (TCHAR_T) == 1); + const TCHAR_T *tmpsrc; # if USE_SNPRINTF tmpsrc = (TCHAR_T *) (result + length); # else tmpsrc = tmp; # endif + DCHAR_T *tmpdst; + size_t tmpdst_len; # if WIDE_CHAR_VERSION /* Convert tmpsrc[0..count-1] to a freshly allocated wide character array. */ @@ -7746,9 +7557,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif { const TCHAR_T *tmpsrc; - DCHAR_T *tmpdst; - size_t n; - # if USE_SNPRINTF if (result == resultbuf) { @@ -7768,11 +7576,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, tmpsrc = tmp; ENSURE_ALLOCATION (xsum (length, count)); # endif - tmpdst = result + length; + DCHAR_T *tmpdst = result + length; /* Copy backwards, because of overlapping. */ tmpsrc += count; tmpdst += count; - for (n = count; n > 0; n--) + for (size_t n = count; n > 0; n--) *--tmpdst = *--tmpsrc; } } @@ -7957,8 +7765,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { /* Convert the %f result to upper case for %F. */ DCHAR_T *rp = result + length; - size_t rc; - for (rc = count; rc > 0; rc--, rp++) + for (size_t rc = count; rc > 0; rc--, rp++) if (*rp >= 'a' && *rp <= 'z') *rp = *rp - 'a' + 'A'; } @@ -7981,9 +7788,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (result != resultbuf && length + 1 < allocated) { /* Shrink the allocated memory if possible. */ - DCHAR_T *memory; - - memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T)); + DCHAR_T *memory = + (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T)); if (memory != NULL) result = memory; } -- cgit v1.2.3