Skip to content
Myr

detect invalid path expression (fix #862)

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

authored by David Tolnay · committed by David Tolnay · parent 5856a2d · Browse files · 4 files+129 added−11 removed
This commit

State

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

Diff stats

4 files+129 added−11 removed

  • tests:+36 added−0 removed
  • other:+93 added−11 removed

4 files+129 added−11 removed

modified:tests/jq.test+36 added
693 unmodified lines
694path(.)69542696[]697698try path(.a | map(select(.b == 0))) catch .699{"a":[{"b":0}]}700"Invalid path expression with result [{\"b\":0}]"701702try path(.a | map(select(.b == 0)) | .[0]) catch .703{"a":[{"b":0}]}704"Invalid path expression near attempt to access element 0 of [{\"b\":0}]"705706try path(.a | map(select(.b == 0)) | .c) catch .707{"a":[{"b":0}]}708"Invalid path expression near attempt to access element \"c\" of [{\"b\":0}]"709710try path(.a | map(select(.b == 0)) | .[]) catch .711{"a":[{"b":0}]}712"Invalid path expression near attempt to iterate through [{\"b\":0}]"713714path(.a[path(.b)[0]])715{"a":{"b":0}}716["a","b"]717718[paths]719[1,[[],{"a":2}]]720[[0],[1],[1,0],[1,1],[1,1,"a"]]721
80 unmodified lines
802.foo[2].bar = 1803{"foo":[11], "bar":42}804{"foo":[11,null,{"bar":1}], "bar":42}805806try ((map(select(.a == 1))[].b) = 10) catch .807[{"a":0},{"a":1}]808"Invalid path expression near attempt to iterate through [{\"a\":1}]"809810try ((map(select(.a == 1))[].a) |= .+1) catch .811[{"a":0},{"a":1}]812"Invalid path expression near attempt to iterate through [{\"a\":1}]"813814def x: .[1,2]; x=10815[0,1,2]816[0,10,10]817818try (def x: reverse; x=10) catch .819[0,1,2]820"Invalid path expression with result [2,1,0]"821822#823# Conditionals824#825
399 unmodified lines
modified:execute.c+65 added-7 removed
32 unmodified lines
33 stack_ptr stk_top;34 stack_ptr fork_top;3536 jv path;37 jv value_at_path;38 int subexp_nest;39 int debug_trace_enabled;40 int initial_execution;41 unsigned next_label;
149 unmodified lines
191struct forkpoint {192 stack_ptr saved_data_stack;193 stack_ptr saved_curr_frame;194 int path_len, subexp_nest;195 jv value_at_path;196 uint16_t* return_address;197};198199struct stack_pos {
11 unmodified lines
211 fork->saved_data_stack = jq->stk_top;212 fork->saved_curr_frame = jq->curr_frame;213 fork->path_len =214 jv_get_kind(jq->path) == JV_KIND_ARRAY ? jv_array_length(jv_copy(jq->path)) : 0;215 fork->value_at_path = jv_copy(jq->value_at_path);216 fork->subexp_nest = jq->subexp_nest;217 fork->return_address = retaddr;218 jq->stk_top = sp.saved_data_stack;219 jq->curr_frame = sp.saved_curr_frame;220}221219void path_append(jq_state* jq, jv component) {222static int path_intact(jq_state *jq, jv curr) {223 if (jq->subexp_nest == 0 && jv_get_kind(jq->path) == JV_KIND_ARRAY) {224 return jv_identical(curr, jv_copy(jq->value_at_path));225 } else {226 jv_free(curr);227 return 1;228 }229}230231static void path_append(jq_state* jq, jv component, jv value_at_path) {232 if (jq->subexp_nest == 0 && jv_get_kind(jq->path) == JV_KIND_ARRAY) {233 int n1 = jv_array_length(jv_copy(jq->path));234 jq->path = jv_array_append(jq->path, component);235 int n2 = jv_array_length(jv_copy(jq->path));236 assert(n2 == n1 + 1);237 jv_free(jq->value_at_path);238 jq->value_at_path = value_at_path;239 } else {240 jv_free(component);241 jv_free(value_at_path);242 }243}244245uint16_t* stack_restore(jq_state *jq){
21 unmodified lines
267 jq->path = jv_array_slice(jq->path, 0, path_len);268 } else {269 assert(path_len == 0);270 }271 jv_free(jq->value_at_path);272 jq->value_at_path = fork->value_at_path;273 jq->subexp_nest = fork->subexp_nest;274 jq->fork_top = stack_pop_block(&jq->stk, jq->fork_top, sizeof(struct forkpoint));275 return retaddr;276}
10 unmodified lines
287288 if (jv_get_kind(jq->path) != JV_KIND_INVALID)289 jv_free(jq->path);290 jq->path = jv_null();291 jv_free(jq->value_at_path);292 jq->value_at_path = jv_null();293 jq->subexp_nest = 0;294}295296void jq_report_error(jq_state *jq, jv value) {
257 unmodified lines
554555 stack_save(jq, pc - 1, stack_get_pos(jq));556557 stack_push(jq, jv_number(jq->subexp_nest));539 stack_push(jq, v);558 stack_push(jq, jq->value_at_path);559 stack_push(jq, jv_copy(v));560561 jq->path = jv_array();562 jq->value_at_path = v; // next INDEX operation must index into v563 jq->subexp_nest = 0;564 break;565 }566567 case PATH_END: {568 jv v = stack_pop(jq);569 // detect invalid path expression like path(.a | reverse)570 if (!path_intact(jq, jv_copy(v))) {571 char errbuf[30];572 jv msg = jv_string_fmt(573 "Invalid path expression with result %s",574 jv_dump_string_trunc(v, errbuf, sizeof(errbuf)));575 set_error(jq, jv_invalid_with_msg(msg));576 goto do_backtrack;577 }578 jv_free(v); // discard value, only keep path579580 jv old_value_at_path = stack_pop(jq);581 int old_subexp_nest = (int)jv_number_value(stack_pop(jq));582583 jv path = jq->path;584 jq->path = stack_pop(jq);
3 unmodified lines
588 stack_save(jq, pc - 1, spos);589590 stack_push(jq, path);591 jq->subexp_nest = old_subexp_nest;592 jv_free(jq->value_at_path);593 jq->value_at_path = old_value_at_path;594 break;595 }596597 case ON_BACKTRACK(PATH_BEGIN):
6 unmodified lines
604 case INDEX:605 case INDEX_OPT: {606 jv t = stack_pop(jq);607 jv k = stack_pop(jq);575 path_append(jq, jv_copy(k));576 jv v = jv_get(t, k);608 // detect invalid path expression like path(reverse | .a)609 if (!path_intact(jq, jv_copy(t))) {610 char keybuf[15];611 char objbuf[30];612 jv msg = jv_string_fmt(613 "Invalid path expression near attempt to access element %s of %s",614 jv_dump_string_trunc(k, keybuf, sizeof(keybuf)),615 jv_dump_string_trunc(t, objbuf, sizeof(objbuf)));616 set_error(jq, jv_invalid_with_msg(msg));617 goto do_backtrack;618 }619 jv v = jv_get(t, jv_copy(k));620 if (jv_is_valid(v)) {621 path_append(jq, k, jv_copy(v));622 stack_push(jq, v);623 } else {624 jv_free(k);625 if (opcode == INDEX)626 set_error(jq, v);627 else628 jv_free(v);
20 unmodified lines
649 break;650 }651652 case EACH:608 case EACH_OPT:653 case EACH_OPT: {654 jv container = stack_pop(jq);655 // detect invalid path expression like path(reverse | .[])656 if (!path_intact(jq, jv_copy(container))) {657 char errbuf[30];658 jv msg = jv_string_fmt(659 "Invalid path expression near attempt to iterate through %s",660 jv_dump_string_trunc(container, errbuf, sizeof(errbuf)));661 set_error(jq, jv_invalid_with_msg(msg));662 goto do_backtrack;663 }664 stack_push(jq, container);665 stack_push(jq, jv_number(-1));666 // fallthrough667 }668 case ON_BACKTRACK(EACH):669 case ON_BACKTRACK(EACH_OPT): {670 int idx = jv_number_value(stack_pop(jq));671 jv container = stack_pop(jq);
37 unmodified lines
709 goto do_backtrack;710 } else if (is_last) {711 // we don't need to make a backtrack point712 jv_free(container);656 path_append(jq, key);713 path_append(jq, key, jv_copy(value));714 stack_push(jq, value);715 } else {716 struct stack_pos spos = stack_get_pos(jq);717 stack_push(jq, container);718 stack_push(jq, jv_number(idx));719 stack_save(jq, pc - 1, spos);663 path_append(jq, key);720 path_append(jq, key, jv_copy(value));721 stack_push(jq, value);722 }723 break;724 }
211 unmodified lines
936 jq->err_cb_data = stderr;937938 jq->attrs = jv_object();939 jq->path = jv_null();940 jq->value_at_path = jv_null();941 return jq;942}943944void jq_set_error_cb(jq_state *jq, jq_msg_cb cb, void *data) {
211 unmodified lines
modified:jv.c+27 added-4 removed
135 unmodified lines
136 * Numbers137 */138139jv jv_number(double x) {140 jv j;141 j.kind_flags = JV_KIND_NUMBER;142 j.size = 0;143 j.u.number = x;140 jv j = {JV_KIND_NUMBER, 0, 0, 0, {.number = x}};141 return j;142}143144double jv_number_value(jv j) {
1142 unmodified lines
1287 jv_free(b);1288 return r;1289}12901291int jv_identical(jv a, jv b) {1292 int r;1293 if (a.kind_flags != b.kind_flags1294 || a.offset != b.offset1295 || a.size != b.size) {1296 r = 0;1297 } else {1298 switch (jv_get_kind(a)) {1299 case JV_KIND_ARRAY:1300 case JV_KIND_STRING:1301 case JV_KIND_OBJECT:1302 r = a.u.ptr == b.u.ptr;1303 break;1304 case JV_KIND_NUMBER:1305 r = a.u.number == b.u.number;1306 break;1307 default:1308 r = 1;1309 break;1310 }1311 }1312 jv_free(a);1313 jv_free(b);1314 return r;1315}13161317int jv_contains(jv a, jv b) {1318 int r = 1;1319 if (jv_get_kind(a) != jv_get_kind(b)) {1320 r = 0;
13 unmodified lines
modified:jv.h+1 added
44 unmodified lines
4546int jv_get_refcnt(jv);4748int jv_equal(jv, jv);49int jv_identical(jv, jv);50int jv_contains(jv, jv);5152jv jv_invalid(void);53jv jv_invalid_with_msg(jv);
187 unmodified lines