Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use go/version to remove dependent on semver #83

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.21
require (
github.com/stretchr/testify v1.8.4
golang.org/x/arch v0.6.0
golang.org/x/mod v0.14.0
)

require (
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc=
golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
59 changes: 23 additions & 36 deletions goversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@

import (
"bytes"
"regexp"
"strings"

"errors"
"go/version"

Check failure on line 23 in goversion.go

View workflow job for this annotation

GitHub Actions / build_and_tests (ubuntu-latest)

package go/version is not in std (/opt/hostedtoolcache/go/1.21.6/x64/src/go/version)
"golang.org/x/arch/x86/x86asm"
"golang.org/x/mod/semver"
"regexp"
)

var goVersionMatcher = regexp.MustCompile(`(go[\d+\.]*(beta|rc)?[\d*])`)
var goVersionMatcher = regexp.MustCompile(`(go[\d+.]*(beta|rc)?[\d*])`)

// GoVersion holds information about the compiler version.
type GoVersion struct {
Expand Down Expand Up @@ -57,22 +56,7 @@
if a == b {
return 0
}
return semver.Compare(buildSemVerString(a), buildSemVerString(b))
}

func buildSemVerString(v string) string {
// First remove the go prefix
tmp := strings.TrimPrefix(v, "go")

// If it has a pre-release, we need to add a dash and patch version of 0.
if strings.Contains(tmp, "beta") {
tmp = strings.ReplaceAll(tmp, "beta", ".0-beta")
}
if strings.Contains(tmp, "rc") {
tmp = strings.ReplaceAll(tmp, "rc", ".0-rc")
}

return "v" + tmp
return version.Compare(a, b)
}

func findGoCompilerVersion(f *GoFile) (*GoVersion, error) {
Expand All @@ -85,24 +69,23 @@
// version string.

data, err := f.fh.getRData()
// If read only data section does not exist, try text.
if err == ErrSectionDoesNotExist {
// If a read-only data section does not exist, try text.
if errors.Is(err, ErrSectionDoesNotExist) {
data, err = f.fh.getCodeSection()
}
if err != nil {
return nil, err
}
notfound := false
for !notfound {
version := matchGoVersionString(data)
if version == "" {
for {
v, ok := matchGoVersionString(data)
if !ok {
return nil, ErrNoGoVersionFound
}
ver := ResolveGoVersion(version)
// Go before 1.4 does not have the version string so if we have found
// a version string below 1.4beta1 it is a false positive.
ver := ResolveGoVersion(v)
// Go before 1.4 does not have the version string, so if we have found
// a version string below 1.4beta1, it is a false positive.
if ver == nil || GoVersionCompare(ver.Name, "go1.4beta1") < 0 {
off := bytes.Index(data, []byte(version))
off := bytes.Index(data, []byte(v))
// No match
if off == -1 {
break
Expand All @@ -120,7 +103,7 @@
// used to identify the version.
// The function returns nil if no version is found.
func tryFromSchedInit(f *GoFile) *GoVersion {
// Check for non supported architectures.
// Check for non-supported architectures.
if f.FileInfo.Arch != Arch386 && f.FileInfo.Arch != ArchAMD64 {
return nil
}
Expand Down Expand Up @@ -151,7 +134,7 @@
}
}

// Check if the functions was found
// Check if the functions were found
if fcn == nil {
// If we can't find the function there is nothing to do.
return nil
Expand Down Expand Up @@ -230,7 +213,7 @@
continue
}

// Likely the version string.
// Likely a version string.
ver := string(bstr)

gover := ResolveGoVersion(ver)
Expand All @@ -245,6 +228,10 @@
return nil
}

func matchGoVersionString(data []byte) string {
return string(goVersionMatcher.Find(data))
func matchGoVersionString(data []byte) (string, bool) {
matched := string(goVersionMatcher.Find(data))
if matched == "" {
return "", false
}
return matched, true
}
Loading