Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ func ParseCPUs(value string) (int64, error) {
if !nano.IsInt() {
return 0, errors.New("value is too precise")
}
// Int64 keeps the low 64 bits of a value that does not fit.
if !nano.Num().IsInt64() {
return 0, errors.New("value is out of range")
}
return nano.Num().Int64(), nil
}

Expand Down
23 changes: 23 additions & 0 deletions opts/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package opts

import (
"fmt"
"math"
"strings"
"testing"

Expand Down Expand Up @@ -429,6 +430,28 @@ func TestParseCPUsReturnZeroOnInvalidValues(t *testing.T) {
assert.Equal(t, z1, resValue)
}

func TestParseCPUsRejectsValuesThatDoNotFitInInt64(t *testing.T) {
got, err := ParseCPUs("10000000000")
assert.Error(t, err, "value is out of range")
assert.Equal(t, int64(0), got)

got, err = ParseCPUs("9223372036.854775808")
assert.Error(t, err, "value is out of range")
assert.Equal(t, int64(0), got)

got, err = ParseCPUs("-9223372036.854775809")
assert.Error(t, err, "value is out of range")
assert.Equal(t, int64(0), got)

got, err = ParseCPUs("9223372036.854775807")
assert.NilError(t, err)
assert.Equal(t, int64(math.MaxInt64), got)

got, err = ParseCPUs("-9223372036.854775808")
assert.NilError(t, err)
assert.Equal(t, int64(math.MinInt64), got)
}

func TestUmaskOpt(t *testing.T) {
t.Run("type", func(t *testing.T) {
var opt UmaskOpt
Expand Down