Skip to content
Myr

Fix whitespace in number parsing (#3195)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Handle input errors for --indent Handle malformed and overflowing arguments. Also, forbid leading and trailing spaces to match the behavior of tonumber from commit ce0e788 (improve tonumber/0 performance by parsing input as number literal, 2024-03-02). * 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.

authored by Thalia Archibald · committed by GitHub · parent aaace54 · Browse files · 3 files+11 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+11 added−16 removed

  • tests:+2 added−2 removed
  • other:+9 added−14 removed

3 files+11 added−16 removed

modified:src/jv_dtoa.c+3 added-11 removed
2366 unmodified lines
2367#endif23682369 sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;2370 dval(&rv) = 0.;2371 for(s = s00;;s++) switch(*s) {2371 switch(*(s = s00)) {2372 case '-':2373 sign = 1;2374 /* no break */2375 JQ_FALLTHROUGH;2376 case '+':2377 if (*++s)2378 goto break2;2378 break;2379 /* no break */2380 JQ_FALLTHROUGH;2381 case 0:2382 goto ret0;2383 case '\t':2384 case '\n':2385 case '\v':2386 case '\f':2387 case '\r':2388 case ' ':2389 continue;2383 default:2391 goto break2;2384 break;2385 }2393 break2:2386 if (*s == '0') {2387#ifndef NO_HEX_FP /*{*/2388 switch(s[1]) {2389 case 'x':
1887 unmodified lines
modified:src/main.c+6 added-3 removed
420 unmodified lines
421 if (i >= argc - 1) {422 fprintf(stderr, "jq: --indent takes one parameter\n");423 die();424 }425 dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));426 int indent = atoi(argv[i+1]);427 if (indent < -1 || indent > 7) {425 char* end = NULL;426 errno = 0;427 long indent = strtol(argv[i+1], &end, 10);428 if (errno || indent < -1 || indent > 7 ||429 isspace(*argv[i+1]) || end == argv[i+1] || *end) {430 fprintf(stderr, "jq: --indent takes a number between -1 and 7\n");431 die();432 }433 dumpopts &= ~(JV_PRINT_TAB | JV_PRINT_INDENT_FLAGS(7));434 dumpopts |= JV_PRINT_INDENT_FLAGS(indent);435 i++;436 } else if (isoption(&text, 0, "seq", is_short)) {437 options |= SEQ;
273 unmodified lines
modified:tests/jq.test+2 added-2 removed
2340 unmodified lines
234112342223432344.[] |= try tonumber2345["1", "2a", "3", " 4 ", "5.67", ".89", "-876", "+5.43", 21]2346[1, 3, 5.67, 0.89, -876, 5.43, 21]2345["1", "2a", "3", " 4", "5 ", "6.7", ".89", "-876", "+5.43", 21]2346[1, 3, 6.7, 0.89, -876, 5.43, 21]23472348# Also 1859, but from 20732349any(keys[]|tostring?;true)2350{"a":"1","b":"2","c":"3"}
143 unmodified lines