Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion quickjs-libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4254,7 +4254,10 @@ static JSValue js_worker_postMessage(JSContext *ctx, JSValueConst this_val,
msg->data = malloc(data_len);
if (!msg->data)
goto fail;
memcpy(msg->data, data, data_len);
/* memcpy with NULL src/dst is UB even when n == 0; the writer side
can produce zero-length payloads (e.g. JSON.stringify(undefined)). */
Comment on lines +4257 to +4258
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.

This comment doesn't make sense because JS_WriteObject2 never returns zero-length output; you even mention that in the PR description.

I also don't think this is the right place or the best way to handle it. If data_len == 0, you get divergent behavior because malloc(0) either returns nullptr or a unique ptr. In other words, this code path may not ever be reached, depending on the libc used.

If you really want to handle this hypothetical scenario, an assert right after the JS_WriteObject2 call should be sufficient.

if (data_len > 0)
memcpy(msg->data, data, data_len);
msg->data_len = data_len;

if (sab_tab.len > 0) {
Expand Down
Loading