1
0
mirror of https://github.com/janet-lang/janet synced 2025-01-10 23:50:26 +00:00

Update strtod.c to be less accepting of some badly formed numbers.

This commit is contained in:
Calvin Rose 2018-05-06 15:27:52 -04:00
parent b31791200b
commit 7e63427208
3 changed files with 17 additions and 3 deletions

View File

@ -149,6 +149,7 @@ static struct DstScanRes dst_scan_impl(
/* Initialize flags */
int seenadigit = 0;
int gotradix = 0;
/* Initialize result */
res.mant = 0;
@ -197,15 +198,19 @@ static struct DstScanRes dst_scan_impl(
} else if (res.base == 10 && (*str == 'E' || *str == 'e')) {
res.foundexp = 1;
break;
} else if (*str == 'x' || *str == 'X') {
} else if (!gotradix && (*str == 'x' || *str == 'X')) {
if (res.seenpoint || res.mant > 0) goto error;
res.base = 16;
res.mant = 0;
} else if (*str == 'r' || *str == 'R') {
seenadigit = 0;
gotradix = 1;
} else if (!gotradix && (*str == 'r' || *str == 'R')) {
if (res.seenpoint) goto error;
if (res.mant < 2 || res.mant > 36) goto error;
res.base = res.mant;
res.mant = 0;
seenadigit = 0;
gotradix = 1;
} else if (*str == '_') {
;
/* underscores are ignored - can be used for separator */
@ -242,6 +247,10 @@ static struct DstScanRes dst_scan_impl(
while (str < end && *str == '0') str++;
while (str < end && ee < (INT32_MAX / 40)) {
int digit = digit_lookup[*str & 0x7F];
if (*str == '_') {
str++;
continue;
}
if (*str > 127 || digit >= res.base) goto error;
ee = res.base * ee + digit;
str++;

View File

@ -169,7 +169,6 @@ static uint8_t checkescape(uint8_t c) {
case 'f': return '\f';
case 'e': return 27;
case '"': return '"';
case '\'': return '\'';
case '\\': return '\\';
}
}

View File

@ -67,4 +67,10 @@
(assert (= nil (get roottab :childprop)), "table get 2")
(assert (= 456 (get childtab :childprop)), "proto no effect")
# Long strings
(assert (= "hello, world" \==\hello, world\==\), "simple long string")
(assert (= "hello, \"world\"" \\hello, "world"\\), "long string with embedded quotes")
(assert (= "hello, \\\\\\ \"world\"" \=\hello, \\\ "world"\=\), "long string with embedded quotes and backslashes")
(end-suite)