Skip to content

fix(version): no method on a zero-value Version panics - #24

Merged
jonyoder merged 1 commit into
mainfrom
zero-version-string
Aug 7, 2026
Merged

jonyoder merged 1 commit into
mainfrom
zero-version-string

Conversation

@jonyoder

@jonyoder jonyoder commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

What

No method on a zero-value Version panics. Eight of the thirteen exported methods did.

How this was found, and why the original finding is wrong

rstudio/package-manager#19466's F14 reports "a zero-value Version panics in Files" — a go-pyresolver finding. Measured against the code, F14 names the one call site that does not crash and misses the one that does, and the root cause is here rather than there:

Call Actual behavior
go-pyresolver Files(…, Version{}) No panic. Returns %!s(PANIC=…) inside the message; errors.Is(err, ErrFilesUnavailable) still true
go-pyresolver Metadata(…, Version{}) Panics
version.Version{}.String() Panics

The reason is the important part: fmt recovers a panic raised inside a String method. Files formats the version into an error, so it looked healthy and quietly produced a garbled message. Metadata calls ver.String() directly as a map key, so it crashed. A panicking String method is worse than an ordinary panic because it is invisible exactly where it is most likely to be reached.

So rather than fix the symptom in the downstream module, I probed the whole exported surface here:

String       PANIC   index out of range [0] with length 0
BaseVersion  PANIC   (same)
Public       PANIC   (same, via String)
Compare      PANIC   (same, via String's equality fast path)
Equal / GreaterThan / GreaterThanOrEqual / LessThan / LessThanOrEqual
             PANIC   (all via Compare)
Original / Local / IsPreRelease / IsPostRelease   ok

That matters beyond a CLI display bug: every comparison method panicked, and a PubGrub solver compares versions constantly.

Three separate defects

Each was verified load-bearing by reverting it alone and watching the tests fail.

1. release[0] indexed unconditionally in String and BaseVersion. Parse cannot produce an empty release — the grammar requires [0-9]+ — so an empty one always means an uninitialized Version. Now renders "". There is a test asserting that no version Parse accepts renders as "", so the sentinel cannot collide with a real version. writeRelease is extracted so the two renderers cannot drift on the empty case, which is how fixing only one would have left the other broken.

2. real.Compare(Version{}) still died with a nil pointer dereference even after (1). A zero key's pre/post/dev/local are nil Part interfaces, and go-version's Parts.IsAny ranges over elements calling p.IsAny() with no nil check (part/list.go:101). That is reached from part/list.go:54 — before any element is compared, and only on the argument side. Which is precisely why Version{}.Compare(real) returned -1 while real.Compare(Version{}) crashed. That asymmetry was the tell that this was not about release lengths. Compare now decides from the release segment before building a key.

3. Compare padded k2 to len(k2.release) — its own length, a no-op — where it means the longer of the two. No behavior change for parsed versions: the underlying comparison tolerates the mismatch and still returns the right answer, verified across nine pairs with differing segment counts in both directions. This was latent, not wrong, and became fatal only via a nil release.

Semantics chosen

A zero Version renders as "", sorts below every parsed version, and two zero values compare equal.

Sizing

Fixed, not Breaking. Every changed path previously panicked, and no caller can depend on a panic. Parsed-version behavior is unchanged. This is a patch bump.

Verification

  • go test ./... — all packages pass, including internal/proptest, which matters for a comparison change.
  • gofmt -l . — empty.
  • Each of the three fixes reverted individually; the tests fail in each case, with different method sets failing per defect.
  • The zero-value test is deliberately exhaustive over the exported surface rather than covering only the known-broken methods, because the failure mode is somebody adding a method that indexes release[0]. It also exercises both comparison directions — without which defect (2) would have gone unnoticed.

Follow-up

go-pyresolver gets the boundary guard for F14 separately (rejecting an uninitialized Version explicitly rather than treating it as a lookup miss), after it bumps to this release.

🤖 Generated with Claude Code

EIGHT OF THIRTEEN EXPORTED METHODS PANICKED on a zero-value Version, which is
reachable by accident all over Go -- an unset struct field, a map miss, a slice
grown with make. Found while investigating rstudio/package-manager#19466's F14,
which reports it as "a zero-value Version panics in Files" in go-pyresolver. That
finding names the one call site that does NOT crash and misses the one that does;
the root cause is here, not there.

Probed the whole exported surface rather than the two methods already suspected:

    String       PANIC   index out of range [0] with length 0
    BaseVersion  PANIC   (same)
    Public       PANIC   (same, via String)
    Compare      PANIC   (same, via String's fast path)
    Equal / GreaterThan / GreaterThanOrEqual / LessThan / LessThanOrEqual
                 PANIC   (all via Compare)
    Original / Local / IsPreRelease / IsPostRelease   ok

THREE SEPARATE DEFECTS, each verified load-bearing by reverting it alone:

1. release[0] indexed unconditionally in String and BaseVersion. Parse cannot
   produce an empty release -- the grammar requires [0-9]+ -- so an empty one
   always means an uninitialized Version. Now renders "". Asserted that no
   version Parse accepts renders as "", so the sentinel cannot collide.
   Extracted writeRelease so String and BaseVersion cannot drift on the empty
   case, which is how fixing only one of them would leave the other broken.

2. real.Compare(Version{}) died with a NIL POINTER DEREFERENCE even after (1).
   A zero key's pre/post/dev/local are nil Part INTERFACES, and go-version's
   Parts.IsAny ranges over the elements calling p.IsAny() with no nil check
   (part/list.go:101). That call is reached from part/list.go:54, BEFORE any
   element is compared, and only on the argument side -- which is exactly why
   Version{}.Compare(real) returned -1 while real.Compare(Version{}) crashed.
   THE ASYMMETRY WAS THE TELL that this is not about release lengths. Compare
   now decides from the release segment before it ever builds a key.

3. Compare padded k2 to len(k2.release) -- its own length, a no-op -- where it
   means the longer of the two. NO BEHAVIOR CHANGE FOR PARSED VERSIONS: the
   underlying comparison tolerates the mismatch and still returns the right
   answer, verified across nine pairs with differing segment counts in both
   directions, which is why this sat latent. It only became fatal via a nil
   release.

⚠️ A PANICKING String METHOD IS WORSE THAN AN ORDINARY PANIC, because fmt
RECOVERS it. `fmt.Errorf("%s", v)` returned an error containing
"%!s(PANIC=String method: ...)" and reported no failure at all, so the bug was
invisible precisely where it was most likely to be hit, while a direct call
crashed. go-pyresolver's Files() is exactly that case: it formats the version
into an error, so it looked healthy and returned a garbled message, while
Metadata() calls ver.String() directly and crashed. That is why F14 pointed at
the wrong function.

The zero-value test is deliberately exhaustive over the exported surface rather
than covering only the methods known to be broken, since the failure mode is
somebody adding a method that indexes release[0]. It also exercises BOTH
comparison directions, without which defect (2) would have gone unnoticed.

Sizing: Fixed, not Breaking. Every changed path previously panicked, and no
caller can depend on a panic. Parsed-version behavior is unchanged.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@jonyoder
jonyoder merged commit 448c491 into main Aug 7, 2026
2 checks passed
@jonyoder
jonyoder deleted the zero-version-string branch August 7, 2026 18:47
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