fix: use shared_ptr in XCBUtils::getWindowFrameExtents

To void use-after-free issues.

Signed-off-by: black-desk <me@black-desk.cn>
This commit is contained in:
tsic404 2023-05-11 18:12:37 +08:00 committed by 爱折腾的小竹同学
parent 611bc0e092
commit ad07a96240

View File

@ -206,23 +206,25 @@ WindowFrameExtents XCBUtils::getWindowFrameExtents(XWindow xid)
{ {
xcb_atom_t perp = getAtom("_NET_FRAME_EXTENTS"); xcb_atom_t perp = getAtom("_NET_FRAME_EXTENTS");
xcb_get_property_cookie_t cookie = xcb_get_property(m_connect, false, xid, perp, XCB_ATOM_CARDINAL, 0, 4); xcb_get_property_cookie_t cookie = xcb_get_property(m_connect, false, xid, perp, XCB_ATOM_CARDINAL, 0, 4);
xcb_get_property_reply_t *reply = xcb_get_property_reply(m_connect, cookie, nullptr); std::shared_ptr<xcb_get_property_reply_t> reply(
xcb_get_property_reply(m_connect, cookie, nullptr),
[](xcb_get_property_reply_t* ptr) { free(ptr); }
);
if (!reply || reply->format == 0) { if (!reply || reply->format == 0) {
if (reply)
free(reply);
perp = getAtom("_GTK_FRAME_EXTENTS"); perp = getAtom("_GTK_FRAME_EXTENTS");
cookie = xcb_get_property(m_connect, false, xid, perp, XCB_ATOM_CARDINAL, 0, 4); cookie = xcb_get_property(m_connect, false, xid, perp, XCB_ATOM_CARDINAL, 0, 4);
reply = xcb_get_property_reply(m_connect, cookie, nullptr); reply.reset(
xcb_get_property_reply(m_connect, cookie, nullptr),
[](xcb_get_property_reply_t* ptr) { free(ptr); }
);
if (!reply) if (!reply)
return WindowFrameExtents(); return WindowFrameExtents();
} }
if (reply->format != 32 || reply->value_len != 4) { if (reply->format != 32 || reply->value_len != 4) {
free(reply);
return WindowFrameExtents(); return WindowFrameExtents();
} }
uint32_t *data = static_cast<uint32_t *>(xcb_get_property_value(reply)); uint32_t *data = static_cast<uint32_t *>(xcb_get_property_value(reply.get()));
free(reply);
if (!data) if (!data)
return WindowFrameExtents(); return WindowFrameExtents();