Skip to content

Commit 6dbf01b

Browse files
committed
Remove unsafe sprintf() and strcat() calls
Prep work for enabling the sanitizers on macos CI since they are marked as deprecated and cause the build to fail when -Werror is enabled.
1 parent 6535064 commit 6dbf01b

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

quickjs.c

+24-22
Original file line numberDiff line numberDiff line change
@@ -11417,20 +11417,20 @@ static int js_ecvt(double d, int n_digits, int *decpt, int *sign, char *buf,
1141711417
return n_digits;
1141811418
}
1141911419

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,
1142111421
int rounding_mode)
1142211422
{
1142311423
int n;
1142411424
if (rounding_mode != FE_TONEAREST)
1142511425
fesetround(rounding_mode);
11426-
n = snprintf(buf, buf_size, "%.*f", n_digits, d);
11426+
n = snprintf(*buf, sizeof(*buf), "%.*f", n_digits, d);
1142711427
if (rounding_mode != FE_TONEAREST)
1142811428
fesetround(FE_TONEAREST);
11429-
assert(n < buf_size);
11429+
assert(n < sizeof(*buf));
1143011430
return n;
1143111431
}
1143211432

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)
1143411434
{
1143511435
int rounding_mode;
1143611436
rounding_mode = FE_TONEAREST;
@@ -11444,12 +11444,12 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
1144411444
zero (RNDNA), but in printf the "ties" case is not specified
1144511445
(for example it is RNDN for glibc, RNDNA for Windows), so we
1144611446
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);
1144811448
rounding_mode = FE_TONEAREST;
1144911449
/* XXX: could use 2 digits to reduce the average running time */
1145011450
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);
1145311453
if (n1 == n2 && memcmp(buf1, buf2, n1) == 0) {
1145411454
/* exact result: round away from zero */
1145511455
if (buf1[0] == '-')
@@ -11460,7 +11460,7 @@ static void js_fcvt(char *buf, int buf_size, double d, int n_digits)
1146011460
}
1146111461
}
1146211462
#endif /* CONFIG_PRINTF_RNDN */
11463-
js_fcvt1(buf, buf_size, d, n_digits, rounding_mode);
11463+
js_fcvt1(buf, d, n_digits, rounding_mode);
1146411464
}
1146511465

1146611466
/* 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)
1147611476
/* XXX: slow and maybe not fully correct. Use libbf when it is fast enough.
1147711477
XXX: radix != 10 is only supported for small integers
1147811478
*/
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)
1148011481
{
1148111482
char *q;
1148211483

1148311484
if (!isfinite(d)) {
1148411485
if (isnan(d)) {
11485-
strcpy(buf, "NaN");
11486+
pstrcpy(*buf, sizeof(*buf), "NaN");
11487+
} else if (d < 0) {
11488+
pstrcpy(*buf, sizeof(*buf), "-Infinity");
1148611489
} else {
11487-
q = buf;
11488-
if (d < 0)
11489-
*q++ = '-';
11490-
strcpy(q, "Infinity");
11490+
pstrcpy(*buf, sizeof(*buf), "Infinity");
1149111491
}
1149211492
} else if (flags == JS_DTOA_VAR_FORMAT) {
1149311493
int64_t i64;
@@ -11499,12 +11499,12 @@ static void js_dtoa1(char *buf, double d, int radix, int n_digits, int flags)
1149911499
goto generic_conv;
1150011500
/* fast path for integers */
1150111501
ptr = i64toa(buf1 + sizeof(buf1), i64, radix);
11502-
strcpy(buf, ptr);
11502+
pstrcpy(*buf, sizeof(*buf), ptr);
1150311503
} else {
1150411504
if (d == 0.0)
1150511505
d = 0.0; /* convert -0 to 0 */
1150611506
if (flags == JS_DTOA_FRAC_FORMAT) {
11507-
js_fcvt(buf, JS_DTOA_BUF_SIZE, d, n_digits);
11507+
js_fcvt(buf, d, n_digits);
1150811508
} else {
1150911509
char buf1[JS_DTOA_BUF_SIZE];
1151011510
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)
1151911519
/* the number has k digits (k >= 1) */
1152011520
k = js_ecvt(d, n_digits, &decpt, &sign, buf1, is_fixed);
1152111521
n = decpt; /* d=10^(n-k)*(buf1) i.e. d= < x.yyyy 10^(n-1) */
11522-
q = buf;
11522+
q = *buf;
1152311523
if (sign)
1152411524
*q++ = '-';
1152511525
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)
1156111561
p = n - 1;
1156211562
if (p >= 0)
1156311563
*q++ = '+';
11564-
sprintf(q, "%d", p);
11564+
snprintf(q, *buf + sizeof(*buf) - q, "%d", p);
1156511565
}
1156611566
}
1156711567
}
@@ -11571,7 +11571,7 @@ static JSValue js_dtoa(JSContext *ctx,
1157111571
double d, int radix, int n_digits, int flags)
1157211572
{
1157311573
char buf[JS_DTOA_BUF_SIZE];
11574-
js_dtoa1(buf, d, radix, n_digits, flags);
11574+
js_dtoa1(&buf, d, radix, n_digits, flags);
1157511575
return JS_NewString(ctx, buf);
1157611576
}
1157711577

@@ -27363,6 +27363,7 @@ static char *js_default_module_normalize_name(JSContext *ctx,
2736327363
{
2736427364
char *filename, *p;
2736527365
const char *r;
27366+
int cap;
2736627367
int len;
2736727368

2736827369
if (name[0] != '.') {
@@ -27376,7 +27377,8 @@ static char *js_default_module_normalize_name(JSContext *ctx,
2737627377
else
2737727378
len = 0;
2737827379

27379-
filename = js_malloc(ctx, len + strlen(name) + 1 + 1);
27380+
cap = len + strlen(name) + 1 + 1;
27381+
filename = js_malloc(ctx, cap);
2738027382
if (!filename)
2738127383
return NULL;
2738227384
memcpy(filename, base_name, len);
@@ -27408,8 +27410,8 @@ static char *js_default_module_normalize_name(JSContext *ctx,
2740827410
}
2740927411
}
2741027412
if (filename[0] != '\0')
27411-
strcat(filename, "/");
27412-
strcat(filename, r);
27413+
pstrcat(filename, cap, "/");
27414+
pstrcat(filename, cap, r);
2741327415
// printf("normalize: %s %s -> %s\n", base_name, name, filename);
2741427416
return filename;
2741527417
}

0 commit comments

Comments
 (0)