Repository navigation
fix(version): no method on a zero-value Version panics - #24
Merged
Merged
Conversation
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>
This was referenced Aug 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
No method on a zero-value
Versionpanics. 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
Versionpanics inFiles" — ago-pyresolverfinding. 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:go-pyresolverFiles(…, Version{})%!s(PANIC=…)inside the message;errors.Is(err, ErrFilesUnavailable)still truego-pyresolverMetadata(…, Version{})version.Version{}.String()The reason is the important part:
fmtrecovers a panic raised inside aStringmethod.Filesformats the version into an error, so it looked healthy and quietly produced a garbled message.Metadatacallsver.String()directly as a map key, so it crashed. A panickingStringmethod 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:
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 inStringandBaseVersion.Parsecannot produce an empty release — the grammar requires[0-9]+— so an empty one always means an uninitializedVersion. Now renders"". There is a test asserting that no versionParseaccepts renders as"", so the sentinel cannot collide with a real version.writeReleaseis 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'spre/post/dev/localare nilPartinterfaces, andgo-version'sParts.IsAnyranges over elements callingp.IsAny()with no nil check (part/list.go:101). That is reached frompart/list.go:54— before any element is compared, and only on the argument side. Which is precisely whyVersion{}.Compare(real)returned-1whilereal.Compare(Version{})crashed. That asymmetry was the tell that this was not about release lengths.Comparenow decides from the release segment before building a key.3.
Comparepaddedk2tolen(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
Versionrenders 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, includinginternal/proptest, which matters for a comparison change.gofmt -l .— empty.release[0]. It also exercises both comparison directions — without which defect (2) would have gone unnoticed.Follow-up
go-pyresolvergets the boundary guard for F14 separately (rejecting an uninitializedVersionexplicitly rather than treating it as a lookup miss), after it bumps to this release.🤖 Generated with Claude Code