Skip to content

Fix Core panicking on a zero-value Version - #227

Merged
tgross merged 1 commit into
hashicorp:mainfrom
xianjianlf2:fix/core-zero-value-20261012
Oct 13, 2026
Merged

tgross merged 1 commit into
hashicorp:mainfrom
xianjianlf2:fix/core-zero-value-20261012

Conversation

@xianjianlf2

Copy link
Copy Markdown
Contributor

Description

Calling Core() on a zero-value Version panics because its segment slice is empty. Return a new version representing 0.0.0, consistent with the zero value's String and Equal behavior.

The regression test checks the returned version and verifies that the receiver remains unchanged.

Related Issue

No linked issue.

How Has This Been Tested?

The regression test reproduces the panic before the fix.

Passing after the fix:

  • go test -race -count=1 ./...
  • go vet ./...
  • git diff --check

Code, tests, and this draft were prepared with OpenAI Codex.

@xianjianlf2
xianjianlf2 requested a review from a team as a code owner October 12, 2026 09:08
@hashicorp-cla-app

hashicorp-cla-app Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

tgross
tgross previously requested changes Oct 12, 2026

@tgross tgross left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but needs rebase

@xianjianlf2
xianjianlf2 force-pushed the fix/core-zero-value-20261012 branch from c6402f4 to 61d6772 Compare October 13, 2026 02:06

@tgross tgross left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

But FWIW I hate that this Equal comparison works: https://go.dev/play/p/wG8ub8LEjK8

package main

import (
	"fmt"

	"github.com/hashicorp/go-version"
)

func main() {
	v1 := &version.Version{}
	v2 := version.Must(version.NewVersion("0.0.0"))
	fmt.Println("v1:", v1.String())
	fmt.Println("v2:", v2.String())
	fmt.Println(v1.Equal(v2))
	fmt.Println(v1.Core())
}

@tgross
tgross enabled auto-merge (squash) October 13, 2026 13:20
@tgross
tgross dismissed their stale review October 13, 2026 13:20

approved

@tgross
tgross disabled auto-merge October 13, 2026 13:20
@tgross
tgross merged commit c6a3b57 into hashicorp:main Oct 13, 2026
4 checks passed
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.

2 participants