Skip to content

fix(x11window): ref to an obj ouside its lifetime

Definition of a reference to an object whose life time has not started. Union definition is not begun lifetime of any member without explicit initialization. https://invent.kde.org/plasma/kwin/-/blob/master/src/x11window.cpp#L3950

    union {
        xcb_configure_notify_event_t event;
        char buffer[32];
    } u;
    static_assert(sizeof(u.event) < 32, "wouldn't need the union otherwise");
    memset(&u, 0, sizeof(u));
    xcb_configure_notify_event_t &c = u.event; // The lifetime of `event` does not begin here, but it is accessed. Reference to an object with a lifetime that has not begun
    u.event.response_type = XCB_CONFIGURE_NOTIFY; // The lifetime of `event` started here
    [...]
    // Reference use here
    xcb_send_event(kwinApp()->x11Connection(), true, c.event, XCB_EVENT_MASK_STRUCTURE_NOTIFY, reinterpret_cast<const char *>(&u));

[class.union#general-5.3]

5 When the left operand of an assignment operator involves a member access expression ([expr.ref]) that nominates a union member, it may begin the lifetime of that union member, as described below. For an expression E, define the set S(E) of subexpressions of E as follows: [...] (5.3) -- Otherwise, S(E) is empty.

and https://eel.is/c++draft/class.union#general-example-2

[dcl.ref#6.sentence-6]

The behavior of an evaluation of a reference ([expr.prim.id], [expr.ref]) that does not happen after ([intro.races]) the initialization of the reference is undefined.

only follow behavior is defined:

[basic.life#10.sentence-1]

After the lifetime of an object has ended and before the storage which the object occupied is reused or released, if a new object is created at the storage location which the original object occupied and the original object was transparently replaceable by the new object, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object.

Edited by Safocl Stollmannovic

Merge request reports

Loading