Skip to content

JS_NewRuntime gives GCC-UBSAN error with bounds-checking #928

@andrjohns

Description

@andrjohns

I'm getting an odd error when compiling quickjs with gcc -fsanitize=bounds-strict (newly required as part of R's packaging checks) and calling JS_NewRuntime():

quickjs.c:2946:14: runtime error: index 4 out of bounds for type 'uint8_t [*]'

It can be reproduced with the debian:sid-slim docker image with default gcc (gcc 14), using dummy program:

#include "cutils.c"
#include "libbf.c"
#include "libregexp.c"
#include "libunicode.c"
#include "quickjs.c"

int main() {
  JSRuntime* rt = JS_NewRuntime();
  JS_FreeRuntime(rt);
  return 0;
}

Compiled with:

~/quickjs# gcc -fsanitize=bounds-strict -D_GNU_SOURCE -std=gnu11 -funsigned-char gcc_ubsan_test.c -lm -o gcc_ubsan_test
~/quickjs# ./gcc_ubsan_test 
quickjs.c:2946:14: runtime error: index 4 out of bounds for type 'uint8_t [*]'

The error is pointing to this function:

// XXX: `str` must be pure ASCII. No UTF-8 encoded strings
// XXX: `str` must not be the string representation of a small integer
static JSAtom __JS_NewAtomInit(JSRuntime *rt, const char *str, int len,
                               int atom_type)
{
    JSString *p;
    p = js_alloc_string_rt(rt, len, 0);
    if (!p)
        return JS_ATOM_NULL;
    memcpy(p->u.str8, str, len);
    p->u.str8[len] = '\0';
    return __JS_NewAtom(rt, p, atom_type);
}

And specifically the indexing:

p->u.str8[len] = '\0';

Any chance that there's a simple/obvious fix here? Thanks!

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