Skip to content

Fix infinite loop on allocation failure in unicode_script() - #1829

Merged
saghul merged 1 commit into
quickjs-ng:masterfrom
Kayyo321:fix/unicode-script-alloc-failure-loop
Oct 18, 2026
Merged

saghul merged 1 commit into
quickjs-ng:masterfrom
Kayyo321:fix/unicode-script-alloc-failure-loop

Conversation

@Kayyo321

@Kayyo321 Kayyo321 commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

unicode_script() ended its fail label with goto fail;, so any allocation failure looped back into the cleanup handler instead of returning. For \p{sc=...} (is_ext=0) it never returns; for \p{scx=...} (is_ext=1) each pass calls cr_free(cr1) and cr_free(cr2) again, and cr_free() does not clear points, so the same buffer is freed repeatedly. It is reached from RegExp compilation via parse_unicode_property(), so new RegExp('\p{scx=Latin}', 'u') compiled under a memory limit (~110k on this build) aborts on glibc double-free detection.

Return -1 from the fail label. cr_free() still runs once on each failing is_ext path; the -2 "not found" return and the success path (which frees cr1/cr2 once) are unchanged.

lre-test, api-test, and run-test262 -c tests.conf (0/120 errors) pass. test262 proper was not run here (submodule not checked out).

Fixes #1828

🤖 Generated with Claude Code

@bnoordhuis bnoordhuis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can drop the test, this bug is small enough it doesn't need one. LGTM apart from that.

The fail label in unicode_script() ended with `goto fail;`, so any
allocation failure sent the function back into its own cleanup handler
forever. On the Script= path (is_ext=0) that is a non-returning spin; on
the Script_Extensions= path (is_ext=1) each iteration calls cr_free() on
cr1 and cr2 again, and cr_free() does not clear points, so the same
buffer is freed repeatedly.

Reachable from regexp compilation: /\p{sc=Latin}/u and /\p{scx=Latin}/u
run unicode_script() via parse_unicode_property(), so a RegExp compiled
under a memory limit can hang or abort on a double free.

Return -1 from the fail label instead. cr_free() still runs once on
every failing is_ext path, the -2 unknown-script return is unaffected,
and the success path still frees cr1/cr2 exactly once.
@Kayyo321
Kayyo321 force-pushed the fix/unicode-script-alloc-failure-loop branch from b8bb367 to 92d804d Compare October 18, 2026 02:29
@Kayyo321

Copy link
Copy Markdown
Contributor Author

Thanks, dropped the test & removed that from the PR desc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unicode_script() loops on its own cleanup label on allocation failure

3 participants