Skip to content

cmp/cmpopts: prevent nil pointer dereference on nil value in EquateComparable - #401

Open
aoright wants to merge 1 commit into
google:masterfrom
aoright:fix/equate-comparable-nil-guard
Open

cmp/cmpopts: prevent nil pointer dereference on nil value in EquateComparable#401
aoright wants to merge 1 commit into
google:masterfrom
aoright:fix/equate-comparable-nil-guard

Conversation

@aoright

@aoright aoright commented Aug 19, 2026

Copy link
Copy Markdown

Problem

In EquateComparable, passing an untyped nil value results in reflect.TypeOf(typ) returning nil. Attempting to call t.Comparable() directly on nil leads to an unhandled runtime nil pointer dereference panic.

Solution

  • Guard against t == nil in EquateComparable before calling t.Comparable(), raising the formatted panic message consistently.
  • Add test cases in util_test.go for invalid inputs (nil and non-comparable function).

…mparable

Signed-off-by: aoright <102943475+aoright@users.noreply.github.com>
@google-cla

google-cla Bot commented Aug 19, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

1 participant