-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨
[safecast]
Introduced utilities to perform casting safely (#511)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description protect against [CWE-190](https://cwe.mitre.org/data/definitions/190.html) ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
- Loading branch information
1 parent
87ce671
commit 878f149
Showing
15 changed files
with
897 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
:sparkles: `[safecast]` Introduced utilities to perform casting safely and protect against [CWE-190](https://cwe.mitre.org/data/definitions/190.html) |
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
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
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,19 @@ | ||
# Safecast | ||
|
||
the purpose of this utilities is to perform safe number conversion in go similarly to [go-safecast](https://github.com/ccoVeille/go-safecast) from which they are inspired from. | ||
It should help tackling gosec [G115 rule](https://github.com/securego/gosec/pull/1149) | ||
|
||
G115: Potential overflow when converting between integer types. | ||
|
||
and [CWE-190](https://cwe.mitre.org/data/definitions/190.html) | ||
|
||
|
||
infinite loop | ||
access to wrong resource by id | ||
grant access to someone who exhausted their quota | ||
|
||
Contrary to `go-safecast` no error is returned when attempting casting and the MAX or MIN value of the type is returned instead if the value is beyond the allowed window. | ||
For instance, `toInt8(255)-> 127` and `toInt8(-255)-> -128` | ||
|
||
|
||
|
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,35 @@ | ||
package safecast | ||
|
||
func greaterThanUpperBoundary[C1 IConvertable, C2 IConvertable](value C1, upperBoundary C2) (greater bool) { | ||
if value <= 0 { | ||
return | ||
} | ||
|
||
switch f := any(value).(type) { | ||
case float64: | ||
greater = f >= float64(upperBoundary) | ||
case float32: | ||
greater = float64(f) >= float64(upperBoundary) | ||
default: | ||
// for all other integer types, it fits in an uint64 without overflow as we know value is positive. | ||
greater = uint64(value) > uint64(upperBoundary) | ||
} | ||
|
||
return | ||
} | ||
|
||
func lessThanLowerBoundary[T IConvertable, T2 IConvertable](value T, boundary T2) (lower bool) { | ||
if value >= 0 { | ||
return | ||
} | ||
|
||
switch f := any(value).(type) { | ||
case float64: | ||
lower = f <= float64(boundary) | ||
case float32: | ||
lower = float64(f) <= float64(boundary) | ||
default: | ||
lower = int64(value) < int64(boundary) | ||
} | ||
return | ||
} |
Oops, something went wrong.