From 4eb9ff2abf3f3dd85d2819a73b19385ef7b0bfe0 Mon Sep 17 00:00:00 2001 From: Wilmer Paulino Date: Fri, 3 May 2019 11:22:05 -0700 Subject: [PATCH] tor: use string comparison to check min supported version The current method would not allow version strings like 0.4.0.5 since it would check every number of the version string individually. --- tor/controller.go | 24 ++++++++++-------------- tor/controller_test.go | 8 ++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/tor/controller.go b/tor/controller.go index aa8f74d5..d58631dd 100644 --- a/tor/controller.go +++ b/tor/controller.go @@ -325,7 +325,6 @@ func computeHMAC256(key, message []byte) []byte { func supportsV3(version string) error { // We'll split the minimum Tor version that's supported and the given // version in order to individually compare each number. - requiredParts := strings.Split(MinTorVersion, ".") parts := strings.Split(version, ".") if len(parts) != 4 { return errors.New("version string is not of the format " + @@ -338,22 +337,19 @@ func supportsV3(version string) error { build := strings.Split(parts[len(parts)-1], "-") parts[len(parts)-1] = build[0] - // Convert them each number from its string representation to integers - // and check that they respect the minimum version. - for i := range parts { - n, err := strconv.Atoi(parts[i]) - if err != nil { - return err - } - requiredN, err := strconv.Atoi(requiredParts[i]) - if err != nil { + // Ensure that each part of the version string corresponds to a number. + for _, part := range parts { + if _, err := strconv.Atoi(part); err != nil { return err } + } - if n < requiredN { - return fmt.Errorf("version %v below minimum version "+ - "supported %v", version, MinTorVersion) - } + // Once we've determined we have a proper version string of the format + // major.minor.revision.build, we can just do a string comparison to + // determine if it satisfies the minimum version supported. + if version < MinTorVersion { + return fmt.Errorf("version %v below minimum version supported "+ + "%v", version, MinTorVersion) } return nil diff --git a/tor/controller_test.go b/tor/controller_test.go index 7ef9ca14..b040a4f9 100644 --- a/tor/controller_test.go +++ b/tor/controller_test.go @@ -28,6 +28,10 @@ func TestParseTorVersion(t *testing.T) { version: "0.4.3.6", valid: true, }, + { + version: "0.4.0.5", + valid: true, + }, { version: "1.3.3.6", valid: true, @@ -56,6 +60,10 @@ func TestParseTorVersion(t *testing.T) { version: "0.1.3.6", valid: false, }, + { + version: "0.0.6.3", + valid: false, + }, } for i, test := range tests {