From d79801ffe08a9302748a8f608213b9aab4fc0a72 Mon Sep 17 00:00:00 2001 From: liu Date: Tue, 28 May 2024 20:55:40 +0800 Subject: [PATCH] fix: check empty when validating utf8 (#642) --- utf8/utf8.go | 12 +++++++++++- utf8/utf8_test.go | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/utf8/utf8.go b/utf8/utf8.go index 52c35fb28..573fe2f5b 100644 --- a/utf8/utf8.go +++ b/utf8/utf8.go @@ -17,6 +17,8 @@ package utf8 import ( + `runtime` + `github.com/bytedance/sonic/internal/rt` `github.com/bytedance/sonic/internal/native/types` `github.com/bytedance/sonic/internal/native` @@ -62,10 +64,18 @@ func CorrectWith(dst []byte, src []byte, repl string) []byte { // Validate is a simd-accelereated drop-in replacement for the standard library's utf8.Valid. func Validate(src []byte) bool { + if src == nil { + return true + } return ValidateString(rt.Mem2Str(src)) } // ValidateString as Validate, but for string. func ValidateString(src string) bool { - return native.ValidateUTF8Fast(&src) == 0 + if src == "" { + return true + } + ret := native.ValidateUTF8Fast(&src) == 0 + runtime.KeepAlive(src) + return ret } diff --git a/utf8/utf8_test.go b/utf8/utf8_test.go index 29536283a..d4b55dfe2 100644 --- a/utf8/utf8_test.go +++ b/utf8/utf8_test.go @@ -100,6 +100,9 @@ func TestValidate_Random(t *testing.T) { assert.Equal(t, utf8.Valid(data), Validate(data), string(data)) } + compare(t, []byte{}) + compare(t, nil) + // random testing nums := 1000 maxLen := 1000