-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for overflows when converting ints to uints
As was done in MetalLB, we fix the G115 gosec errors by converting ints to uints more carefully. Signed-off-by: Ori Braunshtein <[email protected]>
- Loading branch information
Showing
7 changed files
with
70 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1309,9 +1309,9 @@ func TestMergeNeighbors(t *testing.T) { | |
Name: "[email protected]", | ||
ASN: "65040", | ||
Addr: "192.0.1.20", | ||
HoldTime: ptr.To(uint64(180)), | ||
KeepaliveTime: ptr.To(uint64(60)), | ||
ConnectTime: ptr.To(uint64(60)), | ||
HoldTime: ptr.To(int64(180)), | ||
KeepaliveTime: ptr.To(int64(60)), | ||
ConnectTime: ptr.To(int64(60)), | ||
}, | ||
}, | ||
expected: []*frr.NeighborConfig{ | ||
|
@@ -1341,9 +1341,9 @@ func TestMergeNeighbors(t *testing.T) { | |
Name: "[email protected]", | ||
ASN: "65040", | ||
Addr: "192.0.1.20", | ||
HoldTime: ptr.To(uint64(180)), | ||
KeepaliveTime: ptr.To(uint64(60)), | ||
ConnectTime: ptr.To(uint64(60)), | ||
HoldTime: ptr.To(int64(180)), | ||
KeepaliveTime: ptr.To(int64(60)), | ||
ConnectTime: ptr.To(int64(60)), | ||
}, | ||
}, | ||
toMerge: []*frr.NeighborConfig{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier:Apache-2.0 | ||
|
||
//go:build !arm | ||
// +build !arm | ||
|
||
package safeconvert | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
func IntToUInt32(toConvert int) (uint32, error) { | ||
if toConvert < 0 { | ||
return 0, fmt.Errorf("trying to convert negative value to uint32: %d", toConvert) | ||
} | ||
if toConvert > math.MaxUint32 { | ||
return 0, fmt.Errorf("trying to convert value to uint32: %d, would overflow", toConvert) | ||
} | ||
return uint32(toConvert), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build arm | ||
// +build arm | ||
|
||
// SPDX-License-Identifier:Apache-2.0 | ||
|
||
package safeconvert | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func IntToUInt32(toConvert int) (uint32, error) { | ||
if toConvert < 0 { | ||
return 0, fmt.Errorf("trying to convert negative value to uint32: %d", toConvert) | ||
} | ||
// No need to check for upper limit on arm as maxInt is lower than maxuint32 | ||
return uint32(toConvert), nil | ||
} |