Skip to content

Commit b7f5cdf

Browse files
committed
Use uint64 for parseSize
Functions like RAMInBytes offen used to parse memory set to cgroup, while Linux kernel take cgroup memory value as uint64, so we need the return value of RAMInBytes also be uint64, otherwise we are not able to set memory larger than MAX(int64) and smaller than MAX(uint64). This'll somewhat break compatibility since parseSize returns 0 when input is not parseable, but I doubt people actually depend on that because we already return error, so I don't think this'll be a problem. Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
1 parent 0dadbb0 commit b7f5cdf

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

size.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const (
2626
PiB = 1024 * TiB
2727
)
2828

29-
type unitMap map[string]int64
29+
type unitMap map[string]uint64
3030

3131
var (
3232
decimalMap = unitMap{"k": KB, "m": MB, "g": GB, "t": TB, "p": PB}
@@ -75,34 +75,35 @@ func BytesSize(size float64) string {
7575

7676
// FromHumanSize returns an integer from a human-readable specification of a
7777
// size using SI standard (eg. "44kB", "17MB").
78-
func FromHumanSize(size string) (int64, error) {
78+
func FromHumanSize(size string) (uint64, error) {
7979
return parseSize(size, decimalMap)
8080
}
8181

8282
// RAMInBytes parses a human-readable string representing an amount of RAM
8383
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
84-
// returns the number of bytes, or -1 if the string is unparseable.
84+
// returns the number of bytes, or 0 and corresponding error if the string
85+
// is unparseable.
8586
// Units are case-insensitive, and the 'b' suffix is optional.
86-
func RAMInBytes(size string) (int64, error) {
87+
func RAMInBytes(size string) (uint64, error) {
8788
return parseSize(size, binaryMap)
8889
}
8990

9091
// Parses the human-readable size string into the amount it represents.
91-
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
92+
func parseSize(sizeStr string, uMap unitMap) (uint64, error) {
9293
matches := sizeRegex.FindStringSubmatch(sizeStr)
9394
if len(matches) != 4 {
94-
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
95+
return 0, fmt.Errorf("invalid size: '%s'", sizeStr)
9596
}
9697

9798
size, err := strconv.ParseFloat(matches[1], 64)
9899
if err != nil {
99-
return -1, err
100+
return 0, err
100101
}
101102

102103
unitPrefix := strings.ToLower(matches[3])
103104
if mul, ok := uMap[unitPrefix]; ok {
104105
size *= float64(mul)
105106
}
106107

107-
return int64(size), nil
108+
return uint64(size), nil
108109
}

size_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestRAMInBytes(t *testing.T) {
125125

126126
assertSuccessEquals(t, 32, RAMInBytes, "32.3")
127127
tmp := 32.3 * MiB
128-
assertSuccessEquals(t, int64(tmp), RAMInBytes, "32.3 mb")
128+
assertSuccessEquals(t, uint64(tmp), RAMInBytes, "32.3 mb")
129129

130130
assertError(t, RAMInBytes, "")
131131
assertError(t, RAMInBytes, "hello")
@@ -142,15 +142,15 @@ func assertEquals(t *testing.T, expected, actual interface{}) {
142142
}
143143

144144
// func that maps to the parse function signatures as testing abstraction
145-
type parseFn func(string) (int64, error)
145+
type parseFn func(string) (uint64, error)
146146

147147
// Define 'String()' for pretty-print
148148
func (fn parseFn) String() string {
149149
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
150150
return fnName[strings.LastIndex(fnName, ".")+1:]
151151
}
152152

153-
func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
153+
func assertSuccessEquals(t *testing.T, expected uint64, fn parseFn, arg string) {
154154
res, err := fn(arg)
155155
if err != nil || res != expected {
156156
t.Errorf("%s(\"%s\") -> expected '%d' but got '%d' with error '%v'", fn, arg, expected, res, err)
@@ -159,7 +159,7 @@ func assertSuccessEquals(t *testing.T, expected int64, fn parseFn, arg string) {
159159

160160
func assertError(t *testing.T, fn parseFn, arg string) {
161161
res, err := fn(arg)
162-
if err == nil && res != -1 {
162+
if err == nil && res != 0 {
163163
t.Errorf("%s(\"%s\") -> expected error but got '%d'", fn, arg, res)
164164
}
165165
}

0 commit comments

Comments
 (0)