From 71e5c8dfa7395997fdc0c2464f539088d7274add Mon Sep 17 00:00:00 2001 From: ucwong Date: Sat, 10 Jun 2023 20:33:47 +0800 Subject: [PATCH] safe copy --- common/util.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/common/util.go b/common/util.go index b5dd598..20967ac 100644 --- a/common/util.go +++ b/common/util.go @@ -15,6 +15,16 @@ package common +//func SafeCopy(des, src []byte) []byte { +// return append(des[:0], src...) +//} + func SafeCopy(des, src []byte) []byte { - return append(des[:0], src...) + if len(des) < len(src) { + des = make([]byte, len(src)) + } else { + des = des[:len(src)] + } + copy(des, src) + return des }