Skip to content

Do not hang when padding a string with an empty string - #1195

Merged
schungx merged 1 commit into
rhaiscript:mainfrom
b84330808:fix-pad-empty-string
Oct 24, 2026
Merged

schungx merged 1 commit into
rhaiscript:mainfrom
b84330808:fix-pad-empty-string

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

pad with an empty padding string never returns:

let x = "hello";
x.pad(10, "");   // never returns

pad_with_string appends padding until the character count reaches len, but an empty padding adds nothing, so the while loop never ends. The loop runs inside the native function, so max_operations and on_progress can't interrupt it: with set_max_operations(1_000), set_max_string_size(100) and an on_progress callback that terminates after 20 operations, the script above still hangs. For an engine that runs untrusted scripts, that's an easy way to lock up the host thread.

The fix returns early when padding is empty and leaves the string unchanged, the same as when len isn't larger than the string. The doc comment now says so.

Added test_string_pad to tests/string.rs. Without the fix it never finishes; with it, it passes. cargo test --tests passes, and the string tests also pass with no_object, no_index, unchecked, sync and internals,grain.

Found by calling every standard-library function with edge-case arguments; this was the only hang or panic it turned up.

I didn't add a CHANGELOG entry because 1.27.0 is already released and there's no section for the next version yet. Happy to add one wherever you prefer.

I used Claude Code to write this change.

`pad(string, len, "")` looped forever: the padding adds zero characters
per iteration, so the length never reaches `len`. The loop runs inside
the native function, so `max_operations` and `on_progress` cannot stop
it either. An empty padding string now leaves the string unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@schungx
schungx merged commit a6ef48d into rhaiscript:main Oct 24, 2026
56 checks passed
@schungx schungx added the bug label Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants