Skip to content

Commit

Permalink
fix(listener): prevent callbacks from using invalid pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Apr 10, 2024
1 parent 055757a commit 5dcc6c8
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 51 deletions.
12 changes: 6 additions & 6 deletions include/rohrkabel/utils/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
namespace pipewire
{
template <typename T, ereignis::ereignis_event... Events>
class listener : public spa::hook
struct listener : public spa::hook
{
using events = ereignis::manager<Events...>;

protected:
events m_events;
std::unique_ptr<events> m_events;

public:
~listener() = default;

public:
listener() = default;
listener(listener &&other) noexcept : spa::hook(std::move(other)) {}
listener() : m_events(std::make_unique<events>()) {}
listener(listener &&other) noexcept : spa::hook(std::move(other)), m_events(std::move(other.m_events)) {}

public:
void clear(T event)
Expand All @@ -36,13 +36,13 @@ namespace pipewire
template <T Event>
std::uint64_t on(events::template type_t<Event> &&callback)
{
return m_events.template at<Event>().add(std::move(callback));
return m_events->template at<Event>().add(std::move(callback));
}

template <T Event>
void once(events::template type_t<Event> &&callback)
{
m_events.template at<Event>().once(std::move(callback));
m_events->template at<Event>().once(std::move(callback));
}

public:
Expand Down
10 changes: 5 additions & 5 deletions src/events/client.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ namespace pipewire
m_impl->events.version = PW_VERSION_CLIENT_EVENTS;

m_impl->events.info = [](void *data, const pw_client_info *info) {
auto &thiz = *reinterpret_cast<client_listener *>(data);
thiz.m_events.at<client_event::info>().fire(client_info::from(info));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<client_event::info>().fire(client_info::from(info));
};

m_impl->events.permissions = [](void *data, std::uint32_t index, std::uint32_t count,
const pw_permission *permission) {
auto &thiz = *reinterpret_cast<client_listener *>(data);
thiz.m_events.at<client_event::permission>().fire(index, count, permission);
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<client_event::permission>().fire(index, count, permission);
};

pw_client_add_listener(client, listener::get(), &m_impl->events, this);
pw_client_add_listener(client, listener::get(), &m_impl->events, m_events.get());
}

client_listener::client_listener(client_listener &&client_listener) noexcept
Expand Down
14 changes: 7 additions & 7 deletions src/events/core.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ namespace pipewire
m_impl->events.version = PW_VERSION_CORE_EVENTS;

m_impl->events.info = [](void *data, const pw_core_info *info) {
auto &thiz = *reinterpret_cast<core_listener *>(data);
thiz.m_events.at<core_event::info>().fire(core_info::from(info));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<core_event::info>().fire(core_info::from(info));
};

m_impl->events.done = [](void *data, std::uint32_t id, int seq) {
auto &thiz = *reinterpret_cast<core_listener *>(data);
thiz.m_events.at<core_event::done>().fire(id, seq);
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<core_event::done>().fire(id, seq);
};

m_impl->events.error = [](void *data, std::uint32_t id, int seq, int res, const char *message) {
auto &thiz = *reinterpret_cast<core_listener *>(data);
thiz.m_events.at<core_event::error>().fire(id, error{seq, res, message});
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<core_event::error>().fire(id, error{seq, res, message});
};

pw_core_add_listener(core, listener::get(), &m_impl->events, this);
pw_core_add_listener(core, listener::get(), &m_impl->events, m_events.get());
}

core_listener::core_listener(core_listener &&core_listener) noexcept
Expand Down
10 changes: 5 additions & 5 deletions src/events/device.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ namespace pipewire
m_impl->events.version = PW_VERSION_DEVICE_EVENTS;

m_impl->events.info = [](void *data, const pw_device_info *info) {
auto &thiz = *reinterpret_cast<device_listener *>(data);
thiz.m_events.at<device_event::info>().fire(device_info::from(info));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<device_event::info>().fire(device_info::from(info));
};

m_impl->events.param = [](void *data, int seq, uint32_t id, uint32_t index, uint32_t next,
const struct spa_pod *param) {
auto &thiz = *reinterpret_cast<device_listener *>(data);
thiz.m_events.at<device_event::param>().fire(seq, id, index, next, spa::pod::copy(param));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<device_event::param>().fire(seq, id, index, next, spa::pod::copy(param));
};

pw_device_add_listener(device, listener::get(), &m_impl->events, this);
pw_device_add_listener(device, listener::get(), &m_impl->events, m_events.get());
}

device_listener::device_listener(device_listener &&device_listener) noexcept
Expand Down
6 changes: 3 additions & 3 deletions src/events/link.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ namespace pipewire
m_impl->events.version = PW_VERSION_LINK_EVENTS;

m_impl->events.info = [](void *data, const pw_link_info *info) {
auto &thiz = *reinterpret_cast<link_listener *>(data);
thiz.m_events.at<link_event::info>().fire(link_info::from(info));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<link_event::info>().fire(link_info::from(info));
};

pw_link_add_listener(link, listener::get(), &m_impl->events, this);
pw_link_add_listener(link, listener::get(), &m_impl->events, m_events.get());
}

link_listener::link_listener(link_listener &&link_listener) noexcept
Expand Down
6 changes: 3 additions & 3 deletions src/events/metadata.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ namespace pipewire

m_impl->events.property = [](void *data, std::uint32_t subject, const char *key, const char *type,
const char *value) {
auto &thiz = *reinterpret_cast<metadata_listener *>(data);
auto &events = *reinterpret_cast<listener::events *>(data);
auto property = metadata_property{type, value, subject};

return thiz.m_events.at<metadata_event::property>().during(0, key, std::move(property)).value_or(0);
return events.at<metadata_event::property>().during(0, key, std::move(property)).value_or(0);
};

pw_metadata_add_listener(metadata, listener::get(), &m_impl->events, this);
pw_metadata_add_listener(metadata, listener::get(), &m_impl->events, m_events.get());
}

metadata_listener::metadata_listener(metadata_listener &&metadata_listener) noexcept
Expand Down
10 changes: 5 additions & 5 deletions src/events/node.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ namespace pipewire
m_impl->events.version = PW_VERSION_NODE_EVENTS;

m_impl->events.info = [](void *data, const pw_node_info *info) {
auto &thiz = *reinterpret_cast<node_listener *>(data);
thiz.m_events.at<node_event::info>().fire(node_info::from(info));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<node_event::info>().fire(node_info::from(info));
};

m_impl->events.param = [](void *data, int seq, uint32_t id, uint32_t index, uint32_t next,
const struct spa_pod *param) {
auto &thiz = *reinterpret_cast<node_listener *>(data);
thiz.m_events.at<node_event::param>().fire(seq, id, index, next, spa::pod::copy(param));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<node_event::param>().fire(seq, id, index, next, spa::pod::copy(param));
};

pw_node_add_listener(node, listener::get(), &m_impl->events, this);
pw_node_add_listener(node, listener::get(), &m_impl->events, m_events.get());
}

node_listener::node_listener(node_listener &&node_listener) noexcept
Expand Down
10 changes: 5 additions & 5 deletions src/events/port.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ namespace pipewire
m_impl->events.version = PW_VERSION_PORT_EVENTS;

m_impl->events.info = [](void *data, const pw_port_info *info) {
auto &thiz = *reinterpret_cast<port_listener *>(data);
thiz.m_events.at<port_event::info>().fire(port_info::from(info));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<port_event::info>().fire(port_info::from(info));
};

m_impl->events.param = [](void *data, int seq, uint32_t id, uint32_t index, uint32_t next,
const struct spa_pod *param) {
auto &thiz = *reinterpret_cast<port_listener *>(data);
thiz.m_events.at<port_event::param>().fire(seq, id, index, next, spa::pod::copy(param));
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<port_event::param>().fire(seq, id, index, next, spa::pod::copy(param));
};

pw_port_add_listener(port, listener::get(), &m_impl->events, this);
pw_port_add_listener(port, listener::get(), &m_impl->events, m_events.get());
}

port_listener::port_listener(port_listener &&port_listener) noexcept
Expand Down
14 changes: 7 additions & 7 deletions src/events/proxy.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ namespace pipewire
m_impl->events.version = PW_VERSION_PROXY_EVENTS;

m_impl->events.error = [](void *data, int seq, int res, const char *message) {
auto &thiz = *reinterpret_cast<proxy_listener *>(data);
thiz.m_events.at<proxy_event::error>().fire(seq, res, message);
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<proxy_event::error>().fire(seq, res, message);
};

m_impl->events.bound = [](void *data, std::uint32_t global) {
auto &thiz = *reinterpret_cast<proxy_listener *>(data);
thiz.m_events.at<proxy_event::bound>().fire(global);
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<proxy_event::bound>().fire(global);
};

m_impl->events.bound_props = [](void *data, std::uint32_t global, const spa_dict *props) {
auto &thiz = *reinterpret_cast<proxy_listener *>(data);
thiz.m_events.at<proxy_event::bound_props>().fire(global, props);
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<proxy_event::bound_props>().fire(global, props);
};

pw_proxy_add_listener(proxy, listener::get(), &m_impl->events, this);
pw_proxy_add_listener(proxy, listener::get(), &m_impl->events, m_events.get());
}

proxy_listener::proxy_listener(proxy_listener &&proxy_listener) noexcept
Expand Down
10 changes: 5 additions & 5 deletions src/events/registry.events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace pipewire

m_impl->events.global = [](void *data, std::uint32_t id, std::uint32_t permissions, const char *type,
std::uint32_t version, const spa_dict *props) {
auto &thiz = *reinterpret_cast<registry_listener *>(data);
thiz.m_events.at<registry_event::global>().fire(global{id, version, permissions, props, type});
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<registry_event::global>().fire(global{id, version, permissions, props, type});
};

m_impl->events.global_remove = [](void *data, std::uint32_t id) {
auto &thiz = *reinterpret_cast<registry_listener *>(data);
thiz.m_events.at<registry_event::global_removed>().fire(id);
auto &events = *reinterpret_cast<listener::events *>(data);
events.at<registry_event::global_removed>().fire(id);
};

// NOLINTNEXTLINE(*-optional-access)
pw_registry_add_listener(registry, listener::get(), &m_impl->events, this);
pw_registry_add_listener(registry, listener::get(), &m_impl->events, m_events.get());
}

registry_listener::registry_listener(registry_listener &&registry_listener) noexcept
Expand Down

0 comments on commit 5dcc6c8

Please sign in to comment.