Skip to content

fix(baked_in): handle "nil" param in excluded_if for pointer fields - #1585

Open
64johnlee wants to merge 1 commit into
go-playground:masterfrom
64johnlee:fix-excluded-if-nil-param-1320
Open

fix(baked_in): handle "nil" param in excluded_if for pointer fields#1585
64johnlee wants to merge 1 commit into
go-playground:masterfrom
64johnlee:fix-excluded-if-nil-param-1320

Conversation

@64johnlee

Copy link
Copy Markdown

Summary

Fixes #1320excluded_if panics with strconv.ParseInt: parsing "nil": invalid syntax when a tag like excluded_if=W nil is used and W is a non-nil pointer-to-numeric type.

Root cause

requireCheckFieldValue uses extractTypeInternal to resolve the comparison field, which dereferences non-nil pointers to their underlying type. So a *int field whose value is non-nil arrives at the reflect.Int switch case. The function then called asInt("nil") unconditionally, which panics.

The same bug exists for *uint, *float32, *float64, and *[N]T comparisons against "nil".

Fix

  • Add an early if value == "nil" { return false } guard in the Int, Uint, Float32, Float64, and Array cases of requireCheckFieldValue. A fully-dereferenced numeric/array value can never be nil, so the comparison is always false.
  • Simplify the reflect.Ptr case: extractTypeInternal only returns kind == reflect.Ptr for nil pointers (non-nil ones are dereferenced to their element kind), so the non-nil recursive branch was dead code and has been removed.

Test

Two new sub-cases added to TestExcludedIf:

  • W *int non-nil, R *int non-nil with excluded_if=W nil — previously panicked, now returns nil error.
  • W *int nil, R *int nil with excluded_if=W nil — condition fires, R is absent, passes.

All existing tests continue to pass (go test ./...).

Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com

When a struct field is a pointer-to-numeric type (e.g. *int) and the
excluded_if tag compares it against the literal "nil", extractTypeInternal
dereferences a non-nil pointer to its underlying kind (e.g. reflect.Int).
The switch then called asInt("nil") which panics via strconv.ParseInt.

Fix: add an early `value == "nil"` guard in all numeric and array cases
of requireCheckFieldValue that return false immediately (a non-nil, fully
dereferenced numeric/array value can never equal nil). Also simplify the
reflect.Ptr case: because extractTypeInternal only returns Ptr kind for
nil pointers, the non-nil recursive branch was unreachable and has been
removed.

Closes go-playground#1320

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@64johnlee
64johnlee requested a review from a team as a code owner June 9, 2026 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant