Skip to content

Commit f6fdfee

Browse files
committed
Do not skip leading whitespace in jvp_strtod
`jvp_strtod` skips leading whitespace, but its decnum counterpart `decNumberFromString` (called within `jv_number_with_literal`) does not. Those two are called interchangeably, so it leads to inconsistent behavior depending on whether the decnum feature is enabled. Additionally, `classify`, used in the token scanner, only considers [ \t\n\r] to be whitespace, but `jvp_strtod` consumes the larger set [ \t\n\v\f\r], so those extra characters are considered literals. Changing this deviates from the behavior of `strdod` from <stdlib.h> and is technically a breaking API change, since it is a public symbol.
1 parent 984b5cd commit f6fdfee

File tree

2 files changed

+5
-13
lines changed

2 files changed

+5
-13
lines changed

src/jv_dtoa.c

+3-11
Original file line numberDiff line numberDiff line change
@@ -2368,29 +2368,21 @@ jvp_strtod
23682368

23692369
sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
23702370
dval(&rv) = 0.;
2371-
for(s = s00;;s++) switch(*s) {
2371+
switch(*(s = s00)) {
23722372
case '-':
23732373
sign = 1;
23742374
/* no break */
23752375
JQ_FALLTHROUGH;
23762376
case '+':
23772377
if (*++s)
2378-
goto break2;
2378+
break;
23792379
/* no break */
23802380
JQ_FALLTHROUGH;
23812381
case 0:
23822382
goto ret0;
2383-
case '\t':
2384-
case '\n':
2385-
case '\v':
2386-
case '\f':
2387-
case '\r':
2388-
case ' ':
2389-
continue;
23902383
default:
2391-
goto break2;
2384+
break;
23922385
}
2393-
break2:
23942386
if (*s == '0') {
23952387
#ifndef NO_HEX_FP /*{*/
23962388
switch(s[1]) {

tests/jq.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -2084,8 +2084,8 @@ null
20842084
2
20852085

20862086
.[] |= try tonumber
2087-
["1", "2a", "3", " 4 ", "5.67", ".89", "-876", "+5.43", 21]
2088-
[1, 3, 5.67, 0.89, -876, 5.43, 21]
2087+
["1", "2a", "3", " 4", "5 ", "6.7", ".89", "-876", "+5.43", 21]
2088+
[1, 3, 6.7, 0.89, -876, 5.43, 21]
20892089

20902090
# Also 1859, but from 2073
20912091
any(keys[]|tostring?;true)

0 commit comments

Comments
 (0)