fix: handle nil values in concatMaps to prevent SIGSEGV - #1201
Open
lxcxjxhx wants to merge 1 commit into
Open
Conversation
toSliceValue panics with nil pointer dereference when a map[string]any chunk contains nil values that appear before non-nil values for the same key. reflect.TypeOf(nil) returns nil, causing reflect.SliceOf(nil) to crash. Filter out nil elements in toSliceValue and return an empty []any slice when all elements are nil, making concat behavior symmetric regardless of chunk ordering. Fixes cloudwego#1181
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.
Description
Fix a nil pointer dereference (SIGSEGV) panic in
concatMapswhen mergingmap[string]anystream chunks where a key isnilin an earlier chunk and set in a later chunk.Root Cause
toSliceValuecallsreflect.TypeOf(vs[0])wherevsis a[]any. When the first element isnil,reflect.TypeOfreturnsnil, causingreflect.SliceOf(nil)to panic with SIGSEGV.This happens in
concatMapsat line 151 when collecting per-key values across chunks — nil values from map entries get appended to the[]anyslice viareflect.Append, andtoSliceValuedoesn't handle nil elements.Fix
In
toSliceValue, filter out nil elements before processing. If all elements are nil, return an empty[]anyslice. Otherwise, use the first non-nil element's type for the slice type. This makes concat behavior symmetric regardless of whether nil values appear before or after non-nil values.Changes
internal/concat.go: Handle nil elements intoSliceValueinternal/concat_test.go: Add test cases for nil-first, nil-last, and mixed-nil scenariosTesting
go test ./internal/... -count=1— all passConcatItems([]map[string]any{{"key": nil}, {"key": "value"}})no longer panicsRelated