Skip to content

Commit

Permalink
Add utility function to convert uint64 to uint32 securely
Browse files Browse the repository at this point in the history
  • Loading branch information
epapbak committed Sep 30, 2024
1 parent 45b7622 commit cbee36f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
29 changes: 29 additions & 0 deletions types/conversion.go
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
}
46 changes: 46 additions & 0 deletions types/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 (
"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)
}
})
}
}

0 comments on commit cbee36f

Please sign in to comment.