@@ -11417,20 +11417,20 @@ static int js_ecvt(double d, int n_digits, int *decpt, int *sign, char *buf,
11417
11417
return n_digits;
11418
11418
}
11419
11419
11420
- static int js_fcvt1(char *buf, int buf_size , double d, int n_digits,
11420
+ static int js_fcvt1(char ( *buf)[JS_DTOA_BUF_SIZE] , double d, int n_digits,
11421
11421
int rounding_mode)
11422
11422
{
11423
11423
int n;
11424
11424
if (rounding_mode != FE_TONEAREST)
11425
11425
fesetround(rounding_mode);
11426
- n = snprintf(buf, buf_size , "%.*f", n_digits, d);
11426
+ n = snprintf(* buf, sizeof(*buf) , "%.*f", n_digits, d);
11427
11427
if (rounding_mode != FE_TONEAREST)
11428
11428
fesetround(FE_TONEAREST);
11429
- assert(n < buf_size );
11429
+ assert(n < sizeof(*buf) );
11430
11430
return n;
11431
11431
}
11432
11432
11433
- static void js_fcvt(char *buf, int buf_size , double d, int n_digits)
11433
+ static void js_fcvt(char ( *buf)[JS_DTOA_BUF_SIZE] , double d, int n_digits)
11434
11434
{
11435
11435
int rounding_mode;
11436
11436
rounding_mode = FE_TONEAREST;
@@ -11444,12 +11444,12 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
11444
11444
zero (RNDNA), but in printf the "ties" case is not specified
11445
11445
(for example it is RNDN for glibc, RNDNA for Windows), so we
11446
11446
must round manually. */
11447
- n1 = js_fcvt1(buf1, sizeof(buf1) , d, n_digits + 1, FE_TONEAREST);
11447
+ n1 = js_fcvt1(& buf1, d, n_digits + 1, FE_TONEAREST);
11448
11448
rounding_mode = FE_TONEAREST;
11449
11449
/* XXX: could use 2 digits to reduce the average running time */
11450
11450
if (buf1[n1 - 1] == '5') {
11451
- n1 = js_fcvt1(buf1, sizeof(buf1) , d, n_digits + 1, FE_DOWNWARD);
11452
- n2 = js_fcvt1(buf2, sizeof(buf2) , d, n_digits + 1, FE_UPWARD);
11451
+ n1 = js_fcvt1(& buf1, d, n_digits + 1, FE_DOWNWARD);
11452
+ n2 = js_fcvt1(& buf2, d, n_digits + 1, FE_UPWARD);
11453
11453
if (n1 == n2 && memcmp(buf1, buf2, n1) == 0) {
11454
11454
/* exact result: round away from zero */
11455
11455
if (buf1[0] == '-')
@@ -11460,7 +11460,7 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
11460
11460
}
11461
11461
}
11462
11462
#endif /* CONFIG_PRINTF_RNDN */
11463
- js_fcvt1(buf, buf_size, d, n_digits, rounding_mode);
11463
+ js_fcvt1(buf, d, n_digits, rounding_mode);
11464
11464
}
11465
11465
11466
11466
/* radix != 10 is only supported with flags = JS_DTOA_VAR_FORMAT */
@@ -11476,18 +11476,18 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
11476
11476
/* XXX: slow and maybe not fully correct. Use libbf when it is fast enough.
11477
11477
XXX: radix != 10 is only supported for small integers
11478
11478
*/
11479
- static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
11479
+ static void js_dtoa1(char (*buf)[JS_DTOA_BUF_SIZE], double d,
11480
+ int radix, int n_digits, int flags)
11480
11481
{
11481
11482
char *q;
11482
11483
11483
11484
if (!isfinite(d)) {
11484
11485
if (isnan(d)) {
11485
- strcpy(buf, "NaN");
11486
+ pstrcpy(*buf, sizeof(*buf), "NaN");
11487
+ } else if (d < 0) {
11488
+ pstrcpy(*buf, sizeof(*buf), "-Infinity");
11486
11489
} else {
11487
- q = buf;
11488
- if (d < 0)
11489
- *q++ = '-';
11490
- strcpy(q, "Infinity");
11490
+ pstrcpy(*buf, sizeof(*buf), "Infinity");
11491
11491
}
11492
11492
} else if (flags == JS_DTOA_VAR_FORMAT) {
11493
11493
int64_t i64;
@@ -11499,12 +11499,12 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
11499
11499
goto generic_conv;
11500
11500
/* fast path for integers */
11501
11501
ptr = i64toa(buf1 + sizeof(buf1), i64, radix);
11502
- strcpy( buf, ptr);
11502
+ pstrcpy(* buf, sizeof(*buf) , ptr);
11503
11503
} else {
11504
11504
if (d == 0.0)
11505
11505
d = 0.0; /* convert -0 to 0 */
11506
11506
if (flags == JS_DTOA_FRAC_FORMAT) {
11507
- js_fcvt(buf, JS_DTOA_BUF_SIZE, d, n_digits);
11507
+ js_fcvt(buf, d, n_digits);
11508
11508
} else {
11509
11509
char buf1[JS_DTOA_BUF_SIZE];
11510
11510
int sign, decpt, k, n, i, p, n_max;
@@ -11519,7 +11519,7 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
11519
11519
/* the number has k digits (k >= 1) */
11520
11520
k = js_ecvt(d, n_digits, &decpt, &sign, buf1, is_fixed);
11521
11521
n = decpt; /* d=10^(n-k)*(buf1) i.e. d= < x.yyyy 10^(n-1) */
11522
- q = buf;
11522
+ q = * buf;
11523
11523
if (sign)
11524
11524
*q++ = '-';
11525
11525
if (flags & JS_DTOA_FORCE_EXP)
@@ -11561,7 +11561,7 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
11561
11561
p = n - 1;
11562
11562
if (p >= 0)
11563
11563
*q++ = '+';
11564
- sprintf( q, "%d", p);
11564
+ snprintf(q, *buf + sizeof(*buf) - q, "%d", p);
11565
11565
}
11566
11566
}
11567
11567
}
@@ -11571,7 +11571,7 @@ static JSValue js_dtoa(JSContext *ctx,
11571
11571
double d, int radix, int n_digits, int flags)
11572
11572
{
11573
11573
char buf[JS_DTOA_BUF_SIZE];
11574
- js_dtoa1(buf, d, radix, n_digits, flags);
11574
+ js_dtoa1(& buf, d, radix, n_digits, flags);
11575
11575
return JS_NewString(ctx, buf);
11576
11576
}
11577
11577
@@ -27363,6 +27363,7 @@ static char *js_default_module_normalize_name(JSContext *ctx,
27363
27363
{
27364
27364
char *filename, *p;
27365
27365
const char *r;
27366
+ int cap;
27366
27367
int len;
27367
27368
27368
27369
if (name[0] != '.') {
@@ -27376,7 +27377,8 @@ static char *js_default_module_normalize_name(JSContext *ctx,
27376
27377
else
27377
27378
len = 0;
27378
27379
27379
- filename = js_malloc(ctx, len + strlen(name) + 1 + 1);
27380
+ cap = len + strlen(name) + 1 + 1;
27381
+ filename = js_malloc(ctx, cap);
27380
27382
if (!filename)
27381
27383
return NULL;
27382
27384
memcpy(filename, base_name, len);
@@ -27408,8 +27410,8 @@ static char *js_default_module_normalize_name(JSContext *ctx,
27408
27410
}
27409
27411
}
27410
27412
if (filename[0] != '\0')
27411
- strcat (filename, "/");
27412
- strcat (filename, r);
27413
+ pstrcat (filename, cap , "/");
27414
+ pstrcat (filename, cap , r);
27413
27415
// printf("normalize: %s %s -> %s\n", base_name, name, filename);
27414
27416
return filename;
27415
27417
}
0 commit comments