From eb518ac79060c9c0dc9b71e6528a39a214f4416c Mon Sep 17 00:00:00 2001 From: KENDAL Date: Tue, 11 Aug 2026 11:11:03 +0300 Subject: [PATCH 1/3] fix(utils): use Go 1.20+ unsafe.String in UnsafeCastToString and add upfront overflow check in CalculateMaxSize --- pkg/utils/bytes.go | 5 ++++- pkg/utils/calc.go | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/utils/bytes.go b/pkg/utils/bytes.go index cfcccdc..b7014f2 100644 --- a/pkg/utils/bytes.go +++ b/pkg/utils/bytes.go @@ -15,5 +15,8 @@ func Uint64ToBytes(i uint64) []byte { // to byteSlice will also modify the contents of the string, so it is the caller's responsibility // to ensure that the byte slice will not modified after the string is created. func UnsafeCastToString(byteSlice []byte) string { - return *(*string)(unsafe.Pointer(&byteSlice)) // #nosec G103 + if len(byteSlice) == 0 { + return "" + } + return unsafe.String(unsafe.SliceData(byteSlice), len(byteSlice)) } diff --git a/pkg/utils/calc.go b/pkg/utils/calc.go index 541f8c6..29fe856 100644 --- a/pkg/utils/calc.go +++ b/pkg/utils/calc.go @@ -12,8 +12,12 @@ func CalculateMaxSize(src int64) (int, error) { if src <= 0 { return 0, fmt.Errorf("src must be positive: %d", src) } + maxAllowed := int64(math.MaxInt) + if src > maxAllowed-maxAllowed/5 { + return 0, fmt.Errorf("src is too large, final value not fit: %d", src) + } finalVal := src + int64(float64(src)*0.2) - if finalVal < 0 || finalVal > math.MaxInt { + if finalVal < 0 || finalVal > maxAllowed { return 0, fmt.Errorf("src is too large, final value not fit: %d", src) } return int(finalVal), nil From d67d9e841bfff19c942ad9d4ff04ee0f4c9e74fb Mon Sep 17 00:00:00 2001 From: KENDAL Date: Wed, 12 Aug 2026 11:29:25 +0300 Subject: [PATCH 2/3] fix(utils): use integer-based overflow check in CalculateMaxSize and add focused unit tests --- pkg/utils/bytes_test.go | 26 ++++++++++++++++++++++++++ pkg/utils/calc.go | 10 ++++------ pkg/utils/calc_peers_test.go | 8 ++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 pkg/utils/bytes_test.go diff --git a/pkg/utils/bytes_test.go b/pkg/utils/bytes_test.go new file mode 100644 index 0000000..398dea4 --- /dev/null +++ b/pkg/utils/bytes_test.go @@ -0,0 +1,26 @@ +package utils_test + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/getoptimum/optimum-gateway/pkg/utils" +) + +func TestUnsafeCastToString(t *testing.T) { + t.Run("nil slice", func(t *testing.T) { + var b []byte = nil + require.Equal(t, "", utils.UnsafeCastToString(b)) + }) + + t.Run("empty non-nil slice", func(t *testing.T) { + b := []byte{} + require.Equal(t, "", utils.UnsafeCastToString(b)) + }) + + t.Run("ordinary byte content", func(t *testing.T) { + b := []byte("hello optimum gateway") + require.Equal(t, "hello optimum gateway", utils.UnsafeCastToString(b)) + }) +} diff --git a/pkg/utils/calc.go b/pkg/utils/calc.go index 29fe856..382463d 100644 --- a/pkg/utils/calc.go +++ b/pkg/utils/calc.go @@ -7,18 +7,16 @@ import ( // CalculateMaxSize calculates the maximum size of a message that can be sent over the network. // add some extra bytes for header and service info -// check possible overflow also +// check possible overflow also using integer arithmetic func CalculateMaxSize(src int64) (int, error) { if src <= 0 { return 0, fmt.Errorf("src must be positive: %d", src) } maxAllowed := int64(math.MaxInt) - if src > maxAllowed-maxAllowed/5 { - return 0, fmt.Errorf("src is too large, final value not fit: %d", src) - } - finalVal := src + int64(float64(src)*0.2) - if finalVal < 0 || finalVal > maxAllowed { + overhead := src / 5 + if src > maxAllowed-overhead { return 0, fmt.Errorf("src is too large, final value not fit: %d", src) } + finalVal := src + overhead return int(finalVal), nil } diff --git a/pkg/utils/calc_peers_test.go b/pkg/utils/calc_peers_test.go index 47979d6..132f120 100644 --- a/pkg/utils/calc_peers_test.go +++ b/pkg/utils/calc_peers_test.go @@ -29,6 +29,14 @@ func TestCalculateMaxSize(t *testing.T) { require.Equal(t, 120, size) }) + t.Run("accepts max allowed value without overflow", func(t *testing.T) { + maxAllowed := int64(math.MaxInt) + src := maxAllowed - maxAllowed/5 + size, err := utils.CalculateMaxSize(src) + require.NoError(t, err) + require.Greater(t, size, 0) + }) + t.Run("rejects overflow after overhead", func(t *testing.T) { _, err := utils.CalculateMaxSize(int64(math.MaxInt)) require.ErrorContains(t, err, "src is too large") From d079ca6160ee60a80f93a57337bc317d94ec79ae Mon Sep 17 00:00:00 2001 From: KENDAL Date: Wed, 12 Aug 2026 12:03:40 +0300 Subject: [PATCH 3/3] style(utils): simplify UnsafeCastToString and trim extra tests as requested by maintainer --- pkg/utils/bytes.go | 3 --- pkg/utils/bytes_test.go | 26 -------------------------- pkg/utils/calc_peers_test.go | 8 -------- 3 files changed, 37 deletions(-) delete mode 100644 pkg/utils/bytes_test.go diff --git a/pkg/utils/bytes.go b/pkg/utils/bytes.go index b7014f2..6fd1ca8 100644 --- a/pkg/utils/bytes.go +++ b/pkg/utils/bytes.go @@ -15,8 +15,5 @@ func Uint64ToBytes(i uint64) []byte { // to byteSlice will also modify the contents of the string, so it is the caller's responsibility // to ensure that the byte slice will not modified after the string is created. func UnsafeCastToString(byteSlice []byte) string { - if len(byteSlice) == 0 { - return "" - } return unsafe.String(unsafe.SliceData(byteSlice), len(byteSlice)) } diff --git a/pkg/utils/bytes_test.go b/pkg/utils/bytes_test.go deleted file mode 100644 index 398dea4..0000000 --- a/pkg/utils/bytes_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package utils_test - -import ( - "testing" - - "github.com/stretchr/testify/require" - - "github.com/getoptimum/optimum-gateway/pkg/utils" -) - -func TestUnsafeCastToString(t *testing.T) { - t.Run("nil slice", func(t *testing.T) { - var b []byte = nil - require.Equal(t, "", utils.UnsafeCastToString(b)) - }) - - t.Run("empty non-nil slice", func(t *testing.T) { - b := []byte{} - require.Equal(t, "", utils.UnsafeCastToString(b)) - }) - - t.Run("ordinary byte content", func(t *testing.T) { - b := []byte("hello optimum gateway") - require.Equal(t, "hello optimum gateway", utils.UnsafeCastToString(b)) - }) -} diff --git a/pkg/utils/calc_peers_test.go b/pkg/utils/calc_peers_test.go index 132f120..47979d6 100644 --- a/pkg/utils/calc_peers_test.go +++ b/pkg/utils/calc_peers_test.go @@ -29,14 +29,6 @@ func TestCalculateMaxSize(t *testing.T) { require.Equal(t, 120, size) }) - t.Run("accepts max allowed value without overflow", func(t *testing.T) { - maxAllowed := int64(math.MaxInt) - src := maxAllowed - maxAllowed/5 - size, err := utils.CalculateMaxSize(src) - require.NoError(t, err) - require.Greater(t, size, 0) - }) - t.Run("rejects overflow after overhead", func(t *testing.T) { _, err := utils.CalculateMaxSize(int64(math.MaxInt)) require.ErrorContains(t, err, "src is too large")