Skip to content

Commit

Permalink
check that cumulus port names cannot map to negative line numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
iljarotar committed Sep 17, 2024
1 parent 11ed256 commit 8abf726
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/metal-api/internal/metal/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,18 @@ func cumulusPortNameToLine(port string) (int, error) {
if err != nil {
return 0, fmt.Errorf("unable to convert port name to line number: %w", err)
}
if count <= 0 {
return 0, fmt.Errorf("invalid port name would map to negative number")
}
line = (count - 1) * 4
} else {
count, err := strconv.Atoi(countString)
if err != nil {
return 0, fmt.Errorf("unable to convert port name to line number: %w", err)
}
if count <= 0 {
return 0, fmt.Errorf("invalid port name would map to negative number")
}

index, err := strconv.Atoi(indexString)
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion cmd/metal-api/internal/metal/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,22 @@ func Test_cumulusPortNameToLine(t *testing.T) {
},
{
name: "cannot convert second number",
port: "swp0s_0",
port: "swp1s_0",
want: 0,
wantErr: fmt.Errorf("unable to convert port name to line number: %w", parseIntError2),
},
{
name: "cannot convert swp0 because that would result in a negative line number",
port: "swp0",
want: 0,
wantErr: fmt.Errorf("invalid port name would map to negative number"),
},
{
name: "cannot convert swp0s1 because that would result in a negative line number",
port: "swp0s1",
want: 0,
wantErr: fmt.Errorf("invalid port name would map to negative number"),
},
{
name: "convert line without breakout",
port: "swp4",
Expand Down

0 comments on commit 8abf726

Please sign in to comment.