From b40eea78c3eb81eeb110a64fedd0d4f73a2774d9 Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:04:29 +0300 Subject: [PATCH] fix(strutil): clamp substring lengths before integer conversion (#377) --- strutil/string.go | 6 +++--- strutil/string_test.go | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/strutil/string.go b/strutil/string.go index ca1795d..f3bdd8a 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -316,10 +316,10 @@ func Substring(s string, offset int, length uint) string { return "" } - end := offset + int(length) - if end > size { - end = size + if length > uint(size-offset) { + length = uint(size - offset) } + end := offset + int(length) return strings.ReplaceAll(string(rs[offset:end]), "\x00", "") } diff --git a/strutil/string_test.go b/strutil/string_test.go index cca0889..b421063 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -482,6 +482,8 @@ func TestSubstring(t *testing.T) { t.Parallel() assert := internal.NewAssert(t, "TestSubstring") + assert.Equal("好世界", Substring("你好世界", 1, ^uint(0))) + assert.Equal("界", Substring("你好世界", -1, ^uint(0))) assert.Equal("bcd", Substring("abcde", 1, 3)) assert.Equal("bcde", Substring("abcde", 1, 5))