From dc292864aaa0f549614bb53b0eee7d019177cc52 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 22 Nov 2021 12:34:09 +0300 Subject: [PATCH] [#356] refs: Handle uint32 overflow in `SubnetID` text format Clarify the bit size limit in `SubnetID.UnmarshalText` method. Cover overflow case in unit test. Signed-off-by: Leonard Lyubich --- refs/types.go | 4 +++- refs/types_test.go | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/refs/types.go b/refs/types.go index 6e014b7..cd7ba5d 100644 --- a/refs/types.go +++ b/refs/types.go @@ -199,7 +199,9 @@ func (s *SubnetID) MarshalText() ([]byte, error) { } // UnmarshalText decodes SubnetID from the text according to NeoFS API V2 protocol: -// should be base-10 integer string format. +// should be base-10 integer string format with bitsize = 32. +// +// Returns strconv.ErrRange if integer overflows uint32. // // Must not be called on nil. // diff --git a/refs/types_test.go b/refs/types_test.go index 187a843..52c6b80 100644 --- a/refs/types_test.go +++ b/refs/types_test.go @@ -1,6 +1,7 @@ package refs_test import ( + "math" "strconv" "testing" @@ -47,4 +48,13 @@ func TestSubnetID_UnmarshalText(t *testing.T) { require.NoError(t, err) require.EqualValues(t, val, id.GetValue()) + + t.Run("uint32 overflow", func(t *testing.T) { + txt := strconv.FormatUint(math.MaxUint32+1, 10) + + var id refs.SubnetID + + err := id.UnmarshalText([]byte(txt)) + require.ErrorIs(t, err, strconv.ErrRange) + }) }