forked from RedHatInsights/insights-operator-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility function to convert uint64 to uint32 securely
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 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,29 @@ | ||
// Copyright 2024 Red Hat, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// Uint64ToUint32 safely converts a uint64 into a uint32 without overflow | ||
func Uint64ToUint32(v uint64) (uint32, error) { | ||
// Check for overflow without casting | ||
if v > uint64(^uint32(0)) { // ^uint32(0) is the maximum uint32 value (4,294,967,295) | ||
return 0, errors.New("value exceeds uint32 range") | ||
} | ||
|
||
return uint32(v), nil // #nosec G103: safe after bounds check | ||
} |
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,33 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestUint64ToUint32(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input uint64 | ||
want uint32 | ||
wantErr bool | ||
}{ | ||
{"Zero", 0, 0, false}, | ||
{"Max uint32", 4294967295, 4294967295, false}, | ||
{"Overflow", 4294967296, 0, true}, | ||
{"Large overflow", 18446744073709551615, 0, true}, | ||
{"Mid-range value", 2147483648, 2147483648, false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := Uint64ToUint32(tt.input) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Uint64ToUint32() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("Uint64ToUint32() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |