Skip to content

Commit c1ffe7f

Browse files
Merge pull request #791 from lightpanda-io/zig_event_target_fix
Fix crash when event.currentTarget is used with EventTargetTBase
2 parents 8033e41 + ce98c33 commit c1ffe7f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/browser/html/window.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,19 @@ test "Browser.HTML.Window" {
387387
.{ "window.setTimeout(() => {longCall = true}, 5001);", null },
388388
.{ "longCall;", "false" },
389389
}, .{});
390+
391+
// window event target
392+
try runner.testCases(&.{
393+
.{
394+
\\ let called = false;
395+
\\ window.addEventListener("ready", (e) => {
396+
\\ called = (e.currentTarget == window);
397+
\\ }, {capture: false, once: false});
398+
\\ const evt = new Event("ready", { bubbles: true, cancelable: false });
399+
\\ window.dispatchEvent(evt);
400+
\\ called;
401+
,
402+
"true",
403+
},
404+
}, .{});
390405
}

src/browser/netsurf.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,17 @@ pub const EventTargetTBase = extern struct {
775775
.add_event_listener = add_event_listener,
776776
.iter_event_listener = iter_event_listener,
777777
},
778+
779+
// When we dispatch the event, we need to provide a target. In reality, the
780+
// target is the container of this EventTargetTBase. But we can't pass that
781+
// to _dom_event_target_dispatch, because it expects a dom_event_target.
782+
// If you try to pass an non-event_target, you'll get weird behavior. For
783+
// example, libdom might dom_node_ref that memory. Say we passed a *Window
784+
// as the target, what happens if libdom calls dom_node_ref(window)? If
785+
// you're lucky, you'll crash. If you're unlucky, you'll increment a random
786+
// part of the window structure.
787+
refcnt: u32 = 0,
788+
778789
eti: c.dom_event_target_internal = c.dom_event_target_internal{ .listeners = null },
779790

780791
pub fn add_event_listener(et: [*c]c.dom_event_target, t: [*c]c.dom_string, l: ?*c.struct_dom_event_listener, capture: bool) callconv(.C) c.dom_exception {

0 commit comments

Comments
 (0)