mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-06-21 17:31:57 +08:00
fix(stringx): reject start > stop in Substr to prevent slice panic (#5616)
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
This commit is contained in:
@@ -132,7 +132,7 @@ func Substr(str string, start, stop int) (string, error) {
|
||||
return "", ErrInvalidStartPosition
|
||||
}
|
||||
|
||||
if stop < 0 || stop > length {
|
||||
if stop < 0 || stop > length || start > stop {
|
||||
return "", ErrInvalidStopPosition
|
||||
}
|
||||
|
||||
|
||||
@@ -356,6 +356,13 @@ func TestSubstr(t *testing.T) {
|
||||
err: ErrInvalidStopPosition,
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: "hello",
|
||||
start: 3,
|
||||
stop: 2,
|
||||
err: ErrInvalidStopPosition,
|
||||
expect: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, each := range cases {
|
||||
|
||||
Reference in New Issue
Block a user