Skip to content

fix: handle nil values in concatMaps to prevent SIGSEGV - #1201

Open
lxcxjxhx wants to merge 1 commit into
cloudwego:mainfrom
lxcxjxhx:fix/1181-concat-nil-ptr-deref
Open

fix: handle nil values in concatMaps to prevent SIGSEGV#1201
lxcxjxhx wants to merge 1 commit into
cloudwego:mainfrom
lxcxjxhx:fix/1181-concat-nil-ptr-deref

Conversation

@lxcxjxhx

Copy link
Copy Markdown

Description

Fix a nil pointer dereference (SIGSEGV) panic in concatMaps when merging map[string]any stream chunks where a key is nil in an earlier chunk and set in a later chunk.

Root Cause

toSliceValue calls reflect.TypeOf(vs[0]) where vs is a []any. When the first element is nil, reflect.TypeOf returns nil, causing reflect.SliceOf(nil) to panic with SIGSEGV.

This happens in concatMaps at line 151 when collecting per-key values across chunks — nil values from map entries get appended to the []any slice via reflect.Append, and toSliceValue doesn't handle nil elements.

Fix

In toSliceValue, filter out nil elements before processing. If all elements are nil, return an empty []any slice. 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 in toSliceValue
  • internal/concat_test.go: Add test cases for nil-first, nil-last, and mixed-nil scenarios

Testing

  • go test ./internal/... -count=1 — all pass
  • Reproducer from issue: ConcatItems([]map[string]any{{"key": nil}, {"key": "value"}}) no longer panics

Related

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[internal/concat] ConcatItems panics (SIGSEGV) when a map[string]any stream key is nil in an earlier chunk and set in a later chunk

1 participant