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
21 changes: 8 additions & 13 deletions size.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,16 @@ func RAMInBytes(size string) (int64, error) {

// Parses the human-readable size string into the amount it represents.
func parseSize(sizeStr string, uMap unitMap) (int64, error) {
// TODO: rewrite to use strings.Cut if there's a space
// once Go < 1.18 is deprecated.
sep := strings.LastIndexAny(sizeStr, "01234567890. ")
if sep == -1 {
// There should be at least a digit.
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
}
var num, sfx string
if sizeStr[sep] != ' ' {
num, sfx, ok := strings.Cut(sizeStr, " ")
if !ok {
// No space between number and suffix. Find the suffix.
sep := strings.LastIndexAny(sizeStr, "01234567890.")
if sep == -1 {
// There should be at least a digit.
return -1, fmt.Errorf("invalid size: '%s'", sizeStr)
}
num = sizeStr[:sep+1]
sfx = sizeStr[sep+1:]
} else {
// Omit the space separator.
num = sizeStr[:sep]
sfx = sizeStr[sep+1:]
}

size, err := strconv.ParseFloat(num, 64)
Expand Down
2 changes: 1 addition & 1 deletion size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func BenchmarkParseSize(b *testing.B) {
}
}

func assertEquals(t *testing.T, expected, actual interface{}) {
func assertEquals(t *testing.T, expected, actual any) {
t.Helper()
if expected != actual {
t.Errorf("Expected '%v' but got '%v'", expected, actual)
Expand Down
22 changes: 10 additions & 12 deletions ulimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ var ulimitNameMapping = map[string]int{

// ParseUlimit parses and returns a Ulimit from the specified string.
func ParseUlimit(val string) (*Ulimit, error) {
parts := strings.SplitN(val, "=", 2)
if len(parts) != 2 {
name, val, ok := strings.Cut(val, "=")
if !ok {
return nil, fmt.Errorf("invalid ulimit argument: %s", val)
}

if _, exists := ulimitNameMapping[parts[0]]; !exists {
return nil, fmt.Errorf("invalid ulimit type: %s", parts[0])
if _, exists := ulimitNameMapping[name]; !exists {
return nil, fmt.Errorf("invalid ulimit type: %s", name)
}

var (
Expand All @@ -79,21 +79,19 @@ func ParseUlimit(val string) (*Ulimit, error) {
temp int64
err error
)
switch limitVals := strings.Split(parts[1], ":"); len(limitVals) {
case 2:
temp, err = strconv.ParseInt(limitVals[1], 10, 64)
switch softStr, hardStr, ok := strings.Cut(val, ":"); ok {
case true:
temp, err = strconv.ParseInt(hardStr, 10, 64)
if err != nil {
return nil, err
}
hard = &temp
fallthrough
case 1:
soft, err = strconv.ParseInt(limitVals[0], 10, 64)
case false:
soft, err = strconv.ParseInt(softStr, 10, 64)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("too many limit value arguments - %s, can only have up to two, `soft[:hard]`", parts[1])
}

if *hard != -1 {
Expand All @@ -105,7 +103,7 @@ func ParseUlimit(val string) (*Ulimit, error) {
}
}

return &Ulimit{Name: parts[0], Soft: soft, Hard: *hard}, nil
return &Ulimit{Name: name, Soft: soft, Hard: *hard}, nil
}

// GetRlimit returns the RLimit corresponding to Ulimit.
Expand Down
6 changes: 6 additions & 0 deletions ulimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func TestParseUlimitBadFormat(t *testing.T) {
if _, err := ParseUlimit("nofile=:1024"); err == nil {
t.Fatal("expected error on bad syntax")
}
if _, err := ParseUlimit("nofile=1024=512"); err == nil {
t.Fatal("expected error on bad syntax")
}
if _, err := ParseUlimit("nofile=1024:512=extra"); err == nil {
t.Fatal("expected error on bad syntax")
}
}

func TestParseUlimitHardLessThanSoft(t *testing.T) {
Expand Down