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

fix infinite loop #5

Merged
merged 2 commits into from
Nov 15, 2024
Merged
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
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
}
Comment on lines +213 to +220
Copy link
Owner

Choose a reason for hiding this comment

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

What if comparing numbers1 with numbers2, and strings1 with strings2?

Suggested change
var index int
c1 := len(strings1)
c2 := len(strings2)
if c1 >= c2 {
index = c1
} else {
index = c2
}
if reflect.DeepEqual(numbers1, numbers2) && reflect.DeepEqual(strings1, strings2) {
return 0
}


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++ {
Comment on lines +244 to +253
Copy link
Owner

Choose a reason for hiding this comment

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

Are you sure we need them? if s1 == s2 {} prevents infinite loops, I think.

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
Loading