From cbee36f84cf140e88690db8edb7590543bd5b7e1 Mon Sep 17 00:00:00 2001 From: Papa Bakary Camara Date: Mon, 30 Sep 2024 13:26:18 +0200 Subject: [PATCH] Add utility function to convert uint64 to uint32 securely --- types/conversion.go | 29 +++++++++++++++++++++++++ types/conversion_test.go | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 types/conversion.go create mode 100644 types/conversion_test.go diff --git a/types/conversion.go b/types/conversion.go new file mode 100644 index 0000000..1e3d873 --- /dev/null +++ b/types/conversion.go @@ -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 +} diff --git a/types/conversion_test.go b/types/conversion_test.go new file mode 100644 index 0000000..ec9efce --- /dev/null +++ b/types/conversion_test.go @@ -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) + } + }) + } +}