Repository navigation
release the allocator when a slice of an allocated array escapes - #335
Merged
Merged
Conversation
itchyny
reviewed
Nov 17, 2026
| a.release(w) | ||
| if _, ok := w.([]any); ok && len(a) > 0 { | ||
| a.releaseSlice(v, args[0]) | ||
| } |
Owner
There was a problem hiding this comment.
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.
Contributor
Author
There was a problem hiding this comment.
Thanks, that's much simpler. Applied your suggestion and dropped releaseSlice in 45dcc88; the regression tests still pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The result is a cyclic value:
.[1][0] | typegives"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
getpathin_modifyreturns 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.yamlnext 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 vetandstaticcheck -checks all,-ST1000 -tags gojq_debug ./...pass.