Skip to content

Commit 9e7e939

Browse files
aiskbradfitz
authored andcommitted
cpu: handle vendor suffixes in parseRelease
Kernel release strings such as "3.4.35_hi3535" on some Synology devices failed to parse because only dash and plus suffixes were stripped. Parse the leading digits of each component instead, mirroring the Go runtime fix. This copies the implementation from the Go runtime's parseRelease as updated in CL 781800. Fixes golang/go#80094 Co-authored-by: Brad Fitzpatrick <[email protected]> Change-Id: I41f8fd0a1d6c012eebd5368e623b42e99220d1a3 Reviewed-on: https://go-review.googlesource.com/c/sys/+/793760 Reviewed-by: Jorropo <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> LUCI-TryBot-Result: [email protected] <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Reviewed-by: Junyang Shao <[email protected]>
1 parent f6fb8a1 commit 9e7e939

2 files changed

Lines changed: 47 additions & 26 deletions

File tree

‎cpu/parse.go‎

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,50 @@ package cpu
66

77
import "strconv"
88

9-
// parseRelease parses a dot-separated version number. It follows the semver
10-
// syntax, but allows the minor and patch versions to be elided.
9+
// parseRelease parses a dot-separated version number from the prefix
10+
// of rel. It returns ok=true only if at least the major and minor
11+
// components were successfully parsed; the patch component is
12+
// best-effort. Trailing vendor or build suffixes such as
13+
// "-generic", "+", "_hi3535", or "-rc1" are ignored.
1114
//
1215
// This is a copy of the Go runtime's parseRelease from
13-
// https://golang.org/cl/209597.
16+
// https://golang.org/cl/209597, updated in https://golang.org/cl/781800.
1417
func parseRelease(rel string) (major, minor, patch int, ok bool) {
15-
// Strip anything after a dash or plus.
16-
for i := range len(rel) {
17-
if rel[i] == '-' || rel[i] == '+' {
18-
rel = rel[:i]
19-
break
18+
// next consumes a run of decimal digits from the front of rel,
19+
// returning the parsed value. If the digits are followed by a
20+
// '.', it is consumed and more is set so the caller knows to
21+
// parse another component; otherwise scanning terminates and
22+
// the rest of rel is discarded.
23+
next := func() (n int, more, ok bool) {
24+
i := 0
25+
for i < len(rel) && rel[i] >= '0' && rel[i] <= '9' {
26+
i++
2027
}
21-
}
22-
23-
next := func() (int, bool) {
24-
for i := range len(rel) {
25-
if rel[i] == '.' {
26-
ver, err := strconv.Atoi(rel[:i])
27-
rel = rel[i+1:]
28-
return ver, err == nil
29-
}
28+
if i == 0 {
29+
return 0, false, false
30+
}
31+
n, err := strconv.Atoi(rel[:i])
32+
if err != nil {
33+
return 0, false, false
34+
}
35+
if i < len(rel) && rel[i] == '.' {
36+
rel = rel[i+1:]
37+
return n, true, true
3038
}
31-
ver, err := strconv.Atoi(rel)
3239
rel = ""
33-
return ver, err == nil
40+
return n, false, true
41+
}
42+
43+
var more bool
44+
if major, more, ok = next(); !ok || !more {
45+
return 0, 0, 0, false
3446
}
35-
if major, ok = next(); !ok || rel == "" {
36-
return
47+
if minor, more, ok = next(); !ok {
48+
return 0, 0, 0, false
3749
}
38-
if minor, ok = next(); !ok || rel == "" {
39-
return
50+
if !more {
51+
return major, minor, 0, true
4052
}
41-
patch, ok = next()
42-
return
53+
patch, _, _ = next()
54+
return major, minor, patch, true
4355
}

‎cpu/parse_test.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,21 @@ type parseReleaseTest struct {
1414
var parseReleaseTests = []parseReleaseTest{
1515
{"", -1, -1, -1},
1616
{"x", -1, -1, -1},
17-
{"5", 5, 0, 0},
17+
// A single component is not enough; major+minor are required.
18+
{"5", -1, -1, -1},
19+
{"5-x", -1, -1, -1},
1820
{"5.12", 5, 12, 0},
1921
{"5.12-x", 5, 12, 0},
2022
{"5.12.1", 5, 12, 1},
2123
{"5.12.1-x", 5, 12, 1},
2224
{"5.12.1.0", 5, 12, 1},
25+
{"5.15.0-91-generic", 5, 15, 0},
26+
{"4.19.0+", 4, 19, 0},
27+
{"6.6.0-rc1", 6, 6, 0},
28+
// Synology embedded Linux appends a platform identifier after an
29+
// underscore. See golang/go#79612.
30+
{"3.4.35_hi3535", 3, 4, 35},
31+
{"2.6.32_synology", 2, 6, 32},
2332
{"5.20496382327982653440", -1, -1, -1},
2433
}
2534

0 commit comments

Comments
 (0)