Skip to content

Commit

Permalink
fix infinite loop (#5)
Browse files Browse the repository at this point in the history
* fix infinite loop

* test: add a case

Signed-off-by: knqyf263 <[email protected]>

---------

Signed-off-by: knqyf263 <[email protected]>
Co-authored-by: knqyf263 <[email protected]>
  • Loading branch information
iandri and knqyf263 authored Nov 15, 2024
1 parent 3ed183d commit 61cdcc2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
26 changes: 23 additions & 3 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,16 @@ func compare(v1, v2 string) int {
strings2 = append([]string{""}, strings2...)
}

for i := 0; ; i++ {
var index int
c1 := len(strings1)
c2 := len(strings2)
if c1 >= c2 {
index = c1
} else {
index = c2
}

for i := 0; i < index; i++ {
// Compare non-digit strings
diff := compareString(strings1.get(i), strings2.get(i))
if diff != 0 {
Expand All @@ -223,14 +232,25 @@ func compare(v1, v2 string) int {
return diff
}
}

return 0
}

func compareString(s1, s2 string) int {
if s1 == s2 {
return 0
}

for i := 0; ; i++ {
var index int
c1 := len(s1)
c2 := len(s2)
if c1 >= c2 {
index = c1
} else {
index = c2
}

for i := 0; i < index; i++ {
a := 0
if i < len(s1) {
a = order(rune(s1[i]))
Expand All @@ -245,7 +265,7 @@ func compareString(s1, s2 string) int {
return a - b
}
}

return 0
}

// order function returns the number corresponding to rune
Expand Down
1 change: 1 addition & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ func TestEqual(t *testing.T) {
{Version{2, "7.4.052", "1ubuntu3"}, Version{2, "7.4.052", "1ubuntu3"}, true},
{Version{2, "7.4.052", "1ubuntu1"}, Version{2, "7.4.052", "1"}, false},
{Version{upstreamVersion: "7.4.052"}, Version{upstreamVersion: "7.4.052"}, true},
{Version{upstreamVersion: "3.12", debianRevision: "1+deb9u1build0.16.4.1"}, Version{upstreamVersion: "3.12", debianRevision: "1+deb9u1build0.16.04.1"}, true},
}
for _, tc := range cases {
actual := tc.v1.Equal(tc.v2)
Expand Down

0 comments on commit 61cdcc2

Please sign in to comment.