mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(strutil): clamp substring lengths before integer conversion (#377)
This commit is contained in:
1 parent
c701a35586
commit
b40eea78c3
2 files changed
+5
-3
No files matched your search
+3
-3
@@ -316,10 +316,10 @@ func Substring(s string, offset int, length uint) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
end := offset + int(length)
|
if length > uint(size-offset) {
|
||||||
if end > size {
|
length = uint(size - offset)
|
||||||
end = size
|
|
||||||
}
|
}
|
||||||
|
end := offset + int(length)
|
||||||
|
|
||||||
return strings.ReplaceAll(string(rs[offset:end]), "\x00", "")
|
return strings.ReplaceAll(string(rs[offset:end]), "\x00", "")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -482,6 +482,8 @@ func TestSubstring(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
assert := internal.NewAssert(t, "TestSubstring")
|
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("bcd", Substring("abcde", 1, 3))
|
||||||
assert.Equal("bcde", Substring("abcde", 1, 5))
|
assert.Equal("bcde", Substring("abcde", 1, 5))
|
||||||
|
|||||||
Reference in new issue
Block a user