From 1d053afddcf75f06d3711f96a60eb61b91f195d2 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 14 Apr 2022 12:19:39 -0700 Subject: [PATCH] libct/cg/sd: stop using regex, fix systemdVersionAtoi Rewrite systemdVersionAtoi to not use regexp, and fix two issues: 1. It was returning 0 (rather than -1) for some errors. 2. The comment was saying that the input string is without quotes, while in fact it is. Note the new function, similar to the old one, works on input either with or without quotes. Amend the test to add test cases without quotes. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/systemd/common.go | 22 +++++++++++--------- libcontainer/cgroups/systemd/systemd_test.go | 7 +++++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/libcontainer/cgroups/systemd/common.go b/libcontainer/cgroups/systemd/common.go index 98ccc51655c..3e559ccee3b 100644 --- a/libcontainer/cgroups/systemd/common.go +++ b/libcontainer/cgroups/systemd/common.go @@ -7,7 +7,6 @@ import ( "fmt" "math" "os" - "regexp" "strconv" "strings" "sync" @@ -441,18 +440,21 @@ func systemdVersion(cm *dbusConnManager) int { return version } -func systemdVersionAtoi(verStr string) (int, error) { - // verStr should be of the form: - // "v245.4-1.fc32", "245", "v245-1.fc32", "245-1.fc32" (without quotes). +func systemdVersionAtoi(str string) (int, error) { + // str should be of the form: + // "v245.4-1.fc32", "245", "v245-1.fc32", "245-1.fc32" (with quotes). // The result for all of the above should be 245. - // Thus, we unconditionally remove the "v" prefix + // Thus, we unconditionally remove the "v prefix // and then match on the first integer we can grab. - re := regexp.MustCompile(`v?([0-9]+)`) - matches := re.FindStringSubmatch(verStr) - if len(matches) < 2 { - return 0, fmt.Errorf("can't parse version %s: incorrect number of matches %v", verStr, matches) + str = strings.TrimLeft(str, `"v`) + for i := 0; i < len(str); i++ { + if str[i] < '0' || str[i] > '9' { + // First non-digit. + str = str[:i] + break + } } - ver, err := strconv.Atoi(matches[1]) + ver, err := strconv.Atoi(str) if err != nil { return -1, fmt.Errorf("can't parse version: %w", err) } diff --git a/libcontainer/cgroups/systemd/systemd_test.go b/libcontainer/cgroups/systemd/systemd_test.go index 7417bf28789..0d4fae361f8 100644 --- a/libcontainer/cgroups/systemd/systemd_test.go +++ b/libcontainer/cgroups/systemd/systemd_test.go @@ -40,8 +40,11 @@ func TestSystemdVersion(t *testing.T) { {`"v245.4-1.fc32"`, 245, false}, {`"241-1"`, 241, false}, {`"v241-1"`, 241, false}, - {"NaN", 0, true}, - {"", 0, true}, + {`333.45"`, 333, false}, + {`v321-0`, 321, false}, + {"NaN", -1, true}, + {"", -1, true}, + {"v", -1, true}, } for _, sdTest := range systemdVersionTests { ver, err := systemdVersionAtoi(sdTest.verStr)