Skip to content

fix: guard against nil Version in comparisons, collections, and constraint checks - #327

Open
mazkev wants to merge 1 commit into
Masterminds:masterfrom
mazkev:fix/nil-version-safety
Open

mazkev wants to merge 1 commit into
Masterminds:masterfrom
mazkev:fix/nil-version-safety

Conversation

@mazkev

@mazkev mazkev commented Nov 11, 2026

Copy link
Copy Markdown

Summary

This PR prevents runtime panics when working with nil *Version pointers across Compare, derived comparison methods (LessThan, LessThanEqual, GreaterThan, GreaterThanEqual), Collection sorting, and Constraints.Check/Validate.

Details

  1. Version.Compare:

    • Version.Equal was previously patched in feat: nil version equality #213 to safely handle nil pointers without panicking.
    • However, calling Compare (or LessThan, GreaterThan, etc.) when either receiver or argument is nil triggered runtime panics due to method calls on nil pointers (Major(), etc.).
    • Added guards treating nil as smaller than any non-nil *Version, and equal to another nil *Version.
  2. Collection.Less:

    • Safely sorts collections containing nil elements (nil versions are sorted to the front without panics).
  3. Constraints.Check & Constraints.Validate:

    • Check(nil) safely returns false instead of panicking.
    • Validate(nil) safely returns false, []error{errors.New("version cannot be nil")}.
  4. Tests:

    • Added TestNilVersionComparisons in version_test.go.
    • Added TestCollectionWithNil in collection_test.go.
    • Added TestConstraintsNilVersion in constraints_test.go.
    • Verified that all unit tests pass on both 64-bit and 32-bit architectures (GOARCH=386), and go vet is clean.

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