From 2c4b013ce7f8d80904432b3dded6b3ac17c09cf8 Mon Sep 17 00:00:00 2001 From: ian Date: Mon, 7 Mar 2022 12:35:05 +0200 Subject: [PATCH 1/2] fix infinite loop --- version.go | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/version.go b/version.go index 1d80412..a01b2e4 100644 --- a/version.go +++ b/version.go @@ -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 { @@ -223,6 +232,8 @@ func compare(v1, v2 string) int { return diff } } + + return 0 } func compareString(s1, s2 string) int { @@ -230,7 +241,16 @@ func compareString(s1, s2 string) int { 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])) @@ -245,7 +265,7 @@ func compareString(s1, s2 string) int { return a - b } } - + return 0 } // order function returns the number corresponding to rune From 445c9f83e8427dcc43dc5fd3922b945376a37975 Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Fri, 15 Nov 2024 15:11:16 +0200 Subject: [PATCH 2/2] test: add a case Signed-off-by: knqyf263 --- version_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/version_test.go b/version_test.go index 1288140..b592552 100644 --- a/version_test.go +++ b/version_test.go @@ -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)