From 3e5b67b5f6e8bc703f4497f37611e1058a5d3c78 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sat, 5 Jun 2021 16:29:41 -0400 Subject: lib: update `BytesInCommon` Ensure that the returned `common` array does not point to any of the []byte arrays in `bbytes`. --- lib/byte.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/byte.go b/lib/byte.go index 149be2a..a29a7b0 100644 --- a/lib/byte.go +++ b/lib/byte.go @@ -16,11 +16,13 @@ func ByteInBytes(b byte, bs []byte) bool { // Returns bytes that are common in the given array of array of bytes // `bbytes`. func BytesInCommon(bbytes [][]byte) []byte { - common := make([]byte, 0) + var common []byte switch l := len(bbytes); { case l == 1: - common = bbytes[0] + common = make([]byte, len(bbytes[0])) + copy(common, bbytes[0]) case l > 1: + common = make([]byte, 0) commonRest := BytesInCommon(bbytes[1:]) for _, b := range bbytes[0] { if ByteInBytes(b, commonRest) { -- cgit v1.2.3