Skip to content

Commit

Permalink
fix crash on assignment operators with multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Dec 24, 2022
1 parent afc4c6c commit e4b9b6a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
10 changes: 6 additions & 4 deletions cli/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4517,13 +4517,15 @@
expected: |
{"bar":42,"foo":42}
- name: assignment operator query with multiple paths
- name: assignment operator query with multiple paths and values
args:
- -c
- '(.foo,.bar,.baz) = .bar + 1'
input: '{"bar":42}'
- '(.foo,.bar,.baz) = .bar[]'
input: '{"bar":[0,1,2]}'
expected: |
{"bar":43,"baz":43,"foo":43}
{"bar":0,"baz":0,"foo":0}
{"bar":1,"baz":1,"foo":1}
{"bar":2,"baz":2,"foo":2}
- name: assignment operator array index negative error
args:
Expand Down
11 changes: 6 additions & 5 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ func (c *compiler) compileFunc(e *Func) error {
// Appends the compiled code for the assignment operator (`=`) to maximize
// performance. Originally the operator was implemented as follows.
//
// def _assign(p; $x): reduce path(p) as $p (.; setpath($p; $x));
// def _assign(p; $x): reduce path(p) as $q (.; setpath($q; $x));
//
// To overcome the difficulty of reducing allocations on `setpath`, we use the
// `allocator` type and track the allocated addresses during the reduction.
Expand All @@ -1023,8 +1023,9 @@ func (c *compiler) compileAssign() {
scope := c.newScope()
v, p := [2]int{scope.id, 0}, [2]int{scope.id, 1}
x, a := [2]int{scope.id, 2}, [2]int{scope.id, 3}
q := [2]int{scope.id, 4} // Cannot reuse p due to backtracking in x.
c.appends(
&code{op: opscope, v: [3]int{scope.id, 4, 2}},
&code{op: opscope, v: [3]int{scope.id, 5, 2}},
&code{op: opstore, v: v}, // def _assign(p; $x):
&code{op: opstore, v: p},
&code{op: opstore, v: x},
Expand All @@ -1044,10 +1045,10 @@ func (c *compiler) compileAssign() {
&code{op: opcallpc},
&code{op: opload, v: v},
&code{op: oppathend},
&code{op: opstore, v: p}, // as $p (.;
&code{op: opload, v: a}, // setpath($p; $x)
&code{op: opstore, v: q}, // as $q (.;
&code{op: opload, v: a}, // setpath($q; $x)
&code{op: opload, v: x},
&code{op: opload, v: p},
&code{op: opload, v: q},
&code{op: opload, v: v},
&code{op: opcall, v: [3]interface{}{funcSetpathWithAllocator, 3, "_setpath"}},
&code{op: opstore, v: v},
Expand Down

0 comments on commit e4b9b6a

Please sign in to comment.