Skip to content

fix(libc): avoid undefined memcpy in js_worker_postMessage with len==0#1483

Open
andreasrosdal wants to merge 1 commit into
quickjs-ng:masterfrom
nordstjernen-web:claude/sec-worker-null-memcpy
Open

fix(libc): avoid undefined memcpy in js_worker_postMessage with len==0#1483
andreasrosdal wants to merge 1 commit into
quickjs-ng:masterfrom
nordstjernen-web:claude/sec-worker-null-memcpy

Conversation

@andreasrosdal
Copy link
Copy Markdown
Contributor

memcpy(dst, src, 0) is UB per C11 7.21.1 when either pointer is NULL, even if no bytes are copied — UBSan with -fsanitize=nonnull-attribute flags it and a strict optimiser may exploit the implied non-NULL attribute to elide subsequent NULL checks.

In practice JS_WriteObject2 always emits a non-empty buffer, but make the call site safe by hand so the worker pipe is robust to any future serializer change.

No test: triggering the UB requires data_len == 0 and malloc(0) returning non-NULL, which doesn't happen on the current writer. UBSan in CI would catch a regression.

memcpy(dst, src, 0) is UB per C11 7.21.1 when either pointer is NULL,
even if no bytes are copied — UBSan with -fsanitize=nonnull-attribute
flags it and a strict optimiser may exploit the implied non-NULL
attribute to elide subsequent NULL checks.

In practice JS_WriteObject2 always emits a non-empty buffer, but make
the call site safe by hand so the worker pipe is robust to any future
serializer change.

No test: triggering the UB requires data_len == 0 and malloc(0) returning
non-NULL, which doesn't happen on the current writer. UBSan in CI would
catch a regression.
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.

2 participants