Skip to content

Commit

Permalink
Check that last ≥ first in parseCPURange
Browse files Browse the repository at this point in the history
  • Loading branch information
tklauser committed May 2, 2024
1 parent dae747a commit 0cd81e3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions numcpus_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package numcpus

import (
"fmt"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -60,6 +61,9 @@ func parseCPURange(cpus string) (int, error) {
if err != nil {
return 0, err
}
if last < first {
return 0, fmt.Errorf("last CPU in range (%d) less than first (%d)", last, first)
}
n += int(last - first + 1)
}
return n, nil
Expand Down
2 changes: 2 additions & 0 deletions numcpus_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestParseCPURange(t *testing.T) {
{str: "", n: 0},
{str: "0", n: 1},
{str: "0-1", n: 2},
{str: "1-1", n: 1},
{str: "0-7", n: 8},
{str: "1-7", n: 7},
{str: "1-15", n: 15},
Expand All @@ -37,6 +38,7 @@ func TestParseCPURange(t *testing.T) {
{str: "0-", n: 0, wantErr: true},
{str: "0-,1", n: 0, wantErr: true},
{str: "0,-3,5", n: 0, wantErr: true},
{str: "0,5-3", n: 0, wantErr: true},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 0cd81e3

Please sign in to comment.