Skip to content

Commit

Permalink
Fix slice access out of bound error (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wing924 authored Apr 18, 2023
1 parent 603412b commit 28271d1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.0.1 (2023-04-18)

* Fix slice access out of bound error

## 2.0.0 (2023-04-14)

* Support Go 1.20.
Expand Down
7 changes: 7 additions & 0 deletions v2/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func max[T constraints.Ordered](a T, b T) T {
return b
}

func min[T constraints.Ordered](a T, b T) T {
if a < b {
return a
}
return b
}

func regularizeHosts(hosts []string) []string {
if hosts == nil {
return nil
Expand Down
16 changes: 15 additions & 1 deletion v2/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,24 @@ func (h *host) appendToken(token string, digitMode bool) {
}

func (h *host) Less(rhs *host) bool {
for i := range h.NonDigits {
m := min(len(h.NonDigits), len(rhs.NonDigits))
for i := 0; i < m; i++ {
if h.NonDigits[i] < rhs.NonDigits[i] {
return true
}
if h.NonDigits[i] > rhs.NonDigits[i] {
return false
}
}
if len(h.NonDigits) < len(rhs.NonDigits) {
return true
}
if len(h.NonDigits) > len(rhs.NonDigits) {
return false
}

m = min(len(h.Digits), len(rhs.Digits))
for i := 0; i < m; i++ {
if h.Digits[i].Digit < rhs.Digits[i].Digit {
return true
}
Expand All @@ -244,6 +255,9 @@ func (h *host) Less(rhs *host) bool {
return false
}
}
if len(h.Digits) < len(rhs.Digits) {
return true
}
return false
}

Expand Down
3 changes: 2 additions & 1 deletion v2/pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ func TestPack(t *testing.T) {
{[]string{"abc101z", "abc102z"}, []string{"abc[101-102]z"}},
{[]string{"abc103z", "abc102z", "abc101z"}, []string{"abc[101-103]z"}},
{[]string{"abc101z", "abc102z", "abc104z"}, []string{"abc[101-102,104]z"}},
{[]string{"abc101z", "abc102z", "abc104"}, []string{"abc[101-102]z", "abc104"}},
{[]string{"abc101z", "abc102z", "abc104"}, []string{"abc104", "abc[101-102]z"}},
{[]string{"abc101z-02", "abc102z-02", "abc102z-03"}, []string{"abc[101-102]z-02", "abc102z-03"}},
{[]string{"abc101z-01", "abc102z-02", "abc102z-03"}, []string{"abc101z-01", "abc102z-[02-03]"}},
{[]string{"abc101z-01p", "abc101z-01q", "abc102z-01p", "abc102z-01q"}, []string{"abc[101-102]z-01p", "abc[101-102]z-01q"}},
}
for _, test := range cases {
test := test
Expand Down

0 comments on commit 28271d1

Please sign in to comment.