Skip to content

fix: skip empty header values in bindData to avoid panic - #3171

Open
skyfireitdiy wants to merge 1 commit into
labstack:masterfrom
skyfireitdiy:fix/bindheaders-empty-values
Open

skyfireitdiy wants to merge 1 commit into
labstack:masterfrom
skyfireitdiy:fix/bindheaders-empty-values

Conversation

@skyfireitdiy

Copy link
Copy Markdown

What this PR does

Fixes #3169: BindHeaders panics with index out of range [0] with length 0 when a header value is a nil or empty []string.

The binder now skips entries with no values in both binding paths:

  • map path (map[string]string / map[string]any): skip v when len(v) == 0 before indexing v[0]
  • struct path: skip inputValue when len(inputValue) == 0 before indexing inputValue[0]

This follows the suggestion from the issue ("skipping entries with no values"). The semantics of []string{""} (a single empty string) are unchanged — only truly empty/nil slices are skipped.

Changes

  • bind.go: two len() == 0 guards in bindData
  • bind_test.go: new regression test TestBindHeadersEmptyValues covering nil and empty []string against map[string]string, map[string]any, and struct targets

Verification

  • go test ./... — all packages pass
  • gofmt clean, go vet clean
  • Confirmed the new test fails with the exact panic from the issue on the pre-fix code (reproduced via git stash), and passes with the fix

BindHeaders panics with "index out of range [0] with length 0" when a
header value is a nil or empty []string. Skip entries with no values in
both the map and struct binding paths.

Fixes labstack#3169

Co-Authored-By: Jarvis-AI-42 <skyfireitdiy@gmail.com>
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.

BindHeaders panics on nil or empty header value slices

1 participant