Skip to content

release the allocator when a slice of an allocated array escapes - #335

Merged
itchyny merged 2 commits into
itchyny:mainfrom
waw666waw666:fix-modify-slice-alias
Nov 18, 2026
Merged

itchyny merged 2 commits into
itchyny:mainfrom
waw666waw666:fix-modify-slice-alias

Conversation

@waw666waw666

Copy link
Copy Markdown
Contributor
$ echo '[1,2,3]' | gojq -c '(.[0], .[1:2]) |= (if type == "array" then [.] else 10 end)'
runtime: goroutine stack exceeds 1000000000-byte limit
fatal error: stack overflow

$ echo '[1,2,3]' | jq -c '(.[0], .[1:2]) |= (if type == "array" then [.] else 10 end)'
[10,[2],3]

The result is a cyclic value: .[1][0] | type gives "array" instead of "number", and printing or walking the value overflows the stack. That's a fatal error, so a program using gojq as a library can't recover from it. The same thing happens for nested arrays, e.g. (.a[0], .a[1:3]) |= (if type == "array" then [{x: .}, 0] else 10 end).

When getpath in _modify returns an array and the path contains a slice, this change also checks whether the array being sliced is allocated, and releases the allocator if it is. The extra check only runs when the result is an array and the allocator isn't empty.

I added two cases to cli/test.yaml next to the ones from #306. Without the fix, the first one fails ("array" instead of "number"), and the second one crashes the test binary with a stack overflow. go test -race ./..., go vet and staticcheck -checks all,-ST1000 -tags gojq_debug ./... pass.

Comment thread func.go Outdated
a.release(w)
if _, ok := w.([]any); ok && len(a) > 0 {
a.releaseSlice(v, args[0])
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we only need to check the last path element. If the path ends with an index or a key, w is an element stored as is, so
release(w) already detects it by its address. w can share an allocated backing array under a different address only when the last step is a slice
(this includes .[1:3][1:2]). So we can simply clear the allocator in that case, without walking the path and calling funcIndex2 at each step:

        w := funcGetpath(v, args[0])
        a := args[1].(allocator)
        a.release(w)
        // A slice shares the backing array, but its address differs from the array.
        if p, _ := args[0].([]any); len(p) > 0 && len(a) > 0 {
                if _, ok := p[len(p)-1].(map[string]any); ok {
                        clear(a)
                }
        }
        return w

This also clears when the sliced array was not allocated, but that only causes extra copies later, and keeps the code simple.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's much simpler. Applied your suggestion and dropped releaseSlice in 45dcc88; the regression tests still pass.

@itchyny itchyny left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

@itchyny
itchyny merged commit 1dbc851 into itchyny:main Nov 18, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants