Skip to content

Commit f6508a5

Browse files
author
Andrei Aaron
committed
rewrite parseBlockSize to make into consideration only realistic values
Signed-off-by: Andrei Aaron <andaaron@cisco.com>
1 parent 684a946 commit f6508a5

File tree

2 files changed

+13
-29
lines changed

2 files changed

+13
-29
lines changed

smartpqi/arcconf.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,19 @@ const (
1616
noArcConfRC = 127
1717
)
1818

19-
// parseBlockSize parses a block size token that may use SI suffixes
20-
// (e.g. "512", "4K", "1M"). Returns the value in bytes.
19+
// parseBlockSize converts a native disk block/sector size token from arcconf
20+
// output into bytes. Known values are "512" (traditional) and "4K" (4K-native
21+
// drives). This is not intended for parsing human-readable capacity fields
22+
// like "SizeMB" which would require int64 for large disks.
2123
func parseBlockSize(raw string) (int, error) {
22-
s := strings.TrimSpace(raw)
23-
if len(s) == 0 {
24-
return 0, fmt.Errorf("empty block size token")
25-
}
26-
27-
multiplier := 1
28-
suffix := strings.ToLower(s[len(s)-1:])
29-
switch suffix {
30-
case "k":
31-
multiplier = 1024
32-
s = s[:len(s)-1]
33-
case "m":
34-
multiplier = 1024 * 1024
35-
s = s[:len(s)-1]
36-
case "g":
37-
multiplier = 1024 * 1024 * 1024
38-
s = s[:len(s)-1]
39-
}
40-
41-
v, err := strconv.Atoi(s)
42-
if err != nil {
43-
return 0, err
24+
switch strings.TrimSpace(raw) {
25+
case "512":
26+
return 512, nil
27+
case "4K", "4k", "4096":
28+
return 4096, nil
29+
default:
30+
return 0, fmt.Errorf("unsupported block size value %q", raw)
4431
}
45-
46-
return v * multiplier, nil
4732
}
4833

4934
// splitLogicalDevices extracts individual logical device text blocks from raw

smartpqi/arcconf_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,11 +861,10 @@ func TestParseBlockSize(t *testing.T) {
861861
{"4096", 4096, false},
862862
{"4K", 4096, false},
863863
{"4k", 4096, false},
864-
{"1M", 1048576, false},
865-
{"1G", 1073741824, false},
866864
{"", 0, true},
867865
{"abc", 0, true},
868-
{"K", 0, true},
866+
{"1M", 0, true},
867+
{"1G", 0, true},
869868
}
870869

871870
for _, tc := range testCases {

0 commit comments

Comments
 (0)