Skip to content

Commit 419a8ed

Browse files
Tanush576jedel1043
andauthored
feat(string): implement OOM protection and spec-alignment for padStart/padEnd (#5018)
Overview This PR improves the robustness of String.prototype.padStart and String.prototype.padEnd. While the basic functionality existed, the implementation lacked safeguards against massive memory allocations and didn't perfectly match the early-return execution order required by the ECMA-262 specification (Sections 22.1.3.14 & 22.1.3.15). Spec Compliance & Safety Improvements I have updated the logic to align with the following abstract operations: Step 5 (Early Return): If intMaxLength is less than or equal to the current string length, the function now returns the original string immediately before processing the filler. Step 9 (Empty Filler): If the filler string is empty, it returns the original string, preventing infinite loop logic or unnecessary allocation. Memory Safety (OOM Protection): Added a validation check against String::MAX_STRING_LENGTH. Previously: Attempting "a".padStart(Number.MAX_SAFE_INTEGER) could lead to a heap allocation failure/crash. Now: It correctly throws a RangeError, matching the behavior of high-performance engines like V8. Changes Refactored fn pad in core/engine/src/builtins/string/mod.rs to follow spec steps 1-13 sequentially. Integrated String::MAX_STRING_LENGTH check to ensure engine stability. Simplified undefined argument handling for the fillString parameter. Verification Verified with a custom test suite covering: Large maxLength values (Triggering RangeError). undefined and empty string fillers. maxLength values smaller than the target string. Note to Maintainers This is my second contribution as a GSoC 2026 applicant for the ECMAScript Conformance project. After my previous PR for String.prototype.repeat (#5017), I am continuing to audit the String built-ins to ensure they are both spec-compliant and memory-safe. --------- Co-authored-by: José Julián Espina <jedel@startmail.com> Co-authored-by: José Julián Espina <jedel0124@gmail.com>
1 parent 3f04847 commit 419a8ed

File tree

1 file changed

+11
-0
lines changed
  • core/engine/src/builtins/string

1 file changed

+11
-0
lines changed

core/engine/src/builtins/string/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,17 @@ impl String {
15551555

15561556
// 8. Let fillLen be intMaxLength - stringLength.
15571557
let fill_len = int_max_length - string_length;
1558+
1559+
// Check if the resulting string would exceed the maximum string length
1560+
if int_max_length > (Self::MAX_STRING_LENGTH as u64) {
1561+
return Err(JsNativeError::range()
1562+
.with_message(format!(
1563+
"cannot create a string longer than the maximum allowed length ({})",
1564+
Self::MAX_STRING_LENGTH
1565+
))
1566+
.into());
1567+
}
1568+
15581569
let filler_len = filler.len() as u64;
15591570

15601571
// 9. Let truncatedStringFiller be the String value consisting of repeated

0 commit comments

Comments
 (0)