Commit 419a8ed
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
1 file changed
+11
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1555 | 1555 | | |
1556 | 1556 | | |
1557 | 1557 | | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
1558 | 1569 | | |
1559 | 1570 | | |
1560 | 1571 | | |
| |||
0 commit comments