]> granicus.if.org Git - jq/commitdiff
Conditional exprs are not path exprs (fix #1368)
authorNicolas Williams <nico@cryptonector.com>
Wed, 15 Mar 2017 06:07:37 +0000 (01:07 -0500)
committerNicolas Williams <nico@cryptonector.com>
Wed, 15 Mar 2017 06:07:37 +0000 (01:07 -0500)
The conditional expression in if-then-elif-else-end cannot contribute to
path expressions because it doesn't change the input to any of the then/
elif/else expressions.  These must be generated via gen_subexp().

See also #1366.

src/compile.c
tests/jq.test

index e0bd1406ccf676b8c3a840e4361d0b5ce6ee2e9a..8ab26af1fabf16e06f5fe4af97eb6fbccb20700a 100644 (file)
@@ -905,7 +905,7 @@ static block gen_wildvar_binding(block var, const char* name, block body) {
 }
 
 block gen_cond(block cond, block iftrue, block iffalse) {
-  return BLOCK(gen_op_simple(DUP), cond,
+  return BLOCK(gen_op_simple(DUP), BLOCK(gen_subexp(cond), gen_op_simple(POP)),
                gen_condbranch(BLOCK(gen_op_simple(POP), iftrue),
                               BLOCK(gen_op_simple(POP), iffalse)));
 }
index 963595b004f074b96bc097443654a762b76a9857..ac784ef887d77d71639cd45d8a1848a84da4bc4d 100644 (file)
@@ -1382,4 +1382,9 @@ true
 {"a":null,"b":null}
 {"a":null,"b":"b"}
 
+# Regression test for #1368
+(.. | select(type == "object" and has("b") and (.b | type) == "array")|.b) |= .[0]
+{"a": {"b": [1, {"b": 3}]}}
+{"a": {"b": 1}}
+