diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp index 756a3311e8c..fbf917f126d 100644 --- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -518,7 +518,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::substr) if (Value(int_start).is_negative_infinity()) int_start = 0; if (int_start < 0) - int_start = max(size + (i32)int_start, 0); + int_start = max(size + int_start, 0); auto length = vm.argument(1); diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substr.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substr.js index d452643295d..9c0273f7624 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substr.js +++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.substr.js @@ -4,6 +4,9 @@ test("basic functionality", () => { expect("".substr(1)).toBe(""); expect("".substr()).toBe(""); expect("".substr(-1)).toBe(""); + expect("a".substr(-1)).toBe("a"); + expect("a".substr(-2)).toBe("a"); + expect("a".substr(-3)).toBe("a"); expect("hello friends".substr()).toBe("hello friends"); expect("hello friends".substr(1)).toBe("ello friends"); expect("hello friends".substr(0, 5)).toBe("hello");