From 24bb66e6062af0313d91054352930117f2163d31 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 6 Feb 2020 11:59:46 +0300 Subject: [PATCH] util: fix a bug with not copying slice of len=1 in ArrayReverse ArrayReverse copies it's argument only if it's len is > 1. It needs to be consistent in all cases. --- pkg/util/array.go | 4 ---- pkg/util/array_test.go | 11 ++++++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/util/array.go b/pkg/util/array.go index 83627fa5e..4fd4589ed 100644 --- a/pkg/util/array.go +++ b/pkg/util/array.go @@ -2,10 +2,6 @@ package util // ArrayReverse returns a reversed version of the given byte slice. func ArrayReverse(b []byte) []byte { - // Protect from big.Ints that have 1 len bytes. - if len(b) < 2 { - return b - } dest := make([]byte, len(b)) for i, j := 0, len(b)-1; i <= j; i, j = i+1, j-1 { dest[i], dest[j] = b[j], b[i] diff --git a/pkg/util/array_test.go b/pkg/util/array_test.go index d763665e4..4f44ac5df 100644 --- a/pkg/util/array_test.go +++ b/pkg/util/array_test.go @@ -30,7 +30,16 @@ var testCases = []struct { func TestArrayReverse(t *testing.T) { for _, tc := range testCases { - have := ArrayReverse(tc.arr) + arg := make([]byte, len(tc.arr)) + copy(arg, tc.arr) + + have := ArrayReverse(arg) require.Equal(t, tc.rev, have) + + // test that argument was copied + for i := range have { + have[i] = ^have[i] + } + require.Equal(t, tc.arr, arg) } }