Skip to content

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

Closed

Description

@Kayyo321

libunicode.c, unicode_script() (master 9b06171, around lines 1283-1289). The fail label ends with goto fail;:

 fail:
    if (is_ext) {
        cr_free(cr1);
        cr_free(cr2);
    }
    goto fail;

Any allocation failure inside the function reaches this label and never leaves it.

  • Script= / sc= (is_ext=0): cr1 aliases the caller's CharRange, so the handler frees nothing and goto fail is a bare non-returning loop. Under -O2 that side-effect-free infinite loop is UB and tends to fall through rather than spin.
  • Script_Extensions= / scx= (is_ext=1): each pass calls cr_free(cr1) and cr_free(cr2) again. cr_free() (libunicode.c ~line 394) does not clear points, so the same buffer is freed repeatedly.

Reachable from RegExp compilation: parse_unicode_property() (libregexp.c ~923-930) calls unicode_script() for \p{sc=...} and \p{scx=...} under the u/v flag, so a RegExp compiled under a memory limit that makes a CharRange allocation fail hits it.

Reproduce (double free, is_ext=1):

gdb -batch -ex run -ex bt --args build/qjs --memory-limit 110k \
    -e "new RegExp('\p{scx=Latin}', 'u')"

SIGABRT from glibc double-free detection, stack js_free_rt <- lre_realloc <- unicode_script <- get_class_atom <- re_parse_term <- lre_compile <- js_compile_regexp. 111k reproduces too; control patterns without a script property do not.

Verified: the abort above (gdb, those limits); and, injecting an allocation failure at each allocation point through the lre-test.c harness, unicode_script() fails to return on the is_ext=0 patterns and frees cr1/cr2 repeatedly on the is_ext=1 patterns, on master.

Not verified: an end-to-end Script=/sc= hang at a specific limit (a limit sweep did not find one); behaviour on released tags; any effect beyond the crash/abort.

Fix: return -1 from the fail label instead of looping back into it. cr_free() then runs once on each failing is_ext path; the -2 "not found" return and the success path are unchanged. Proposed on branch fix/unicode-script-alloc-failure-loop with an lre-test regression case that compiles the script-property patterns with an injected allocation failure; a PR linking here follows.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions