summaryrefslogtreecommitdiffstats
path: root/gl/vasnprintf.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2026-03-26 12:53:53 +0100
committerGitHub <noreply@github.com>2026-03-26 12:53:53 +0100
commit13e14a6bfd9f29cbfeab0c5161d2a994f97532e7 (patch)
tree3aa7186fe092e42783dc7e981dc39a74ea61c466 /gl/vasnprintf.c
parent9d8503f90ef25b2cecd324dc118e441f40233ea8 (diff)
downloadmonitoring-plugins-13e14a6bfd9f29cbfeab0c5161d2a994f97532e7.tar.gz
Update/gnulib 2026 03 (#2247)HEADmaster
* Sync with the 202601-stable Gnulib code (4a3650d887) * Ignore more deps stuff in gnulib * Remove autogenerated gnulib files * Ignore more gnulib generated headers
Diffstat (limited to 'gl/vasnprintf.c')
-rw-r--r--gl/vasnprintf.c674
1 files changed, 236 insertions, 438 deletions
diff --git a/gl/vasnprintf.c b/gl/vasnprintf.c
index f46e8701..85e30b90 100644
--- a/gl/vasnprintf.c
+++ b/gl/vasnprintf.c
@@ -1,5 +1,5 @@
1/* vsprintf with automatic memory allocation. 1/* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2025 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2002-2026 Free Software Foundation, Inc.
3 3
4 This file is free software: you can redistribute it and/or modify 4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as 5 it under the terms of the GNU Lesser General Public License as
@@ -180,7 +180,7 @@
180# define SNPRINTF swprintf 180# define SNPRINTF swprintf
181# endif 181# endif
182# else 182# else
183 /* Old platforms such as NetBSD 3.0, OpenBSD 3.8, HP-UX 11.00, IRIX 6.5. */ 183 /* Old platforms such as NetBSD 3.0, OpenBSD 3.8, HP-UX 11.00. */
184# define TCHAR_T char 184# define TCHAR_T char
185# endif 185# endif
186#endif 186#endif
@@ -439,7 +439,7 @@ thousands_separator_char (char stackbuf[10])
439 stackbuf[strlen (stackbuf) - 3] = '\0'; 439 stackbuf[strlen (stackbuf) - 3] = '\0';
440# if defined __sun 440# if defined __sun
441 /* Solaris specific hack: Replace wrong result (0xC2 means U+00A0). */ 441 /* Solaris specific hack: Replace wrong result (0xC2 means U+00A0). */
442 if (strcmp (&stackbuf[1], "\302") == 0) 442 if (streq (&stackbuf[1], "\302"))
443 strcpy (&stackbuf[1], MB_CUR_MAX > 1 ? "\302\240" : "\240"); 443 strcpy (&stackbuf[1], MB_CUR_MAX > 1 ? "\302\240" : "\240");
444# endif 444# endif
445 return &stackbuf[1]; 445 return &stackbuf[1];
@@ -524,7 +524,7 @@ thousands_separator_wchar (wchar_t stackbuf[10])
524 /* Now tmp = L"1<thousep>000". */ 524 /* Now tmp = L"1<thousep>000". */
525 tmp[strlen (tmp) - 3] = '\0'; 525 tmp[strlen (tmp) - 3] = '\0';
526 /* Solaris specific hack: Replace wrong result (0xC2 means U+00A0). */ 526 /* Solaris specific hack: Replace wrong result (0xC2 means U+00A0). */
527 if (strcmp (&tmp[1], "\302") == 0) 527 if (streq (&tmp[1], "\302"))
528 strcpy (&tmp[1], MB_CUR_MAX > 1 ? "\302\240" : "\240"); 528 strcpy (&tmp[1], MB_CUR_MAX > 1 ? "\302\240" : "\240");
529 if (tmp[1] != '\0') 529 if (tmp[1] != '\0')
530 { 530 {
@@ -739,19 +739,18 @@ multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
739 /* Here 1 <= len1 <= len2. */ 739 /* Here 1 <= len1 <= len2. */
740 size_t dlen; 740 size_t dlen;
741 mp_limb_t *dp; 741 mp_limb_t *dp;
742 size_t k, i, j;
743 742
744 dlen = len1 + len2; 743 dlen = len1 + len2;
745 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t)); 744 dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
746 if (dp == NULL) 745 if (dp == NULL)
747 return NOMEM_PTR; 746 return NOMEM_PTR;
748 for (k = len2; k > 0; ) 747 for (size_t k = len2; k > 0; )
749 dp[--k] = 0; 748 dp[--k] = 0;
750 for (i = 0; i < len1; i++) 749 for (size_t i = 0; i < len1; i++)
751 { 750 {
752 mp_limb_t digit1 = p1[i]; 751 mp_limb_t digit1 = p1[i];
753 mp_twolimb_t carry = 0; 752 mp_twolimb_t carry = 0;
754 for (j = 0; j < len2; j++) 753 for (size_t j = 0; j < len2; j++)
755 { 754 {
756 mp_limb_t digit2 = p2[j]; 755 mp_limb_t digit2 = p2[j];
757 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2; 756 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
@@ -835,7 +834,6 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
835 size_t a_len = a.nlimbs; 834 size_t a_len = a.nlimbs;
836 const mp_limb_t *b_ptr = b.limbs; 835 const mp_limb_t *b_ptr = b.limbs;
837 size_t b_len = b.nlimbs; 836 size_t b_len = b.nlimbs;
838 mp_limb_t *roomptr;
839 mp_limb_t *tmp_roomptr = NULL; 837 mp_limb_t *tmp_roomptr = NULL;
840 mp_limb_t *q_ptr; 838 mp_limb_t *q_ptr;
841 size_t q_len; 839 size_t q_len;
@@ -845,7 +843,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
845 /* Allocate room for a_len+2 digits. 843 /* Allocate room for a_len+2 digits.
846 (Need a_len+1 digits for the real division and 1 more digit for the 844 (Need a_len+1 digits for the real division and 1 more digit for the
847 final rounding of q.) */ 845 final rounding of q.) */
848 roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t)); 846 mp_limb_t *roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
849 if (roomptr == NULL) 847 if (roomptr == NULL)
850 return NOMEM_PTR; 848 return NOMEM_PTR;
851 849
@@ -887,8 +885,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
887 mp_limb_t remainder = 0; 885 mp_limb_t remainder = 0;
888 const mp_limb_t *sourceptr = a_ptr + a_len; 886 const mp_limb_t *sourceptr = a_ptr + a_len;
889 mp_limb_t *destptr = q_ptr + a_len; 887 mp_limb_t *destptr = q_ptr + a_len;
890 size_t count; 888 for (size_t count = a_len; count > 0; count--)
891 for (count = a_len; count > 0; count--)
892 { 889 {
893 mp_twolimb_t num = 890 mp_twolimb_t num =
894 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr; 891 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
@@ -989,8 +986,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
989 const mp_limb_t *sourceptr = b_ptr; 986 const mp_limb_t *sourceptr = b_ptr;
990 mp_limb_t *destptr = tmp_roomptr; 987 mp_limb_t *destptr = tmp_roomptr;
991 mp_twolimb_t accu = 0; 988 mp_twolimb_t accu = 0;
992 size_t count; 989 for (size_t count = b_len; count > 0; count--)
993 for (count = b_len; count > 0; count--)
994 { 990 {
995 accu += (mp_twolimb_t) *sourceptr++ << s; 991 accu += (mp_twolimb_t) *sourceptr++ << s;
996 *destptr++ = (mp_limb_t) accu; 992 *destptr++ = (mp_limb_t) accu;
@@ -1017,8 +1013,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
1017 const mp_limb_t *sourceptr = a_ptr; 1013 const mp_limb_t *sourceptr = a_ptr;
1018 mp_limb_t *destptr = r_ptr; 1014 mp_limb_t *destptr = r_ptr;
1019 mp_twolimb_t accu = 0; 1015 mp_twolimb_t accu = 0;
1020 size_t count; 1016 for (size_t count = a_len; count > 0; count--)
1021 for (count = a_len; count > 0; count--)
1022 { 1017 {
1023 accu += (mp_twolimb_t) *sourceptr++ << s; 1018 accu += (mp_twolimb_t) *sourceptr++ << s;
1024 *destptr++ = (mp_limb_t) accu; 1019 *destptr++ = (mp_limb_t) accu;
@@ -1094,8 +1089,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
1094 const mp_limb_t *sourceptr = b_ptr; 1089 const mp_limb_t *sourceptr = b_ptr;
1095 mp_limb_t *destptr = r_ptr + j; 1090 mp_limb_t *destptr = r_ptr + j;
1096 mp_twolimb_t carry = 0; 1091 mp_twolimb_t carry = 0;
1097 size_t count; 1092 for (size_t count = b_len; count > 0; count--)
1098 for (count = b_len; count > 0; count--)
1099 { 1093 {
1100 /* Here 0 <= carry <= q*. */ 1094 /* Here 0 <= carry <= q*. */
1101 carry = 1095 carry =
@@ -1119,8 +1113,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
1119 const mp_limb_t *sourceptr = b_ptr; 1113 const mp_limb_t *sourceptr = b_ptr;
1120 mp_limb_t *destptr = r_ptr + j; 1114 mp_limb_t *destptr = r_ptr + j;
1121 mp_limb_t carry = 0; 1115 mp_limb_t carry = 0;
1122 size_t count; 1116 for (size_t count = b_len; count > 0; count--)
1123 for (count = b_len; count > 0; count--)
1124 { 1117 {
1125 mp_limb_t source1 = *sourceptr++; 1118 mp_limb_t source1 = *sourceptr++;
1126 mp_limb_t source2 = *destptr; 1119 mp_limb_t source2 = *destptr;
@@ -1152,8 +1145,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
1152 { 1145 {
1153 mp_limb_t ptr = r_ptr + r_len; 1146 mp_limb_t ptr = r_ptr + r_len;
1154 mp_twolimb_t accu = 0; 1147 mp_twolimb_t accu = 0;
1155 size_t count; 1148 for (size_t count = r_len; count > 0; count--)
1156 for (count = r_len; count > 0; count--)
1157 { 1149 {
1158 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS; 1150 accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
1159 accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s); 1151 accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
@@ -1168,29 +1160,25 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
1168 /* Compare r << 1 with b. */ 1160 /* Compare r << 1 with b. */
1169 if (r_len > b_len) 1161 if (r_len > b_len)
1170 goto increment_q; 1162 goto increment_q;
1171 { 1163 for (size_t i = b_len;;)
1172 size_t i; 1164 {
1173 for (i = b_len;;) 1165 mp_limb_t r_i =
1174 { 1166 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
1175 mp_limb_t r_i = 1167 | (i < r_len ? r_ptr[i] << 1 : 0);
1176 (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0) 1168 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
1177 | (i < r_len ? r_ptr[i] << 1 : 0); 1169 if (r_i > b_i)
1178 mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0); 1170 goto increment_q;
1179 if (r_i > b_i) 1171 if (r_i < b_i)
1180 goto increment_q; 1172 goto keep_q;
1181 if (r_i < b_i) 1173 if (i == 0)
1182 goto keep_q; 1174 break;
1183 if (i == 0) 1175 i--;
1184 break; 1176 }
1185 i--;
1186 }
1187 }
1188 if (q_len > 0 && ((q_ptr[0] & 1) != 0)) 1177 if (q_len > 0 && ((q_ptr[0] & 1) != 0))
1189 /* q is odd. */ 1178 /* q is odd. */
1190 increment_q: 1179 increment_q:
1191 { 1180 {
1192 size_t i; 1181 for (size_t i = 0; i < q_len; i++)
1193 for (i = 0; i < q_len; i++)
1194 if (++(q_ptr[i]) != 0) 1182 if (++(q_ptr[i]) != 0)
1195 goto keep_q; 1183 goto keep_q;
1196 q_ptr[q_len++] = 1; 1184 q_ptr[q_len++] = 1;
@@ -1236,8 +1224,7 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes)
1236 /* Divide a by 10^9, in-place. */ 1224 /* Divide a by 10^9, in-place. */
1237 mp_limb_t remainder = 0; 1225 mp_limb_t remainder = 0;
1238 mp_limb_t *ptr = a_ptr + a_len; 1226 mp_limb_t *ptr = a_ptr + a_len;
1239 size_t count; 1227 for (size_t count = a_len; count > 0; count--)
1240 for (count = a_len; count > 0; count--)
1241 { 1228 {
1242 mp_twolimb_t num = 1229 mp_twolimb_t num =
1243 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr; 1230 ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
@@ -1245,7 +1232,7 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes)
1245 remainder = num % 1000000000; 1232 remainder = num % 1000000000;
1246 } 1233 }
1247 /* Store the remainder as 9 decimal digits. */ 1234 /* Store the remainder as 9 decimal digits. */
1248 for (count = 9; count > 0; count--) 1235 for (size_t count = 9; count > 0; count--)
1249 { 1236 {
1250 *d_ptr++ = '0' + (remainder % 10); 1237 *d_ptr++ = '0' + (remainder % 10);
1251 remainder = remainder / 10; 1238 remainder = remainder / 10;
@@ -1279,18 +1266,15 @@ convert_to_decimal (mpn_t a, size_t extra_zeroes)
1279static void * 1266static void *
1280decode_long_double (long double x, int *ep, mpn_t *mp) 1267decode_long_double (long double x, int *ep, mpn_t *mp)
1281{ 1268{
1282 mpn_t m;
1283 int exp;
1284 long double y;
1285 size_t i;
1286
1287 /* Allocate memory for result. */ 1269 /* Allocate memory for result. */
1270 mpn_t m;
1288 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; 1271 m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
1289 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); 1272 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
1290 if (m.limbs == NULL) 1273 if (m.limbs == NULL)
1291 return NULL; 1274 return NULL;
1292 /* Split into exponential part and mantissa. */ 1275 /* Split into exponential part and mantissa. */
1293 y = safe_frexpl (x, &exp); 1276 int exp;
1277 long double y = safe_frexpl (x, &exp);
1294 if (!(y >= 0.0L && y < 1.0L)) 1278 if (!(y >= 0.0L && y < 1.0L))
1295 abort (); 1279 abort ();
1296 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the 1280 /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
@@ -1328,7 +1312,7 @@ decode_long_double (long double x, int *ep, mpn_t *mp)
1328 } 1312 }
1329# endif 1313# endif
1330# endif 1314# endif
1331 for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) 1315 for (size_t i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
1332 { 1316 {
1333 mp_limb_t hi, lo; 1317 mp_limb_t hi, lo;
1334 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); 1318 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
@@ -1367,18 +1351,15 @@ decode_long_double (long double x, int *ep, mpn_t *mp)
1367static void * 1351static void *
1368decode_double (double x, int *ep, mpn_t *mp) 1352decode_double (double x, int *ep, mpn_t *mp)
1369{ 1353{
1370 mpn_t m;
1371 int exp;
1372 double y;
1373 size_t i;
1374
1375 /* Allocate memory for result. */ 1354 /* Allocate memory for result. */
1355 mpn_t m;
1376 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; 1356 m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
1377 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); 1357 m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
1378 if (m.limbs == NULL) 1358 if (m.limbs == NULL)
1379 return NULL; 1359 return NULL;
1380 /* Split into exponential part and mantissa. */ 1360 /* Split into exponential part and mantissa. */
1381 y = frexp (x, &exp); 1361 int exp;
1362 double y = frexp (x, &exp);
1382 if (!(y >= 0.0 && y < 1.0)) 1363 if (!(y >= 0.0 && y < 1.0))
1383 abort (); 1364 abort ();
1384 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the 1365 /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
@@ -1416,7 +1397,7 @@ decode_double (double x, int *ep, mpn_t *mp)
1416 } 1397 }
1417# endif 1398# endif
1418# endif 1399# endif
1419 for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; ) 1400 for (size_t i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
1420 { 1401 {
1421 mp_limb_t hi, lo; 1402 mp_limb_t hi, lo;
1422 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); 1403 y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
@@ -1451,24 +1432,11 @@ decode_double (double x, int *ep, mpn_t *mp)
1451static char * 1432static char *
1452scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) 1433scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1453{ 1434{
1454 int s;
1455 size_t extra_zeroes;
1456 unsigned int abs_n;
1457 unsigned int abs_s;
1458 mp_limb_t *pow5_ptr;
1459 size_t pow5_len;
1460 unsigned int s_limbs;
1461 unsigned int s_bits;
1462 mpn_t pow5;
1463 mpn_t z;
1464 void *z_memory;
1465 char *digits;
1466
1467 /* x = 2^e * m, hence 1435 /* x = 2^e * m, hence
1468 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m) 1436 y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
1469 = round (2^s * 5^n * m). */ 1437 = round (2^s * 5^n * m). */
1470 s = e + n; 1438 int s = e + n;
1471 extra_zeroes = 0; 1439 size_t extra_zeroes = 0;
1472 /* Factor out a common power of 10 if possible. */ 1440 /* Factor out a common power of 10 if possible. */
1473 if (s > 0 && n > 0) 1441 if (s > 0 && n > 0)
1474 { 1442 {
@@ -1481,11 +1449,12 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1481 z = round (2^s * 5^n * m). */ 1449 z = round (2^s * 5^n * m). */
1482 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same 1450 /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1483 sign. 2.322 is slightly larger than log(5)/log(2). */ 1451 sign. 2.322 is slightly larger than log(5)/log(2). */
1484 abs_n = (n >= 0 ? n : -n); 1452 unsigned int abs_n = (n >= 0 ? n : -n);
1485 abs_s = (s >= 0 ? s : -s); 1453 unsigned int abs_s = (s >= 0 ? s : -s);
1486 pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1 1454 mp_limb_t *pow5_ptr =
1487 + abs_s / GMP_LIMB_BITS + 1) 1455 (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
1488 * sizeof (mp_limb_t)); 1456 + abs_s / GMP_LIMB_BITS + 1)
1457 * sizeof (mp_limb_t));
1489 if (pow5_ptr == NULL) 1458 if (pow5_ptr == NULL)
1490 { 1459 {
1491 free (memory); 1460 free (memory);
@@ -1493,7 +1462,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1493 } 1462 }
1494 /* Initialize with 1. */ 1463 /* Initialize with 1. */
1495 pow5_ptr[0] = 1; 1464 pow5_ptr[0] = 1;
1496 pow5_len = 1; 1465 size_t pow5_len = 1;
1497 /* Multiply with 5^|n|. */ 1466 /* Multiply with 5^|n|. */
1498 if (abs_n > 0) 1467 if (abs_n > 0)
1499 { 1468 {
@@ -1502,13 +1471,11 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1502 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 1471 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1503 48828125, 244140625, 1220703125 1472 48828125, 244140625, 1220703125
1504 }; 1473 };
1505 unsigned int n13; 1474 for (unsigned int n13 = 0; n13 <= abs_n; n13 += 13)
1506 for (n13 = 0; n13 <= abs_n; n13 += 13)
1507 { 1475 {
1508 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13]; 1476 mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1509 size_t j;
1510 mp_twolimb_t carry = 0; 1477 mp_twolimb_t carry = 0;
1511 for (j = 0; j < pow5_len; j++) 1478 for (size_t j = 0; j < pow5_len; j++)
1512 { 1479 {
1513 mp_limb_t digit2 = pow5_ptr[j]; 1480 mp_limb_t digit2 = pow5_ptr[j];
1514 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2; 1481 carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
@@ -1519,8 +1486,11 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1519 pow5_ptr[pow5_len++] = (mp_limb_t) carry; 1486 pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1520 } 1487 }
1521 } 1488 }
1522 s_limbs = abs_s / GMP_LIMB_BITS; 1489 unsigned int s_limbs = abs_s / GMP_LIMB_BITS;
1523 s_bits = abs_s % GMP_LIMB_BITS; 1490 unsigned int s_bits = abs_s % GMP_LIMB_BITS;
1491 mpn_t pow5;
1492 mpn_t z;
1493 void *z_memory;
1524 if (n >= 0 ? s >= 0 : s <= 0) 1494 if (n >= 0 ? s >= 0 : s <= 0)
1525 { 1495 {
1526 /* Multiply with 2^|s|. */ 1496 /* Multiply with 2^|s|. */
@@ -1528,8 +1498,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1528 { 1498 {
1529 mp_limb_t *ptr = pow5_ptr; 1499 mp_limb_t *ptr = pow5_ptr;
1530 mp_twolimb_t accu = 0; 1500 mp_twolimb_t accu = 0;
1531 size_t count; 1501 for (size_t count = pow5_len; count > 0; count--)
1532 for (count = pow5_len; count > 0; count--)
1533 { 1502 {
1534 accu += (mp_twolimb_t) *ptr << s_bits; 1503 accu += (mp_twolimb_t) *ptr << s_bits;
1535 *ptr++ = (mp_limb_t) accu; 1504 *ptr++ = (mp_limb_t) accu;
@@ -1543,13 +1512,12 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1543 } 1512 }
1544 if (s_limbs > 0) 1513 if (s_limbs > 0)
1545 { 1514 {
1546 size_t count; 1515 for (size_t count = pow5_len; count > 0;)
1547 for (count = pow5_len; count > 0;)
1548 { 1516 {
1549 count--; 1517 count--;
1550 pow5_ptr[s_limbs + count] = pow5_ptr[count]; 1518 pow5_ptr[s_limbs + count] = pow5_ptr[count];
1551 } 1519 }
1552 for (count = s_limbs; count > 0;) 1520 for (size_t count = s_limbs; count > 0;)
1553 { 1521 {
1554 count--; 1522 count--;
1555 pow5_ptr[count] = 0; 1523 pow5_ptr[count] = 0;
@@ -1590,8 +1558,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1590 /* Construct 2^|s|. */ 1558 /* Construct 2^|s|. */
1591 { 1559 {
1592 mp_limb_t *ptr = pow5_ptr + pow5_len; 1560 mp_limb_t *ptr = pow5_ptr + pow5_len;
1593 size_t i; 1561 for (size_t i = 0; i < s_limbs; i++)
1594 for (i = 0; i < s_limbs; i++)
1595 ptr[i] = 0; 1562 ptr[i] = 0;
1596 ptr[s_limbs] = (mp_limb_t) 1 << s_bits; 1563 ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1597 denominator.limbs = ptr; 1564 denominator.limbs = ptr;
@@ -1616,17 +1583,13 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1616 } 1583 }
1617 { 1584 {
1618 mp_limb_t *destptr = num_ptr; 1585 mp_limb_t *destptr = num_ptr;
1619 { 1586 for (size_t i = 0; i < s_limbs; i++)
1620 size_t i; 1587 *destptr++ = 0;
1621 for (i = 0; i < s_limbs; i++)
1622 *destptr++ = 0;
1623 }
1624 if (s_bits > 0) 1588 if (s_bits > 0)
1625 { 1589 {
1626 const mp_limb_t *sourceptr = m.limbs; 1590 const mp_limb_t *sourceptr = m.limbs;
1627 mp_twolimb_t accu = 0; 1591 mp_twolimb_t accu = 0;
1628 size_t count; 1592 for (size_t count = m.nlimbs; count > 0; count--)
1629 for (count = m.nlimbs; count > 0; count--)
1630 { 1593 {
1631 accu += (mp_twolimb_t) *sourceptr++ << s_bits; 1594 accu += (mp_twolimb_t) *sourceptr++ << s_bits;
1632 *destptr++ = (mp_limb_t) accu; 1595 *destptr++ = (mp_limb_t) accu;
@@ -1638,8 +1601,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1638 else 1601 else
1639 { 1602 {
1640 const mp_limb_t *sourceptr = m.limbs; 1603 const mp_limb_t *sourceptr = m.limbs;
1641 size_t count; 1604 for (size_t count = m.nlimbs; count > 0; count--)
1642 for (count = m.nlimbs; count > 0; count--)
1643 *destptr++ = *sourceptr++; 1605 *destptr++ = *sourceptr++;
1644 } 1606 }
1645 numerator.limbs = num_ptr; 1607 numerator.limbs = num_ptr;
@@ -1656,7 +1618,7 @@ scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
1656 1618
1657 if (z_memory == NOMEM_PTR) 1619 if (z_memory == NOMEM_PTR)
1658 return NULL; 1620 return NULL;
1659 digits = convert_to_decimal (z, extra_zeroes); 1621 char *digits = convert_to_decimal (z, extra_zeroes);
1660 free (z_memory); 1622 free (z_memory);
1661 return digits; 1623 return digits;
1662} 1624}
@@ -1711,13 +1673,9 @@ scale10_round_decimal_double (double x, int n)
1711static int 1673static int
1712floorlog10l (long double x) 1674floorlog10l (long double x)
1713{ 1675{
1714 int exp;
1715 long double y;
1716 double z;
1717 double l;
1718
1719 /* Split into exponential part and mantissa. */ 1676 /* Split into exponential part and mantissa. */
1720 y = safe_frexpl (x, &exp); 1677 int exp;
1678 long double y = safe_frexpl (x, &exp);
1721 if (!(y >= 0.0L && y < 1.0L)) 1679 if (!(y >= 0.0L && y < 1.0L))
1722 abort (); 1680 abort ();
1723 if (y == 0.0L) 1681 if (y == 0.0L)
@@ -1758,8 +1716,8 @@ floorlog10l (long double x)
1758 if (!(y >= 0.5L && y < 1.0L)) 1716 if (!(y >= 0.5L && y < 1.0L))
1759 abort (); 1717 abort ();
1760 /* Compute an approximation for l = log2(x) = exp + log2(y). */ 1718 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1761 l = exp; 1719 double l = exp;
1762 z = y; 1720 double z = y;
1763 if (z < 0.70710678118654752444) 1721 if (z < 0.70710678118654752444)
1764 { 1722 {
1765 z *= 1.4142135623730950488; 1723 z *= 1.4142135623730950488;
@@ -1802,13 +1760,9 @@ floorlog10l (long double x)
1802static int 1760static int
1803floorlog10 (double x) 1761floorlog10 (double x)
1804{ 1762{
1805 int exp;
1806 double y;
1807 double z;
1808 double l;
1809
1810 /* Split into exponential part and mantissa. */ 1763 /* Split into exponential part and mantissa. */
1811 y = frexp (x, &exp); 1764 int exp;
1765 double y = frexp (x, &exp);
1812 if (!(y >= 0.0 && y < 1.0)) 1766 if (!(y >= 0.0 && y < 1.0))
1813 abort (); 1767 abort ();
1814 if (y == 0.0) 1768 if (y == 0.0)
@@ -1849,8 +1803,8 @@ floorlog10 (double x)
1849 if (!(y >= 0.5 && y < 1.0)) 1803 if (!(y >= 0.5 && y < 1.0))
1850 abort (); 1804 abort ();
1851 /* Compute an approximation for l = log2(x) = exp + log2(y). */ 1805 /* Compute an approximation for l = log2(x) = exp + log2(y). */
1852 l = exp; 1806 double l = exp;
1853 z = y; 1807 double z = y;
1854 if (z < 0.70710678118654752444) 1808 if (z < 0.70710678118654752444)
1855 { 1809 {
1856 z *= 1.4142135623730950488; 1810 z *= 1.4142135623730950488;
@@ -2513,21 +2467,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2513 goto fail_1_with_EINVAL; 2467 goto fail_1_with_EINVAL;
2514 2468
2515 { 2469 {
2516 size_t buf_neededlength;
2517 TCHAR_T *buf;
2518 TCHAR_T *buf_malloced;
2519 const FCHAR_T *cp;
2520 size_t di;
2521 DIRECTIVE *dp;
2522 /* Output string accumulator. */
2523 DCHAR_T *result;
2524 size_t allocated;
2525 size_t length;
2526
2527 /* Allocate a small buffer that will hold a directive passed to 2470 /* Allocate a small buffer that will hold a directive passed to
2528 sprintf or snprintf. */ 2471 sprintf or snprintf. */
2529 buf_neededlength = 2472 size_t buf_neededlength =
2530 xsum4 (7, d.max_width_length, d.max_precision_length, 6); 2473 xsum4 (7, d.max_width_length, d.max_precision_length, 6);
2474 TCHAR_T *buf;
2475 TCHAR_T *buf_malloced;
2531#if HAVE_ALLOCA 2476#if HAVE_ALLOCA
2532 if (buf_neededlength < 4000 / sizeof (TCHAR_T)) 2477 if (buf_neededlength < 4000 / sizeof (TCHAR_T))
2533 { 2478 {
@@ -2546,9 +2491,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2546 buf_malloced = buf; 2491 buf_malloced = buf;
2547 } 2492 }
2548 2493
2549 result = resultbuf; 2494 /* Output string accumulator. */
2550 allocated = (resultbuf != NULL ? *lengthp : 0); 2495 DCHAR_T *result = resultbuf;
2551 length = 0; 2496 size_t allocated = (resultbuf != NULL ? *lengthp : 0);
2497 size_t length = 0;
2498
2552 /* Invariants: 2499 /* Invariants:
2553 result is either == resultbuf or malloc-allocated. 2500 result is either == resultbuf or malloc-allocated.
2554 If result == NULL, resultbuf is == NULL as well. 2501 If result == NULL, resultbuf is == NULL as well.
@@ -2559,15 +2506,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2559#define ENSURE_ALLOCATION_ELSE(needed, oom_statement) \ 2506#define ENSURE_ALLOCATION_ELSE(needed, oom_statement) \
2560 if ((needed) > allocated) \ 2507 if ((needed) > allocated) \
2561 { \ 2508 { \
2562 size_t memory_size; \
2563 DCHAR_T *memory; \
2564 \
2565 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \ 2509 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
2566 if ((needed) > allocated) \ 2510 if ((needed) > allocated) \
2567 allocated = (needed); \ 2511 allocated = (needed); \
2568 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \ 2512 size_t memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
2569 if (size_overflow_p (memory_size)) \ 2513 if (size_overflow_p (memory_size)) \
2570 oom_statement \ 2514 oom_statement \
2515 DCHAR_T *memory; \
2571 if (result == resultbuf) \ 2516 if (result == resultbuf) \
2572 memory = (DCHAR_T *) malloc (memory_size); \ 2517 memory = (DCHAR_T *) malloc (memory_size); \
2573 else \ 2518 else \
@@ -2581,6 +2526,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2581#define ENSURE_ALLOCATION(needed) \ 2526#define ENSURE_ALLOCATION(needed) \
2582 ENSURE_ALLOCATION_ELSE((needed), goto out_of_memory; ) 2527 ENSURE_ALLOCATION_ELSE((needed), goto out_of_memory; )
2583 2528
2529 const FCHAR_T *cp;
2530 size_t di;
2531 DIRECTIVE *dp;
2584 for (cp = format, di = 0, dp = &d.dir[0]; ; cp = dp->dir_end, di++, dp++) 2532 for (cp = format, di = 0, dp = &d.dir[0]; ; cp = dp->dir_end, di++, dp++)
2585 { 2533 {
2586 if (cp != dp->dir_start) 2534 if (cp != dp->dir_start)
@@ -2681,22 +2629,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2681 { 2629 {
2682 arg_type type = a.arg[dp->arg_index].type; 2630 arg_type type = a.arg[dp->arg_index].type;
2683 int flags = dp->flags; 2631 int flags = dp->flags;
2684 int has_width;
2685 size_t width;
2686 int has_precision;
2687 size_t precision;
2688 2632
2689 has_width = 0; 2633 int has_width = 0;
2690 width = 0; 2634 size_t width = 0;
2691 if (dp->width_start != dp->width_end) 2635 if (dp->width_start != dp->width_end)
2692 { 2636 {
2693 if (dp->width_arg_index != ARG_NONE) 2637 if (dp->width_arg_index != ARG_NONE)
2694 { 2638 {
2695 int arg;
2696
2697 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 2639 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2698 abort (); 2640 abort ();
2699 arg = a.arg[dp->width_arg_index].a.a_int; 2641 int arg = a.arg[dp->width_arg_index].a.a_int;
2700 width = arg; 2642 width = arg;
2701 if (arg < 0) 2643 if (arg < 0)
2702 { 2644 {
@@ -2719,17 +2661,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2719 has_width = 1; 2661 has_width = 1;
2720 } 2662 }
2721 2663
2722 has_precision = 0; 2664 int has_precision = 0;
2723 precision = 0; 2665 size_t precision = 0;
2724 if (dp->precision_start != dp->precision_end) 2666 if (dp->precision_start != dp->precision_end)
2725 { 2667 {
2726 if (dp->precision_arg_index != ARG_NONE) 2668 if (dp->precision_arg_index != ARG_NONE)
2727 { 2669 {
2728 int arg;
2729
2730 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 2670 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2731 abort (); 2671 abort ();
2732 arg = a.arg[dp->precision_arg_index].a.a_int; 2672 int arg = a.arg[dp->precision_arg_index].a.a_int;
2733 /* "A negative precision is taken as if the precision 2673 /* "A negative precision is taken as if the precision
2734 were omitted." */ 2674 were omitted." */
2735 if (arg >= 0) 2675 if (arg >= 0)
@@ -3074,22 +3014,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3074 than INT_MAX characters, whence snprintf or sprintf would 3014 than INT_MAX characters, whence snprintf or sprintf would
3075 fail to process it. */ 3015 fail to process it. */
3076 int flags = dp->flags; 3016 int flags = dp->flags;
3077 int has_width;
3078 size_t width;
3079 int has_precision;
3080 size_t precision;
3081 3017
3082 has_width = 0; 3018 int has_width = 0;
3083 width = 0; 3019 size_t width = 0;
3084 if (dp->width_start != dp->width_end) 3020 if (dp->width_start != dp->width_end)
3085 { 3021 {
3086 if (dp->width_arg_index != ARG_NONE) 3022 if (dp->width_arg_index != ARG_NONE)
3087 { 3023 {
3088 int arg;
3089
3090 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 3024 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3091 abort (); 3025 abort ();
3092 arg = a.arg[dp->width_arg_index].a.a_int; 3026 int arg = a.arg[dp->width_arg_index].a.a_int;
3093 width = arg; 3027 width = arg;
3094 if (arg < 0) 3028 if (arg < 0)
3095 { 3029 {
@@ -3112,17 +3046,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3112 has_width = 1; 3046 has_width = 1;
3113 } 3047 }
3114 3048
3115 has_precision = 0; 3049 int has_precision = 0;
3116 precision = 6; 3050 size_t precision = 6;
3117 if (dp->precision_start != dp->precision_end) 3051 if (dp->precision_start != dp->precision_end)
3118 { 3052 {
3119 if (dp->precision_arg_index != ARG_NONE) 3053 if (dp->precision_arg_index != ARG_NONE)
3120 { 3054 {
3121 int arg;
3122
3123 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 3055 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3124 abort (); 3056 abort ();
3125 arg = a.arg[dp->precision_arg_index].a.a_int; 3057 int arg = a.arg[dp->precision_arg_index].a.a_int;
3126 /* "A negative precision is taken as if the precision 3058 /* "A negative precision is taken as if the precision
3127 were omitted." */ 3059 were omitted." */
3128 if (arg >= 0) 3060 if (arg >= 0)
@@ -3144,18 +3076,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3144 3076
3145 { 3077 {
3146 const char *arg = a.arg[dp->arg_index].a.a_string; 3078 const char *arg = a.arg[dp->arg_index].a.a_string;
3147 size_t bytes;
3148# if ENABLE_UNISTDIO && DCHAR_IS_TCHAR
3149 size_t characters;
3150# endif
3151# if !DCHAR_IS_TCHAR
3152 /* This code assumes that TCHAR_T is 'char'. */
3153 static_assert (sizeof (TCHAR_T) == 1);
3154 DCHAR_T *tmpdst;
3155 size_t tmpdst_len;
3156# endif
3157 size_t w;
3158 3079
3080 size_t bytes;
3159 if (has_precision) 3081 if (has_precision)
3160 { 3082 {
3161 /* Use only at most PRECISION bytes, from the left. */ 3083 /* Use only at most PRECISION bytes, from the left. */
@@ -3169,6 +3091,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3169 } 3091 }
3170 3092
3171# if ENABLE_UNISTDIO && DCHAR_IS_TCHAR 3093# if ENABLE_UNISTDIO && DCHAR_IS_TCHAR
3094 size_t characters;
3172 if (has_width) 3095 if (has_width)
3173 characters = mbsnlen (arg, bytes); 3096 characters = mbsnlen (arg, bytes);
3174 else 3097 else
@@ -3180,6 +3103,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3180# endif 3103# endif
3181 3104
3182# if !DCHAR_IS_TCHAR 3105# if !DCHAR_IS_TCHAR
3106 /* This code assumes that TCHAR_T is 'char'. */
3107 static_assert (sizeof (TCHAR_T) == 1);
3108 DCHAR_T *tmpdst;
3109 size_t tmpdst_len;
3183 /* Convert from TCHAR_T[] to DCHAR_T[]. */ 3110 /* Convert from TCHAR_T[] to DCHAR_T[]. */
3184 tmpdst = 3111 tmpdst =
3185 DCHAR_CONV_FROM_ENCODING (locale_charset (), 3112 DCHAR_CONV_FROM_ENCODING (locale_charset (),
@@ -3191,6 +3118,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3191 goto fail_with_errno; 3118 goto fail_with_errno;
3192# endif 3119# endif
3193 3120
3121 size_t w;
3194 if (has_width) 3122 if (has_width)
3195 { 3123 {
3196# if ENABLE_UNISTDIO 3124# if ENABLE_UNISTDIO
@@ -3260,18 +3188,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3260 convert back the result from a char[] to a wchar_t[]. 3188 convert back the result from a char[] to a wchar_t[].
3261 Instead, just copy the argument wchar_t[] to the result. */ 3189 Instead, just copy the argument wchar_t[] to the result. */
3262 int flags = dp->flags; 3190 int flags = dp->flags;
3263 size_t width;
3264 3191
3265 width = 0; 3192 size_t width = 0;
3266 if (dp->width_start != dp->width_end) 3193 if (dp->width_start != dp->width_end)
3267 { 3194 {
3268 if (dp->width_arg_index != ARG_NONE) 3195 if (dp->width_arg_index != ARG_NONE)
3269 { 3196 {
3270 int arg;
3271
3272 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 3197 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3273 abort (); 3198 abort ();
3274 arg = a.arg[dp->width_arg_index].a.a_int; 3199 int arg = a.arg[dp->width_arg_index].a.a_int;
3275 width = arg; 3200 width = arg;
3276 if (arg < 0) 3201 if (arg < 0)
3277 { 3202 {
@@ -3300,20 +3225,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3300 3225
3301 if (dp->conversion == 's') 3226 if (dp->conversion == 's')
3302 { 3227 {
3303 int has_precision; 3228 int has_precision = 0;
3304 size_t precision; 3229 size_t precision = 6;
3305
3306 has_precision = 0;
3307 precision = 6;
3308 if (dp->precision_start != dp->precision_end) 3230 if (dp->precision_start != dp->precision_end)
3309 { 3231 {
3310 if (dp->precision_arg_index != ARG_NONE) 3232 if (dp->precision_arg_index != ARG_NONE)
3311 { 3233 {
3312 int arg;
3313
3314 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 3234 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3315 abort (); 3235 abort ();
3316 arg = a.arg[dp->precision_arg_index].a.a_int; 3236 int arg = a.arg[dp->precision_arg_index].a.a_int;
3317 /* "A negative precision is taken as if the precision 3237 /* "A negative precision is taken as if the precision
3318 were omitted." */ 3238 were omitted." */
3319 if (arg >= 0) 3239 if (arg >= 0)
@@ -3339,9 +3259,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3339 { 3259 {
3340 /* Use only at most PRECISION wide characters, from 3260 /* Use only at most PRECISION wide characters, from
3341 the left. */ 3261 the left. */
3342 const wchar_t *ls_arg_end; 3262 const wchar_t *ls_arg_end = ls_arg;
3343
3344 ls_arg_end = ls_arg;
3345 characters = 0; 3263 characters = 0;
3346 for (; precision > 0; precision--) 3264 for (; precision > 0; precision--)
3347 { 3265 {
@@ -3413,22 +3331,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3413 such bugs, we implement the entire processing of the 's' 3331 such bugs, we implement the entire processing of the 's'
3414 directive ourselves. */ 3332 directive ourselves. */
3415 int flags = dp->flags; 3333 int flags = dp->flags;
3416 int has_width;
3417 size_t width;
3418 int has_precision;
3419 size_t precision;
3420 3334
3421 has_width = 0; 3335 int has_width = 0;
3422 width = 0; 3336 size_t width = 0;
3423 if (dp->width_start != dp->width_end) 3337 if (dp->width_start != dp->width_end)
3424 { 3338 {
3425 if (dp->width_arg_index != ARG_NONE) 3339 if (dp->width_arg_index != ARG_NONE)
3426 { 3340 {
3427 int arg;
3428
3429 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 3341 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3430 abort (); 3342 abort ();
3431 arg = a.arg[dp->width_arg_index].a.a_int; 3343 int arg = a.arg[dp->width_arg_index].a.a_int;
3432 width = arg; 3344 width = arg;
3433 if (arg < 0) 3345 if (arg < 0)
3434 { 3346 {
@@ -3451,17 +3363,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3451 has_width = 1; 3363 has_width = 1;
3452 } 3364 }
3453 3365
3454 has_precision = 0; 3366 int has_precision = 0;
3455 precision = 6; 3367 size_t precision = 6;
3456 if (dp->precision_start != dp->precision_end) 3368 if (dp->precision_start != dp->precision_end)
3457 { 3369 {
3458 if (dp->precision_arg_index != ARG_NONE) 3370 if (dp->precision_arg_index != ARG_NONE)
3459 { 3371 {
3460 int arg;
3461
3462 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 3372 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3463 abort (); 3373 abort ();
3464 arg = a.arg[dp->precision_arg_index].a.a_int; 3374 int arg = a.arg[dp->precision_arg_index].a.a_int;
3465 /* "A negative precision is taken as if the precision 3375 /* "A negative precision is taken as if the precision
3466 were omitted." */ 3376 were omitted." */
3467 if (arg >= 0) 3377 if (arg >= 0)
@@ -3563,13 +3473,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3563 if (has_precision || has_width) 3473 if (has_precision || has_width)
3564 { 3474 {
3565 /* We know the number of wide characters in advance. */ 3475 /* We know the number of wide characters in advance. */
3566 size_t remaining;
3567# if HAVE_MBRTOWC 3476# if HAVE_MBRTOWC
3568 mbstate_t state; 3477 mbstate_t state;
3569 mbszero (&state); 3478 mbszero (&state);
3570# endif 3479# endif
3571 ENSURE_ALLOCATION (xsum (length, characters)); 3480 ENSURE_ALLOCATION (xsum (length, characters));
3572 for (remaining = characters; remaining > 0; remaining--) 3481 for (size_t remaining = characters; remaining > 0; remaining--)
3573 { 3482 {
3574 wchar_t wc; 3483 wchar_t wc;
3575 int count; 3484 int count;
@@ -3655,13 +3564,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3655# endif 3564# endif
3656 while (precision > 0) 3565 while (precision > 0)
3657 { 3566 {
3658 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3659 int count;
3660
3661 if (*arg_end == 0) 3567 if (*arg_end == 0)
3662 /* Found the terminating null wide character. */ 3568 /* Found the terminating null wide character. */
3663 break; 3569 break;
3664 count = local_wcrtomb (cbuf, *arg_end, &state); 3570 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3571 int count = local_wcrtomb (cbuf, *arg_end, &state);
3665 if (count < 0) 3572 if (count < 0)
3666 /* Cannot convert. */ 3573 /* Cannot convert. */
3667 goto fail_with_EILSEQ; 3574 goto fail_with_EILSEQ;
@@ -3694,13 +3601,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3694# endif 3601# endif
3695 for (;;) 3602 for (;;)
3696 { 3603 {
3697 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3698 int count;
3699
3700 if (*arg_end == 0) 3604 if (*arg_end == 0)
3701 /* Found the terminating null wide character. */ 3605 /* Found the terminating null wide character. */
3702 break; 3606 break;
3703 count = local_wcrtomb (cbuf, *arg_end, &state); 3607 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3608 int count = local_wcrtomb (cbuf, *arg_end, &state);
3704 if (count < 0) 3609 if (count < 0)
3705 /* Cannot convert. */ 3610 /* Cannot convert. */
3706 goto fail_with_EILSEQ; 3611 goto fail_with_EILSEQ;
@@ -3727,27 +3632,22 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3727 3632
3728# if !DCHAR_IS_TCHAR 3633# if !DCHAR_IS_TCHAR
3729 { 3634 {
3730 TCHAR_T *tmpsrc;
3731
3732 /* Convert the string into a piece of temporary memory. */ 3635 /* Convert the string into a piece of temporary memory. */
3733 tmpsrc = (TCHAR_T *) malloc (bytes * sizeof (TCHAR_T)); 3636 TCHAR_T *tmpsrc = (TCHAR_T *) malloc (bytes * sizeof (TCHAR_T));
3734 if (tmpsrc == NULL) 3637 if (tmpsrc == NULL)
3735 goto out_of_memory; 3638 goto out_of_memory;
3736 { 3639 {
3737 TCHAR_T *tmpptr = tmpsrc; 3640 TCHAR_T *tmpptr = tmpsrc;
3738 size_t remaining;
3739# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t 3641# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3740 mbstate_t state; 3642 mbstate_t state;
3741 mbszero (&state); 3643 mbszero (&state);
3742# endif 3644# endif
3743 for (remaining = bytes; remaining > 0; ) 3645 for (size_t remaining = bytes; remaining > 0; )
3744 { 3646 {
3745 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3746 int count;
3747
3748 if (*arg == 0) 3647 if (*arg == 0)
3749 abort (); 3648 abort ();
3750 count = local_wcrtomb (cbuf, *arg, &state); 3649 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3650 int count = local_wcrtomb (cbuf, *arg, &state);
3751 if (count <= 0) 3651 if (count <= 0)
3752 /* Inconsistency. */ 3652 /* Inconsistency. */
3753 abort (); 3653 abort ();
@@ -3814,20 +3714,17 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3814 if (has_precision || has_width) 3714 if (has_precision || has_width)
3815 { 3715 {
3816 /* We know the number of bytes in advance. */ 3716 /* We know the number of bytes in advance. */
3817 size_t remaining;
3818# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t 3717# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3819 mbstate_t state; 3718 mbstate_t state;
3820 mbszero (&state); 3719 mbszero (&state);
3821# endif 3720# endif
3822 ENSURE_ALLOCATION (xsum (length, bytes)); 3721 ENSURE_ALLOCATION (xsum (length, bytes));
3823 for (remaining = bytes; remaining > 0; ) 3722 for (size_t remaining = bytes; remaining > 0; )
3824 { 3723 {
3825 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3826 int count;
3827
3828 if (*arg == 0) 3724 if (*arg == 0)
3829 abort (); 3725 abort ();
3830 count = local_wcrtomb (cbuf, *arg, &state); 3726 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3727 int count = local_wcrtomb (cbuf, *arg, &state);
3831 if (count <= 0) 3728 if (count <= 0)
3832 /* Inconsistency. */ 3729 /* Inconsistency. */
3833 abort (); 3730 abort ();
@@ -3847,12 +3744,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3847# endif 3744# endif
3848 while (arg < arg_end) 3745 while (arg < arg_end)
3849 { 3746 {
3850 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3851 int count;
3852
3853 if (*arg == 0) 3747 if (*arg == 0)
3854 abort (); 3748 abort ();
3855 count = local_wcrtomb (cbuf, *arg, &state); 3749 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3750 int count = local_wcrtomb (cbuf, *arg, &state);
3856 if (count <= 0) 3751 if (count <= 0)
3857 /* Cannot convert. */ 3752 /* Cannot convert. */
3858 goto fail_with_EILSEQ; 3753 goto fail_with_EILSEQ;
@@ -3889,20 +3784,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3889 a correct behaviour for the null wint_t argument and/or the 3784 a correct behaviour for the null wint_t argument and/or the
3890 fallback that avoids EILSEQ. */ 3785 fallback that avoids EILSEQ. */
3891 int flags = dp->flags; 3786 int flags = dp->flags;
3892 int has_width;
3893 size_t width;
3894 3787
3895 has_width = 0; 3788 int has_width = 0;
3896 width = 0; 3789 size_t width = 0;
3897 if (dp->width_start != dp->width_end) 3790 if (dp->width_start != dp->width_end)
3898 { 3791 {
3899 if (dp->width_arg_index != ARG_NONE) 3792 if (dp->width_arg_index != ARG_NONE)
3900 { 3793 {
3901 int arg;
3902
3903 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 3794 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3904 abort (); 3795 abort ();
3905 arg = a.arg[dp->width_arg_index].a.a_int; 3796 int arg = a.arg[dp->width_arg_index].a.a_int;
3906 width = arg; 3797 width = arg;
3907 if (arg < 0) 3798 if (arg < 0)
3908 { 3799 {
@@ -3945,14 +3836,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3945# endif 3836# endif
3946 { 3837 {
3947 /* Count the number of bytes. */ 3838 /* Count the number of bytes. */
3948 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3949 int count;
3950# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t 3839# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3951 mbstate_t state; 3840 mbstate_t state;
3952 mbszero (&state); 3841 mbszero (&state);
3953# endif 3842# endif
3954 3843
3955 count = local_wcrtomb (cbuf, arg, &state); 3844 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3845 int count = local_wcrtomb (cbuf, arg, &state);
3956 if (count < 0) 3846 if (count < 0)
3957 /* Cannot convert. */ 3847 /* Cannot convert. */
3958 goto fail_with_EILSEQ; 3848 goto fail_with_EILSEQ;
@@ -3980,14 +3870,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3980 /* Convert the string into a piece of temporary memory. */ 3870 /* Convert the string into a piece of temporary memory. */
3981 if (bytes > 0) 3871 if (bytes > 0)
3982 { 3872 {
3983 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3984 int count;
3985# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t 3873# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
3986 mbstate_t state; 3874 mbstate_t state;
3987 mbszero (&state); 3875 mbszero (&state);
3988# endif 3876# endif
3989 3877
3990 count = local_wcrtomb (cbuf, arg, &state); 3878 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3879 int count = local_wcrtomb (cbuf, arg, &state);
3991 if (count <= 0) 3880 if (count <= 0)
3992 /* Inconsistency. */ 3881 /* Inconsistency. */
3993 abort (); 3882 abort ();
@@ -4047,13 +3936,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4047 ENSURE_ALLOCATION (xsum (length, bytes)); 3936 ENSURE_ALLOCATION (xsum (length, bytes));
4048 if (bytes > 0) 3937 if (bytes > 0)
4049 { 3938 {
4050 int count;
4051# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t 3939# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
4052 mbstate_t state; 3940 mbstate_t state;
4053 mbszero (&state); 3941 mbszero (&state);
4054# endif 3942# endif
4055 3943
4056 count = local_wcrtomb (result + length, arg, &state); 3944 int count = local_wcrtomb (result + length, arg, &state);
4057 if (count <= 0) 3945 if (count <= 0)
4058 /* Inconsistency. */ 3946 /* Inconsistency. */
4059 abort (); 3947 abort ();
@@ -4062,14 +3950,13 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4062 } 3950 }
4063 else 3951 else
4064 { 3952 {
4065 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
4066 int count;
4067# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t 3953# if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
4068 mbstate_t state; 3954 mbstate_t state;
4069 mbszero (&state); 3955 mbszero (&state);
4070# endif 3956# endif
4071 3957
4072 count = local_wcrtomb (cbuf, arg, &state); 3958 char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
3959 int count = local_wcrtomb (cbuf, arg, &state);
4073 if (count < 0) 3960 if (count < 0)
4074 /* Cannot convert. */ 3961 /* Cannot convert. */
4075 goto fail_with_EILSEQ; 3962 goto fail_with_EILSEQ;
@@ -4102,18 +3989,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4102 /* Implement the 'c' directive ourselves, in order to avoid 3989 /* Implement the 'c' directive ourselves, in order to avoid
4103 EILSEQ in the "C" locale. */ 3990 EILSEQ in the "C" locale. */
4104 int flags = dp->flags; 3991 int flags = dp->flags;
4105 size_t width;
4106 3992
4107 width = 0; 3993 size_t width = 0;
4108 if (dp->width_start != dp->width_end) 3994 if (dp->width_start != dp->width_end)
4109 { 3995 {
4110 if (dp->width_arg_index != ARG_NONE) 3996 if (dp->width_arg_index != ARG_NONE)
4111 { 3997 {
4112 int arg;
4113
4114 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 3998 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4115 abort (); 3999 abort ();
4116 arg = a.arg[dp->width_arg_index].a.a_int; 4000 int arg = a.arg[dp->width_arg_index].a.a_int;
4117 width = arg; 4001 width = arg;
4118 if (arg < 0) 4002 if (arg < 0)
4119 { 4003 {
@@ -4139,9 +4023,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4139 { 4023 {
4140 char arg = (char) a.arg[dp->arg_index].a.a_char; 4024 char arg = (char) a.arg[dp->arg_index].a.a_char;
4141 mbstate_t state; 4025 mbstate_t state;
4142 wchar_t wc;
4143
4144 mbszero (&state); 4026 mbszero (&state);
4027
4028 wchar_t wc;
4145 int count = mbrtowc (&wc, &arg, 1, &state); 4029 int count = mbrtowc (&wc, &arg, 1, &state);
4146 if (count < 0) 4030 if (count < 0)
4147 /* Invalid or incomplete multibyte character. */ 4031 /* Invalid or incomplete multibyte character. */
@@ -4182,30 +4066,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4182 { 4066 {
4183 arg_type type = a.arg[dp->arg_index].type; 4067 arg_type type = a.arg[dp->arg_index].type;
4184 int flags = dp->flags; 4068 int flags = dp->flags;
4185 int has_width;
4186 size_t width;
4187 int has_precision;
4188 size_t precision;
4189 size_t tmp_length;
4190 size_t count;
4191 DCHAR_T tmpbuf[700];
4192 DCHAR_T *tmp;
4193 DCHAR_T *tmp_end;
4194 DCHAR_T *tmp_start;
4195 DCHAR_T *pad_ptr;
4196 DCHAR_T *p;
4197 4069
4198 has_width = 0; 4070 int has_width = 0;
4199 width = 0; 4071 size_t width = 0;
4200 if (dp->width_start != dp->width_end) 4072 if (dp->width_start != dp->width_end)
4201 { 4073 {
4202 if (dp->width_arg_index != ARG_NONE) 4074 if (dp->width_arg_index != ARG_NONE)
4203 { 4075 {
4204 int arg;
4205
4206 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 4076 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4207 abort (); 4077 abort ();
4208 arg = a.arg[dp->width_arg_index].a.a_int; 4078 int arg = a.arg[dp->width_arg_index].a.a_int;
4209 width = arg; 4079 width = arg;
4210 if (arg < 0) 4080 if (arg < 0)
4211 { 4081 {
@@ -4228,17 +4098,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4228 has_width = 1; 4098 has_width = 1;
4229 } 4099 }
4230 4100
4231 has_precision = 0; 4101 int has_precision = 0;
4232 precision = 1; 4102 size_t precision = 1;
4233 if (dp->precision_start != dp->precision_end) 4103 if (dp->precision_start != dp->precision_end)
4234 { 4104 {
4235 if (dp->precision_arg_index != ARG_NONE) 4105 if (dp->precision_arg_index != ARG_NONE)
4236 { 4106 {
4237 int arg;
4238
4239 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 4107 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4240 abort (); 4108 abort ();
4241 arg = a.arg[dp->precision_arg_index].a.a_int; 4109 int arg = a.arg[dp->precision_arg_index].a.a_int;
4242 /* "A negative precision is taken as if the precision 4110 /* "A negative precision is taken as if the precision
4243 were omitted." */ 4111 were omitted." */
4244 if (arg >= 0) 4112 if (arg >= 0)
@@ -4259,6 +4127,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4259 } 4127 }
4260 4128
4261 /* Allocate a temporary buffer of sufficient size. */ 4129 /* Allocate a temporary buffer of sufficient size. */
4130 size_t tmp_length;
4262 switch (type) 4131 switch (type)
4263 { 4132 {
4264 default: 4133 default:
@@ -4325,6 +4194,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4325 if (tmp_length < width) 4194 if (tmp_length < width)
4326 tmp_length = width; 4195 tmp_length = width;
4327 4196
4197 DCHAR_T tmpbuf[700];
4198 DCHAR_T *tmp;
4328 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) 4199 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
4329 tmp = tmpbuf; 4200 tmp = tmpbuf;
4330 else 4201 else
@@ -4340,7 +4211,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4340 goto out_of_memory; 4211 goto out_of_memory;
4341 } 4212 }
4342 4213
4343 tmp_end = tmp + tmp_length; 4214 DCHAR_T *tmp_end = tmp + tmp_length;
4344 4215
4345 unsigned long long arg; 4216 unsigned long long arg;
4346 switch (type) 4217 switch (type)
@@ -4389,7 +4260,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4389 } 4260 }
4390 int need_prefix = ((flags & FLAG_ALT) && arg != 0); 4261 int need_prefix = ((flags & FLAG_ALT) && arg != 0);
4391 4262
4392 p = tmp_end; 4263 DCHAR_T *p = tmp_end;
4393 /* "The result of converting a zero value with a precision 4264 /* "The result of converting a zero value with a precision
4394 of zero is no characters." */ 4265 of zero is no characters." */
4395 if (!(has_precision && precision == 0 && arg == 0)) 4266 if (!(has_precision && precision == 0 && arg == 0))
@@ -4409,7 +4280,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4409 *--p = '0'; 4280 *--p = '0';
4410 } 4281 }
4411 4282
4412 pad_ptr = p; 4283 DCHAR_T *pad_ptr = p;
4413 4284
4414 if (need_prefix) 4285 if (need_prefix)
4415 { 4286 {
@@ -4422,12 +4293,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4422# endif 4293# endif
4423 *--p = '0'; 4294 *--p = '0';
4424 } 4295 }
4425 tmp_start = p; 4296 DCHAR_T *tmp_start = p;
4426 4297
4427 /* The generated string now extends from tmp_start to tmp_end, 4298 /* The generated string now extends from tmp_start to tmp_end,
4428 with the zero padding insertion point being at pad_ptr, 4299 with the zero padding insertion point being at pad_ptr,
4429 tmp_start <= pad_ptr <= tmp_end. */ 4300 tmp_start <= pad_ptr <= tmp_end. */
4430 count = tmp_end - tmp_start; 4301 size_t count = tmp_end - tmp_start;
4431 4302
4432 if (count < width) 4303 if (count < width)
4433 { 4304 {
@@ -4503,26 +4374,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4503 { 4374 {
4504 arg_type type = a.arg[dp->arg_index].type; 4375 arg_type type = a.arg[dp->arg_index].type;
4505 int flags = dp->flags; 4376 int flags = dp->flags;
4506 size_t width;
4507 int has_precision;
4508 size_t precision;
4509 size_t tmp_length;
4510 size_t count;
4511 DCHAR_T tmpbuf[700];
4512 DCHAR_T *tmp;
4513 DCHAR_T *pad_ptr;
4514 DCHAR_T *p;
4515 4377
4516 width = 0; 4378 size_t width = 0;
4517 if (dp->width_start != dp->width_end) 4379 if (dp->width_start != dp->width_end)
4518 { 4380 {
4519 if (dp->width_arg_index != ARG_NONE) 4381 if (dp->width_arg_index != ARG_NONE)
4520 { 4382 {
4521 int arg;
4522
4523 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 4383 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
4524 abort (); 4384 abort ();
4525 arg = a.arg[dp->width_arg_index].a.a_int; 4385 int arg = a.arg[dp->width_arg_index].a.a_int;
4526 width = arg; 4386 width = arg;
4527 if (arg < 0) 4387 if (arg < 0)
4528 { 4388 {
@@ -4544,17 +4404,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4544 goto overflow; 4404 goto overflow;
4545 } 4405 }
4546 4406
4547 has_precision = 0; 4407 int has_precision = 0;
4548 precision = 0; 4408 size_t precision = 0;
4549 if (dp->precision_start != dp->precision_end) 4409 if (dp->precision_start != dp->precision_end)
4550 { 4410 {
4551 if (dp->precision_arg_index != ARG_NONE) 4411 if (dp->precision_arg_index != ARG_NONE)
4552 { 4412 {
4553 int arg;
4554
4555 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 4413 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
4556 abort (); 4414 abort ();
4557 arg = a.arg[dp->precision_arg_index].a.a_int; 4415 int arg = a.arg[dp->precision_arg_index].a.a_int;
4558 /* "A negative precision is taken as if the precision 4416 /* "A negative precision is taken as if the precision
4559 were omitted." */ 4417 were omitted." */
4560 if (arg >= 0) 4418 if (arg >= 0)
@@ -4575,6 +4433,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4575 } 4433 }
4576 4434
4577 /* Allocate a temporary buffer of sufficient size. */ 4435 /* Allocate a temporary buffer of sufficient size. */
4436 size_t tmp_length;
4578 if (type == TYPE_LONGDOUBLE) 4437 if (type == TYPE_LONGDOUBLE)
4579 tmp_length = 4438 tmp_length =
4580 (unsigned int) ((LDBL_DIG + 1) 4439 (unsigned int) ((LDBL_DIG + 1)
@@ -4597,6 +4456,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4597 4456
4598 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ 4457 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
4599 4458
4459 DCHAR_T tmpbuf[700];
4460 DCHAR_T *tmp;
4600 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) 4461 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
4601 tmp = tmpbuf; 4462 tmp = tmpbuf;
4602 else 4463 else
@@ -4612,8 +4473,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4612 goto out_of_memory; 4473 goto out_of_memory;
4613 } 4474 }
4614 4475
4615 pad_ptr = NULL; 4476 DCHAR_T *pad_ptr = NULL;
4616 p = tmp; 4477 DCHAR_T *p = tmp;
4617 if (type == TYPE_LONGDOUBLE) 4478 if (type == TYPE_LONGDOUBLE)
4618 { 4479 {
4619# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || (NEED_WPRINTF_DIRECTIVE_LA && WIDE_CHAR_VERSION) 4480# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || (NEED_WPRINTF_DIRECTIVE_LA && WIDE_CHAR_VERSION)
@@ -4632,11 +4493,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4632 } 4493 }
4633 else 4494 else
4634 { 4495 {
4635 int sign = 0;
4636 DECL_LONG_DOUBLE_ROUNDING 4496 DECL_LONG_DOUBLE_ROUNDING
4637 4497
4638 BEGIN_LONG_DOUBLE_ROUNDING (); 4498 BEGIN_LONG_DOUBLE_ROUNDING ();
4639 4499
4500 int sign = 0;
4640 if (signbit (arg)) /* arg < 0.0L or negative zero */ 4501 if (signbit (arg)) /* arg < 0.0L or negative zero */
4641 { 4502 {
4642 sign = -1; 4503 sign = -1;
@@ -4665,7 +4526,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4665 { 4526 {
4666 int exponent; 4527 int exponent;
4667 long double mantissa; 4528 long double mantissa;
4668
4669 if (arg > 0.0L) 4529 if (arg > 0.0L)
4670 mantissa = printf_frexpl (arg, &exponent); 4530 mantissa = printf_frexpl (arg, &exponent);
4671 else 4531 else
@@ -4679,9 +4539,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4679 { 4539 {
4680 /* Round the mantissa. */ 4540 /* Round the mantissa. */
4681 long double tail = mantissa; 4541 long double tail = mantissa;
4682 size_t q;
4683 4542
4684 for (q = precision; ; q--) 4543 for (size_t q = precision; ; q--)
4685 { 4544 {
4686 int digit = (int) tail; 4545 int digit = (int) tail;
4687 tail -= digit; 4546 tail -= digit;
@@ -4696,7 +4555,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4696 tail *= 16.0L; 4555 tail *= 16.0L;
4697 } 4556 }
4698 if (tail != 0.0L) 4557 if (tail != 0.0L)
4699 for (q = precision; q > 0; q--) 4558 for (size_t q = precision; q > 0; q--)
4700 tail *= 0.0625L; 4559 tail *= 0.0625L;
4701 mantissa += tail; 4560 mantissa += tail;
4702 } 4561 }
@@ -4705,9 +4564,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4705 *p++ = dp->conversion - 'A' + 'X'; 4564 *p++ = dp->conversion - 'A' + 'X';
4706 pad_ptr = p; 4565 pad_ptr = p;
4707 { 4566 {
4708 int digit; 4567 int digit = (int) mantissa;
4709
4710 digit = (int) mantissa;
4711 mantissa -= digit; 4568 mantissa -= digit;
4712 *p++ = '0' + digit; 4569 *p++ = '0' + digit;
4713 if ((flags & FLAG_ALT) 4570 if ((flags & FLAG_ALT)
@@ -4754,9 +4611,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4754 else 4611 else
4755 { 4612 {
4756 char expbuf[6 + 1]; 4613 char expbuf[6 + 1];
4757 const char *ep;
4758 sprintf (expbuf, "%+d", exponent); 4614 sprintf (expbuf, "%+d", exponent);
4759 for (ep = expbuf; (*p = *ep) != '\0'; ep++) 4615 for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++)
4760 p++; 4616 p++;
4761 } 4617 }
4762# endif 4618# endif
@@ -4787,7 +4643,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4787 else 4643 else
4788 { 4644 {
4789 int sign = 0; 4645 int sign = 0;
4790
4791 if (signbit (arg)) /* arg < 0.0 or negative zero */ 4646 if (signbit (arg)) /* arg < 0.0 or negative zero */
4792 { 4647 {
4793 sign = -1; 4648 sign = -1;
@@ -4816,7 +4671,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4816 { 4671 {
4817 int exponent; 4672 int exponent;
4818 double mantissa; 4673 double mantissa;
4819
4820 if (arg > 0.0) 4674 if (arg > 0.0)
4821 mantissa = printf_frexp (arg, &exponent); 4675 mantissa = printf_frexp (arg, &exponent);
4822 else 4676 else
@@ -4830,9 +4684,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4830 { 4684 {
4831 /* Round the mantissa. */ 4685 /* Round the mantissa. */
4832 double tail = mantissa; 4686 double tail = mantissa;
4833 size_t q;
4834 4687
4835 for (q = precision; ; q--) 4688 for (size_t q = precision; ; q--)
4836 { 4689 {
4837 int digit = (int) tail; 4690 int digit = (int) tail;
4838 tail -= digit; 4691 tail -= digit;
@@ -4847,7 +4700,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4847 tail *= 16.0; 4700 tail *= 16.0;
4848 } 4701 }
4849 if (tail != 0.0) 4702 if (tail != 0.0)
4850 for (q = precision; q > 0; q--) 4703 for (size_t q = precision; q > 0; q--)
4851 tail *= 0.0625; 4704 tail *= 0.0625;
4852 mantissa += tail; 4705 mantissa += tail;
4853 } 4706 }
@@ -4856,9 +4709,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4856 *p++ = dp->conversion - 'A' + 'X'; 4709 *p++ = dp->conversion - 'A' + 'X';
4857 pad_ptr = p; 4710 pad_ptr = p;
4858 { 4711 {
4859 int digit; 4712 int digit = (int) mantissa;
4860
4861 digit = (int) mantissa;
4862 mantissa -= digit; 4713 mantissa -= digit;
4863 *p++ = '0' + digit; 4714 *p++ = '0' + digit;
4864 if ((flags & FLAG_ALT) 4715 if ((flags & FLAG_ALT)
@@ -4905,9 +4756,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4905 else 4756 else
4906 { 4757 {
4907 char expbuf[6 + 1]; 4758 char expbuf[6 + 1];
4908 const char *ep;
4909 sprintf (expbuf, "%+d", exponent); 4759 sprintf (expbuf, "%+d", exponent);
4910 for (ep = expbuf; (*p = *ep) != '\0'; ep++) 4760 for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++)
4911 p++; 4761 p++;
4912 } 4762 }
4913# endif 4763# endif
@@ -4920,7 +4770,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
4920 4770
4921 /* The generated string now extends from tmp to p, with the 4771 /* The generated string now extends from tmp to p, with the
4922 zero padding insertion point being at pad_ptr. */ 4772 zero padding insertion point being at pad_ptr. */
4923 count = p - tmp; 4773 size_t count = p - tmp;
4924 4774
4925 if (count < width) 4775 if (count < width)
4926 { 4776 {
@@ -5001,8 +4851,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5001 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE 4851 || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
5002 /* Some systems produce wrong output for Inf, 4852 /* Some systems produce wrong output for Inf,
5003 -Inf, and NaN. Some systems in this category 4853 -Inf, and NaN. Some systems in this category
5004 (IRIX 5.3) also do so for -0.0. Therefore we 4854 also do so for -0.0. Therefore we treat this
5005 treat this case here as well. */ 4855 case here as well. */
5006 && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble)) 4856 && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
5007# endif 4857# endif
5008 )) 4858 ))
@@ -5011,26 +4861,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5011 arg_type type = a.arg[dp->arg_index].type; 4861 arg_type type = a.arg[dp->arg_index].type;
5012# endif 4862# endif
5013 int flags = dp->flags; 4863 int flags = dp->flags;
5014 size_t width;
5015 size_t count;
5016 int has_precision;
5017 size_t precision;
5018 size_t tmp_length;
5019 DCHAR_T tmpbuf[700];
5020 DCHAR_T *tmp;
5021 DCHAR_T *pad_ptr;
5022 DCHAR_T *p;
5023 4864
5024 width = 0; 4865 size_t width = 0;
5025 if (dp->width_start != dp->width_end) 4866 if (dp->width_start != dp->width_end)
5026 { 4867 {
5027 if (dp->width_arg_index != ARG_NONE) 4868 if (dp->width_arg_index != ARG_NONE)
5028 { 4869 {
5029 int arg;
5030
5031 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 4870 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
5032 abort (); 4871 abort ();
5033 arg = a.arg[dp->width_arg_index].a.a_int; 4872 int arg = a.arg[dp->width_arg_index].a.a_int;
5034 width = arg; 4873 width = arg;
5035 if (arg < 0) 4874 if (arg < 0)
5036 { 4875 {
@@ -5052,17 +4891,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5052 goto overflow; 4891 goto overflow;
5053 } 4892 }
5054 4893
5055 has_precision = 0; 4894 int has_precision = 0;
5056 precision = 0; 4895 size_t precision = 0;
5057 if (dp->precision_start != dp->precision_end) 4896 if (dp->precision_start != dp->precision_end)
5058 { 4897 {
5059 if (dp->precision_arg_index != ARG_NONE) 4898 if (dp->precision_arg_index != ARG_NONE)
5060 { 4899 {
5061 int arg;
5062
5063 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 4900 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
5064 abort (); 4901 abort ();
5065 arg = a.arg[dp->precision_arg_index].a.a_int; 4902 int arg = a.arg[dp->precision_arg_index].a.a_int;
5066 /* "A negative precision is taken as if the precision 4903 /* "A negative precision is taken as if the precision
5067 were omitted." */ 4904 were omitted." */
5068 if (arg >= 0) 4905 if (arg >= 0)
@@ -5091,6 +4928,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5091 precision = 6; 4928 precision = 6;
5092 4929
5093 /* Allocate a temporary buffer of sufficient size. */ 4930 /* Allocate a temporary buffer of sufficient size. */
4931 size_t tmp_length;
5094# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE 4932# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
5095 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1); 4933 tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
5096# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE 4934# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
@@ -5155,6 +4993,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5155 4993
5156 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ 4994 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
5157 4995
4996 DCHAR_T tmpbuf[700];
4997 DCHAR_T *tmp;
5158 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) 4998 if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
5159 tmp = tmpbuf; 4999 tmp = tmpbuf;
5160 else 5000 else
@@ -5170,8 +5010,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5170 goto out_of_memory; 5010 goto out_of_memory;
5171 } 5011 }
5172 5012
5173 pad_ptr = NULL; 5013 DCHAR_T *pad_ptr = NULL;
5174 p = tmp; 5014 DCHAR_T *p = tmp;
5175 5015
5176# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE 5016# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
5177# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE 5017# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
@@ -5193,11 +5033,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5193 } 5033 }
5194 else 5034 else
5195 { 5035 {
5196 int sign = 0;
5197 DECL_LONG_DOUBLE_ROUNDING 5036 DECL_LONG_DOUBLE_ROUNDING
5198 5037
5199 BEGIN_LONG_DOUBLE_ROUNDING (); 5038 BEGIN_LONG_DOUBLE_ROUNDING ();
5200 5039
5040 int sign = 0;
5201 if (signbit (arg)) /* arg < 0.0L or negative zero */ 5041 if (signbit (arg)) /* arg < 0.0L or negative zero */
5202 { 5042 {
5203 sign = -1; 5043 sign = -1;
@@ -5229,17 +5069,14 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5229 5069
5230 if (dp->conversion == 'f' || dp->conversion == 'F') 5070 if (dp->conversion == 'f' || dp->conversion == 'F')
5231 { 5071 {
5232 char *digits; 5072 char *digits =
5233 size_t ndigits;
5234
5235 digits =
5236 scale10_round_decimal_long_double (arg, precision); 5073 scale10_round_decimal_long_double (arg, precision);
5237 if (digits == NULL) 5074 if (digits == NULL)
5238 { 5075 {
5239 END_LONG_DOUBLE_ROUNDING (); 5076 END_LONG_DOUBLE_ROUNDING ();
5240 goto out_of_memory; 5077 goto out_of_memory;
5241 } 5078 }
5242 ndigits = strlen (digits); 5079 size_t ndigits = strlen (digits);
5243 5080
5244 if (ndigits > precision) 5081 if (ndigits > precision)
5245 { 5082 {
@@ -5355,12 +5192,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5355 else 5192 else
5356 { 5193 {
5357 /* arg > 0.0L. */ 5194 /* arg > 0.0L. */
5358 int adjusted; 5195 exponent = floorlog10l (arg);
5196 int adjusted = 0;
5359 char *digits; 5197 char *digits;
5360 size_t ndigits; 5198 size_t ndigits;
5361
5362 exponent = floorlog10l (arg);
5363 adjusted = 0;
5364 for (;;) 5199 for (;;)
5365 { 5200 {
5366 digits = 5201 digits =
@@ -5451,9 +5286,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5451 else 5286 else
5452 { 5287 {
5453 char expbuf[6 + 1]; 5288 char expbuf[6 + 1];
5454 const char *ep;
5455 sprintf (expbuf, "%+.2d", exponent); 5289 sprintf (expbuf, "%+.2d", exponent);
5456 for (ep = expbuf; (*p = *ep) != '\0'; ep++) 5290 for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++)
5457 p++; 5291 p++;
5458 } 5292 }
5459# endif 5293# endif
@@ -5489,14 +5323,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5489 else 5323 else
5490 { 5324 {
5491 /* arg > 0.0L. */ 5325 /* arg > 0.0L. */
5492 int exponent; 5326 int exponent = floorlog10l (arg);
5493 int adjusted; 5327 int adjusted = 0;
5494 char *digits; 5328 char *digits;
5495 size_t ndigits; 5329 size_t ndigits;
5496 size_t nzeroes;
5497
5498 exponent = floorlog10l (arg);
5499 adjusted = 0;
5500 for (;;) 5330 for (;;)
5501 { 5331 {
5502 digits = 5332 digits =
@@ -5556,7 +5386,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5556 5386
5557 /* Determine the number of trailing zeroes 5387 /* Determine the number of trailing zeroes
5558 that have to be dropped. */ 5388 that have to be dropped. */
5559 nzeroes = 0; 5389 size_t nzeroes = 0;
5560 if ((flags & FLAG_ALT) == 0) 5390 if ((flags & FLAG_ALT) == 0)
5561 while (nzeroes < ndigits 5391 while (nzeroes < ndigits
5562 && digits[nzeroes] == '0') 5392 && digits[nzeroes] == '0')
@@ -5706,9 +5536,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5706 else 5536 else
5707 { 5537 {
5708 char expbuf[6 + 1]; 5538 char expbuf[6 + 1];
5709 const char *ep;
5710 sprintf (expbuf, "%+.2d", exponent); 5539 sprintf (expbuf, "%+.2d", exponent);
5711 for (ep = expbuf; (*p = *ep) != '\0'; ep++) 5540 for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++)
5712 p++; 5541 p++;
5713 } 5542 }
5714# endif 5543# endif
@@ -5808,7 +5637,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5808 else 5637 else
5809 { 5638 {
5810 int sign = 0; 5639 int sign = 0;
5811
5812 if (signbit (arg)) /* arg < 0.0 or negative zero */ 5640 if (signbit (arg)) /* arg < 0.0 or negative zero */
5813 { 5641 {
5814 sign = -1; 5642 sign = -1;
@@ -5840,14 +5668,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5840 5668
5841 if (dp->conversion == 'f' || dp->conversion == 'F') 5669 if (dp->conversion == 'f' || dp->conversion == 'F')
5842 { 5670 {
5843 char *digits; 5671 char *digits =
5844 size_t ndigits;
5845
5846 digits =
5847 scale10_round_decimal_double (arg, precision); 5672 scale10_round_decimal_double (arg, precision);
5848 if (digits == NULL) 5673 if (digits == NULL)
5849 goto out_of_memory; 5674 goto out_of_memory;
5850 ndigits = strlen (digits); 5675 size_t ndigits = strlen (digits);
5851 5676
5852 if (ndigits > precision) 5677 if (ndigits > precision)
5853 { 5678 {
@@ -5963,12 +5788,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5963 else 5788 else
5964 { 5789 {
5965 /* arg > 0.0. */ 5790 /* arg > 0.0. */
5966 int adjusted; 5791 exponent = floorlog10 (arg);
5792 int adjusted = 0;
5967 char *digits; 5793 char *digits;
5968 size_t ndigits; 5794 size_t ndigits;
5969
5970 exponent = floorlog10 (arg);
5971 adjusted = 0;
5972 for (;;) 5795 for (;;)
5973 { 5796 {
5974 digits = 5797 digits =
@@ -6070,9 +5893,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6070 else 5893 else
6071 { 5894 {
6072 char expbuf[6 + 1]; 5895 char expbuf[6 + 1];
6073 const char *ep;
6074 sprintf (expbuf, decimal_format, exponent); 5896 sprintf (expbuf, decimal_format, exponent);
6075 for (ep = expbuf; (*p = *ep) != '\0'; ep++) 5897 for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++)
6076 p++; 5898 p++;
6077 } 5899 }
6078 } 5900 }
@@ -6109,14 +5931,10 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6109 else 5931 else
6110 { 5932 {
6111 /* arg > 0.0. */ 5933 /* arg > 0.0. */
6112 int exponent; 5934 int exponent = floorlog10 (arg);
6113 int adjusted; 5935 int adjusted = 0;
6114 char *digits; 5936 char *digits;
6115 size_t ndigits; 5937 size_t ndigits;
6116 size_t nzeroes;
6117
6118 exponent = floorlog10 (arg);
6119 adjusted = 0;
6120 for (;;) 5938 for (;;)
6121 { 5939 {
6122 digits = 5940 digits =
@@ -6172,7 +5990,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6172 5990
6173 /* Determine the number of trailing zeroes 5991 /* Determine the number of trailing zeroes
6174 that have to be dropped. */ 5992 that have to be dropped. */
6175 nzeroes = 0; 5993 size_t nzeroes = 0;
6176 if ((flags & FLAG_ALT) == 0) 5994 if ((flags & FLAG_ALT) == 0)
6177 while (nzeroes < ndigits 5995 while (nzeroes < ndigits
6178 && digits[nzeroes] == '0') 5996 && digits[nzeroes] == '0')
@@ -6337,9 +6155,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6337 else 6155 else
6338 { 6156 {
6339 char expbuf[6 + 1]; 6157 char expbuf[6 + 1];
6340 const char *ep;
6341 sprintf (expbuf, decimal_format, exponent); 6158 sprintf (expbuf, decimal_format, exponent);
6342 for (ep = expbuf; (*p = *ep) != '\0'; ep++) 6159 for (const char *ep = expbuf; (*p = *ep) != '\0'; ep++)
6343 p++; 6160 p++;
6344 } 6161 }
6345 } 6162 }
@@ -6409,7 +6226,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6409 6226
6410 /* The generated string now extends from tmp to p, with the 6227 /* The generated string now extends from tmp to p, with the
6411 zero padding insertion point being at pad_ptr. */ 6228 zero padding insertion point being at pad_ptr. */
6412 count = p - tmp; 6229 size_t count = p - tmp;
6413 6230
6414 if (count < width) 6231 if (count < width)
6415 { 6232 {
@@ -6473,16 +6290,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6473 { 6290 {
6474 arg_type type = a.arg[dp->arg_index].type; 6291 arg_type type = a.arg[dp->arg_index].type;
6475 int flags = dp->flags; 6292 int flags = dp->flags;
6476#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
6477 int has_width;
6478#endif
6479#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
6480 size_t width;
6481#endif
6482#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
6483 int has_precision;
6484 size_t precision;
6485#endif
6486#if NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION 6293#if NEED_PRINTF_FLAG_ALT_PRECISION_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
6487 int prec_ourselves; 6294 int prec_ourselves;
6488#else 6295#else
@@ -6511,19 +6318,17 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6511#endif 6318#endif
6512 6319
6513#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 6320#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
6514 has_width = 0; 6321 int has_width = 0;
6515#endif 6322#endif
6516#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 6323#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
6517 width = 0; 6324 size_t width = 0;
6518 if (dp->width_start != dp->width_end) 6325 if (dp->width_start != dp->width_end)
6519 { 6326 {
6520 if (dp->width_arg_index != ARG_NONE) 6327 if (dp->width_arg_index != ARG_NONE)
6521 { 6328 {
6522 int arg;
6523
6524 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 6329 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
6525 abort (); 6330 abort ();
6526 arg = a.arg[dp->width_arg_index].a.a_int; 6331 int arg = a.arg[dp->width_arg_index].a.a_int;
6527 width = arg; 6332 width = arg;
6528 if (arg < 0) 6333 if (arg < 0)
6529 { 6334 {
@@ -6551,17 +6356,15 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6551#endif 6356#endif
6552 6357
6553#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 6358#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
6554 has_precision = 0; 6359 int has_precision = 0;
6555 precision = 6; 6360 size_t precision = 6;
6556 if (dp->precision_start != dp->precision_end) 6361 if (dp->precision_start != dp->precision_end)
6557 { 6362 {
6558 if (dp->precision_arg_index != ARG_NONE) 6363 if (dp->precision_arg_index != ARG_NONE)
6559 { 6364 {
6560 int arg;
6561
6562 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) 6365 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
6563 abort (); 6366 abort ();
6564 arg = a.arg[dp->precision_arg_index].a.a_int; 6367 int arg = a.arg[dp->precision_arg_index].a.a_int;
6565 /* "A negative precision is taken as if the precision 6368 /* "A negative precision is taken as if the precision
6566 were omitted." */ 6369 were omitted." */
6567 if (arg >= 0) 6370 if (arg >= 0)
@@ -6648,19 +6451,21 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6648#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) 6451#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)
6649 switch (dp->conversion) 6452 switch (dp->conversion)
6650 { 6453 {
6651# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO 6454# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO
6455# if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
6652 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need 6456 /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
6653 to perform the padding after this conversion. Functions 6457 to perform the padding after this conversion. Functions
6654 with unistdio extensions perform the padding based on 6458 with unistdio extensions perform the padding based on
6655 character count rather than element count. */ 6459 character count rather than element count. */
6656 case 'c': case 's': 6460 case 'c': case 's':
6657# endif 6461# endif
6658# if NEED_PRINTF_FLAG_ZERO 6462# if NEED_PRINTF_FLAG_ZERO
6659 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G': 6463 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
6660 case 'a': case 'A': 6464 case 'a': case 'A':
6661# endif 6465# endif
6662 pad_ourselves = 1; 6466 pad_ourselves = 1;
6663 break; 6467 break;
6468# endif
6664 default: 6469 default:
6665 pad_ourselves = prec_ourselves | group_ourselves; 6470 pad_ourselves = prec_ourselves | group_ourselves;
6666 break; 6471 break;
@@ -6728,11 +6533,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6728 quick check anyway. */ 6533 quick check anyway. */
6729 if (dp->width_arg_index != ARG_NONE) 6534 if (dp->width_arg_index != ARG_NONE)
6730 { 6535 {
6731 int arg;
6732
6733 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) 6536 if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
6734 abort (); 6537 abort ();
6735 arg = a.arg[dp->width_arg_index].a.a_int; 6538 int arg = a.arg[dp->width_arg_index].a.a_int;
6736 width = arg; 6539 width = arg;
6737 if (arg < 0) 6540 if (arg < 0)
6738 { 6541 {
@@ -6957,7 +6760,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6957 /* The following platforms forbid %n: 6760 /* The following platforms forbid %n:
6958 - On glibc2 systems from 2004-10-18 or newer, the use of 6761 - On glibc2 systems from 2004-10-18 or newer, the use of
6959 %n in format strings in writable memory may crash the 6762 %n in format strings in writable memory may crash the
6960 program (if compiled with _FORTIFY_SOURCE=2). 6763 program (if compiled with _FORTIFY_SOURCE >= 2).
6961 - On macOS 10.13 or newer, the use of %n in format 6764 - On macOS 10.13 or newer, the use of %n in format
6962 strings in writable memory by default crashes the 6765 strings in writable memory by default crashes the
6963 program. 6766 program.
@@ -6990,7 +6793,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
6990 on musl libc because we would run into an swprintf() bug. 6793 on musl libc because we would run into an swprintf() bug.
6991 See <https://www.openwall.com/lists/musl/2023/03/19/1>. */ 6794 See <https://www.openwall.com/lists/musl/2023/03/19/1>. */
6992 fbp[1] = '\0'; 6795 fbp[1] = '\0';
6993# else /* AIX <= 5.1, HP-UX, IRIX, OSF/1, Solaris <= 9, BeOS */ 6796# else /* AIX <= 5.1, HP-UX, Solaris <= 9, BeOS */
6994 fbp[1] = '%'; 6797 fbp[1] = '%';
6995 fbp[2] = 'n'; 6798 fbp[2] = 'n';
6996 fbp[3] = '\0'; 6799 fbp[3] = '\0';
@@ -7657,16 +7460,16 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
7657 ) 7460 )
7658 { 7461 {
7659 /* The result string is not guaranteed to be ASCII. */ 7462 /* The result string is not guaranteed to be ASCII. */
7660 const TCHAR_T *tmpsrc;
7661 DCHAR_T *tmpdst;
7662 size_t tmpdst_len;
7663 /* This code assumes that TCHAR_T is 'char'. */ 7463 /* This code assumes that TCHAR_T is 'char'. */
7664 static_assert (sizeof (TCHAR_T) == 1); 7464 static_assert (sizeof (TCHAR_T) == 1);
7465 const TCHAR_T *tmpsrc;
7665# if USE_SNPRINTF 7466# if USE_SNPRINTF
7666 tmpsrc = (TCHAR_T *) (result + length); 7467 tmpsrc = (TCHAR_T *) (result + length);
7667# else 7468# else
7668 tmpsrc = tmp; 7469 tmpsrc = tmp;
7669# endif 7470# endif
7471 DCHAR_T *tmpdst;
7472 size_t tmpdst_len;
7670# if WIDE_CHAR_VERSION 7473# if WIDE_CHAR_VERSION
7671 /* Convert tmpsrc[0..count-1] to a freshly allocated 7474 /* Convert tmpsrc[0..count-1] to a freshly allocated
7672 wide character array. */ 7475 wide character array. */
@@ -7744,9 +7547,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
7744# endif 7547# endif
7745 { 7548 {
7746 const TCHAR_T *tmpsrc; 7549 const TCHAR_T *tmpsrc;
7747 DCHAR_T *tmpdst;
7748 size_t n;
7749
7750# if USE_SNPRINTF 7550# if USE_SNPRINTF
7751 if (result == resultbuf) 7551 if (result == resultbuf)
7752 { 7552 {
@@ -7766,11 +7566,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
7766 tmpsrc = tmp; 7566 tmpsrc = tmp;
7767 ENSURE_ALLOCATION (xsum (length, count)); 7567 ENSURE_ALLOCATION (xsum (length, count));
7768# endif 7568# endif
7769 tmpdst = result + length; 7569 DCHAR_T *tmpdst = result + length;
7770 /* Copy backwards, because of overlapping. */ 7570 /* Copy backwards, because of overlapping. */
7771 tmpsrc += count; 7571 tmpsrc += count;
7772 tmpdst += count; 7572 tmpdst += count;
7773 for (n = count; n > 0; n--) 7573 for (size_t n = count; n > 0; n--)
7774 *--tmpdst = *--tmpsrc; 7574 *--tmpdst = *--tmpsrc;
7775 } 7575 }
7776 } 7576 }
@@ -7805,9 +7605,9 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
7805 /* glibc prefers to compare the width against the number 7605 /* glibc prefers to compare the width against the number
7806 of characters as well, but only for numeric conversion 7606 of characters as well, but only for numeric conversion
7807 specifiers. See 7607 specifiers. See
7808 <https://sourceware.org/bugzilla/show_bug.cgi?id=28943> 7608 <https://sourceware.org/PR28943>
7809 <https://sourceware.org/bugzilla/show_bug.cgi?id=30883> 7609 <https://sourceware.org/PR30883>
7810 <https://sourceware.org/bugzilla/show_bug.cgi?id=31542> */ 7610 <https://sourceware.org/PR31542> */
7811 switch (dp->conversion) 7611 switch (dp->conversion)
7812 { 7612 {
7813 case 'd': case 'i': case 'u': 7613 case 'd': case 'i': case 'u':
@@ -7955,8 +7755,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
7955 { 7755 {
7956 /* Convert the %f result to upper case for %F. */ 7756 /* Convert the %f result to upper case for %F. */
7957 DCHAR_T *rp = result + length; 7757 DCHAR_T *rp = result + length;
7958 size_t rc; 7758 for (size_t rc = count; rc > 0; rc--, rp++)
7959 for (rc = count; rc > 0; rc--, rp++)
7960 if (*rp >= 'a' && *rp <= 'z') 7759 if (*rp >= 'a' && *rp <= 'z')
7961 *rp = *rp - 'a' + 'A'; 7760 *rp = *rp - 'a' + 'A';
7962 } 7761 }
@@ -7979,9 +7778,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
7979 if (result != resultbuf && length + 1 < allocated) 7778 if (result != resultbuf && length + 1 < allocated)
7980 { 7779 {
7981 /* Shrink the allocated memory if possible. */ 7780 /* Shrink the allocated memory if possible. */
7982 DCHAR_T *memory; 7781 DCHAR_T *memory =
7983 7782 (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
7984 memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
7985 if (memory != NULL) 7783 if (memory != NULL)
7986 result = memory; 7784 result = memory;
7987 } 7785 }