From 536a49f282304207b704828bb07efcb3745a99ef Mon Sep 17 00:00:00 2001 From: rept1d Date: Tue, 19 Dec 2023 10:46:18 +0300 Subject: [PATCH] fix: avoid memory leaks in event_map --- src/dpp/discordevents.cpp | 142 ++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 68 deletions(-) diff --git a/src/dpp/discordevents.cpp b/src/dpp/discordevents.cpp index 18396ed225..013f2344ee 100644 --- a/src/dpp/discordevents.cpp +++ b/src/dpp/discordevents.cpp @@ -320,85 +320,91 @@ void set_ts_not_null(const json* j, const char* keyname, time_t &v) } } -const std::map eventmap = { - { "__LOG__", new dpp::events::logger() }, - { "GUILD_CREATE", new dpp::events::guild_create() }, - { "GUILD_UPDATE", new dpp::events::guild_update() }, - { "GUILD_DELETE", new dpp::events::guild_delete() }, - { "GUILD_MEMBER_UPDATE", new dpp::events::guild_member_update() }, - { "RESUMED", new dpp::events::resumed() }, - { "READY", new dpp::events::ready() }, - { "CHANNEL_CREATE", new dpp::events::channel_create() }, - { "CHANNEL_UPDATE", new dpp::events::channel_update() }, - { "CHANNEL_DELETE", new dpp::events::channel_delete() }, - { "PRESENCE_UPDATE", new dpp::events::presence_update() }, - { "TYPING_START", new dpp::events::typing_start() }, - { "MESSAGE_CREATE", new dpp::events::message_create() }, - { "MESSAGE_UPDATE", new dpp::events::message_update() }, - { "MESSAGE_DELETE", new dpp::events::message_delete() }, - { "MESSAGE_DELETE_BULK", new dpp::events::message_delete_bulk() }, - { "MESSAGE_REACTION_ADD", new dpp::events::message_reaction_add() }, - { "MESSAGE_REACTION_REMOVE", new dpp::events::message_reaction_remove() }, - { "MESSAGE_REACTION_REMOVE_ALL", new dpp::events::message_reaction_remove_all() }, - { "MESSAGE_REACTION_REMOVE_EMOJI", new dpp::events::message_reaction_remove_emoji() }, - { "CHANNEL_PINS_UPDATE", new dpp::events::channel_pins_update() }, - { "GUILD_BAN_ADD", new dpp::events::guild_ban_add() }, - { "GUILD_BAN_REMOVE", new dpp::events::guild_ban_remove() }, - { "GUILD_EMOJIS_UPDATE", new dpp::events::guild_emojis_update() }, - { "GUILD_INTEGRATIONS_UPDATE", new dpp::events::guild_integrations_update() }, - { "INTEGRATION_CREATE", new dpp::events::integration_create() }, - { "INTEGRATION_UPDATE", new dpp::events::integration_update() }, - { "INTEGRATION_DELETE", new dpp::events::integration_delete() }, - { "GUILD_MEMBER_ADD", new dpp::events::guild_member_add() }, - { "GUILD_MEMBER_REMOVE", new dpp::events::guild_member_remove() }, - { "GUILD_MEMBERS_CHUNK", new dpp::events::guild_members_chunk() }, - { "GUILD_ROLE_CREATE", new dpp::events::guild_role_create() }, - { "GUILD_ROLE_UPDATE", new dpp::events::guild_role_update() }, - { "GUILD_ROLE_DELETE", new dpp::events::guild_role_delete() }, - { "VOICE_STATE_UPDATE", new dpp::events::voice_state_update() }, - { "VOICE_SERVER_UPDATE", new dpp::events::voice_server_update() }, - { "WEBHOOKS_UPDATE", new dpp::events::webhooks_update() }, - { "INVITE_CREATE", new dpp::events::invite_create() }, - { "INVITE_DELETE", new dpp::events::invite_delete() }, - { "INTERACTION_CREATE", new dpp::events::interaction_create() }, - { "USER_UPDATE", new dpp::events::user_update() }, - { "GUILD_JOIN_REQUEST_DELETE", new dpp::events::guild_join_request_delete() }, +template +static dpp::events::event* make_static_event() noexcept { + static EventType event; + return &event; +} + +static const std::map event_map = { + { "__LOG__", make_static_event() }, + { "GUILD_CREATE", make_static_event() }, + { "GUILD_UPDATE", make_static_event() }, + { "GUILD_DELETE", make_static_event() }, + { "GUILD_MEMBER_UPDATE", make_static_event() }, + { "RESUMED", make_static_event() }, + { "READY", make_static_event() }, + { "CHANNEL_CREATE", make_static_event() }, + { "CHANNEL_UPDATE", make_static_event() }, + { "CHANNEL_DELETE", make_static_event() }, + { "PRESENCE_UPDATE", make_static_event() }, + { "TYPING_START", make_static_event() }, + { "MESSAGE_CREATE", make_static_event() }, + { "MESSAGE_UPDATE", make_static_event() }, + { "MESSAGE_DELETE", make_static_event() }, + { "MESSAGE_DELETE_BULK", make_static_event() }, + { "MESSAGE_REACTION_ADD", make_static_event() }, + { "MESSAGE_REACTION_REMOVE", make_static_event() }, + { "MESSAGE_REACTION_REMOVE_ALL", make_static_event() }, + { "MESSAGE_REACTION_REMOVE_EMOJI", make_static_event() }, + { "CHANNEL_PINS_UPDATE", make_static_event() }, + { "GUILD_BAN_ADD", make_static_event() }, + { "GUILD_BAN_REMOVE", make_static_event() }, + { "GUILD_EMOJIS_UPDATE", make_static_event() }, + { "GUILD_INTEGRATIONS_UPDATE", make_static_event() }, + { "INTEGRATION_CREATE", make_static_event() }, + { "INTEGRATION_UPDATE", make_static_event() }, + { "INTEGRATION_DELETE", make_static_event() }, + { "GUILD_MEMBER_ADD", make_static_event() }, + { "GUILD_MEMBER_REMOVE", make_static_event() }, + { "GUILD_MEMBERS_CHUNK", make_static_event() }, + { "GUILD_ROLE_CREATE", make_static_event() }, + { "GUILD_ROLE_UPDATE", make_static_event() }, + { "GUILD_ROLE_DELETE", make_static_event() }, + { "VOICE_STATE_UPDATE", make_static_event() }, + { "VOICE_SERVER_UPDATE", make_static_event() }, + { "WEBHOOKS_UPDATE", make_static_event() }, + { "INVITE_CREATE", make_static_event() }, + { "INVITE_DELETE", make_static_event() }, + { "INTERACTION_CREATE", make_static_event() }, + { "USER_UPDATE", make_static_event() }, + { "GUILD_JOIN_REQUEST_DELETE", make_static_event() }, { "GUILD_JOIN_REQUEST_UPDATE", nullptr }, - { "STAGE_INSTANCE_CREATE", new dpp::events::stage_instance_create() }, - { "STAGE_INSTANCE_UPDATE", new dpp::events::stage_instance_update() }, - { "STAGE_INSTANCE_DELETE", new dpp::events::stage_instance_delete() }, - { "THREAD_CREATE", new dpp::events::thread_create() }, - { "THREAD_UPDATE", new dpp::events::thread_update() }, - { "THREAD_DELETE", new dpp::events::thread_delete() }, - { "THREAD_LIST_SYNC", new dpp::events::thread_list_sync() }, - { "THREAD_MEMBER_UPDATE", new dpp::events::thread_member_update() }, - { "THREAD_MEMBERS_UPDATE", new dpp::events::thread_members_update() }, - { "GUILD_STICKERS_UPDATE", new dpp::events::guild_stickers_update() }, + { "STAGE_INSTANCE_CREATE", make_static_event() }, + { "STAGE_INSTANCE_UPDATE", make_static_event() }, + { "STAGE_INSTANCE_DELETE", make_static_event() }, + { "THREAD_CREATE", make_static_event() }, + { "THREAD_UPDATE", make_static_event() }, + { "THREAD_DELETE", make_static_event() }, + { "THREAD_LIST_SYNC", make_static_event() }, + { "THREAD_MEMBER_UPDATE", make_static_event() }, + { "THREAD_MEMBERS_UPDATE", make_static_event() }, + { "GUILD_STICKERS_UPDATE", make_static_event() }, { "GUILD_APPLICATION_COMMAND_COUNTS_UPDATE", nullptr }, { "APPLICATION_COMMAND_PERMISSIONS_UPDATE", nullptr }, { "EMBEDDED_ACTIVITY_UPDATE", nullptr }, { "GUILD_APPLICATION_COMMAND_INDEX_UPDATE", nullptr }, { "CHANNEL_TOPIC_UPDATE", nullptr }, { "VOICE_CHANNEL_STATUS_UPDATE", nullptr }, - { "GUILD_SCHEDULED_EVENT_CREATE", new dpp::events::guild_scheduled_event_create() }, - { "GUILD_SCHEDULED_EVENT_UPDATE", new dpp::events::guild_scheduled_event_update() }, - { "GUILD_SCHEDULED_EVENT_DELETE", new dpp::events::guild_scheduled_event_delete() }, - { "GUILD_SCHEDULED_EVENT_USER_ADD", new dpp::events::guild_scheduled_event_user_add() }, - { "GUILD_SCHEDULED_EVENT_USER_REMOVE", new dpp::events::guild_scheduled_event_user_remove() }, - { "AUTO_MODERATION_RULE_CREATE", new dpp::events::automod_rule_create() }, - { "AUTO_MODERATION_RULE_UPDATE", new dpp::events::automod_rule_update() }, - { "AUTO_MODERATION_RULE_DELETE", new dpp::events::automod_rule_delete() }, - { "AUTO_MODERATION_ACTION_EXECUTION", new dpp::events::automod_rule_execute() }, - { "GUILD_AUDIT_LOG_ENTRY_CREATE", new dpp::events::guild_audit_log_entry_create() }, - { "ENTITLEMENT_CREATE", new dpp::events::entitlement_create() }, - { "ENTITLEMENT_UPDATE", new dpp::events::entitlement_update() }, - { "ENTITLEMENT_DELETE", new dpp::events::entitlement_delete() }, + { "GUILD_SCHEDULED_EVENT_CREATE", make_static_event() }, + { "GUILD_SCHEDULED_EVENT_UPDATE", make_static_event() }, + { "GUILD_SCHEDULED_EVENT_DELETE", make_static_event() }, + { "GUILD_SCHEDULED_EVENT_USER_ADD", make_static_event() }, + { "GUILD_SCHEDULED_EVENT_USER_REMOVE", make_static_event() }, + { "AUTO_MODERATION_RULE_CREATE", make_static_event() }, + { "AUTO_MODERATION_RULE_UPDATE", make_static_event() }, + { "AUTO_MODERATION_RULE_DELETE", make_static_event() }, + { "AUTO_MODERATION_ACTION_EXECUTION", make_static_event() }, + { "GUILD_AUDIT_LOG_ENTRY_CREATE", make_static_event() }, + { "ENTITLEMENT_CREATE", make_static_event() }, + { "ENTITLEMENT_UPDATE", make_static_event() }, + { "ENTITLEMENT_DELETE", make_static_event() }, }; void discord_client::handle_event(const std::string &event, json &j, const std::string &raw) { - auto ev_iter = eventmap.find(event); - if (ev_iter != eventmap.end()) { + auto ev_iter = event_map.find(event); + if (ev_iter != event_map.end()) { /* A handler with nullptr is silently ignored. We don't plan to make a handler for it * so this usually some user-only thing that's crept into the API and shown to bots * that we dont care about.