Skip to content
Myr

Fix tonumber and toboolean to reject strings with embedded null bytes (#3496)

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

authored by itchyny · committed by GitHub · parent bfcf82f · Browse files · 2 files+17 added−0 removed
This commit

State

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

Diff stats

2 files+17 added−0 removed

  • tests:+9 added−0 removed
  • other:+8 added−0 removed

2 files+17 added−0 removed

modified:src/builtin.c+8 added
444 unmodified lines
445 return input;446 }447 if (jv_get_kind(input) == JV_KIND_STRING) {448 const char* s = jv_string_value(input);449 int len = jv_string_length_bytes(jv_copy(input));450 if ((size_t)len != strlen(s)) {451 return type_error(input, "cannot be parsed as a number");452 }453#ifdef USE_DECNUM454 jv number = jv_number_with_literal(s);455 if (jv_get_kind(number) == JV_KIND_INVALID) {456 return type_error(input, "cannot be parsed as a number");
17 unmodified lines
474 return input;475 }476 if (jv_get_kind(input) == JV_KIND_STRING) {477 const char *s = jv_string_value(input);478 int len = jv_string_length_bytes(jv_copy(input));479 if ((size_t)len != strlen(s)) {480 return type_error(input, "cannot be parsed as a boolean");481 }482 if (strcmp(s, "true") == 0) {483 jv_free(input);484 return jv_true();485 } else if (strcmp(s, "false") == 0) {
1606 unmodified lines
modified:tests/jq.test+9 added
696 unmodified lines
6971 + tonumber + ("10" | tonumber)698469915700701"123\u0000456" | try tonumber catch .702null703"string (\"123\\u0000456\") cannot be parsed as a number"704705map(toboolean)706["false","true",false,true]707[false,true,false,true]708
7 unmodified lines
716"string (\"falsee\") cannot be parsed as a boolean"717"array ([]) cannot be parsed as a boolean"718"object ({}) cannot be parsed as a boolean"719720"true\u0000x", "false\u0000" | try toboolean catch .721null722"string (\"true\\u0000x\") cannot be parsed as a boolean"723"string (\"false\\u0000\") cannot be parsed as a boolean"724725[{"a":42},.object,10,.num,false,true,null,"b",[1,4]] | .[] as $x | [$x == .[]]726{"object": {"a":42}, "num":10.0}727[true, true, false, false, false, false, false, false, false]728[true, true, false, false, false, false, false, false, false]
1833 unmodified lines