Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid memory leaks in event_map #1043

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 74 additions & 68 deletions src/dpp/discordevents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,85 +320,91 @@ void set_ts_not_null(const json* j, const char* keyname, time_t &v)
}
}

const std::map<std::string, dpp::events::event*> 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 <typename EventType>
static dpp::events::event* make_static_event() noexcept {
static EventType event;
return &event;
}

static const std::map<std::string, dpp::events::event*> event_map = {
{ "__LOG__", make_static_event<dpp::events::logger>() },
{ "GUILD_CREATE", make_static_event<dpp::events::guild_create>() },
{ "GUILD_UPDATE", make_static_event<dpp::events::guild_update>() },
{ "GUILD_DELETE", make_static_event<dpp::events::guild_delete>() },
{ "GUILD_MEMBER_UPDATE", make_static_event<dpp::events::guild_member_update>() },
{ "RESUMED", make_static_event<dpp::events::resumed>() },
{ "READY", make_static_event<dpp::events::ready>() },
{ "CHANNEL_CREATE", make_static_event<dpp::events::channel_create>() },
{ "CHANNEL_UPDATE", make_static_event<dpp::events::channel_update>() },
{ "CHANNEL_DELETE", make_static_event<dpp::events::channel_delete>() },
{ "PRESENCE_UPDATE", make_static_event<dpp::events::presence_update>() },
{ "TYPING_START", make_static_event<dpp::events::typing_start>() },
{ "MESSAGE_CREATE", make_static_event<dpp::events::message_create>() },
{ "MESSAGE_UPDATE", make_static_event<dpp::events::message_update>() },
{ "MESSAGE_DELETE", make_static_event<dpp::events::message_delete>() },
{ "MESSAGE_DELETE_BULK", make_static_event<dpp::events::message_delete_bulk>() },
{ "MESSAGE_REACTION_ADD", make_static_event<dpp::events::message_reaction_add>() },
{ "MESSAGE_REACTION_REMOVE", make_static_event<dpp::events::message_reaction_remove>() },
{ "MESSAGE_REACTION_REMOVE_ALL", make_static_event<dpp::events::message_reaction_remove_all>() },
{ "MESSAGE_REACTION_REMOVE_EMOJI", make_static_event<dpp::events::message_reaction_remove_emoji>() },
{ "CHANNEL_PINS_UPDATE", make_static_event<dpp::events::channel_pins_update>() },
{ "GUILD_BAN_ADD", make_static_event<dpp::events::guild_ban_add>() },
{ "GUILD_BAN_REMOVE", make_static_event<dpp::events::guild_ban_remove>() },
{ "GUILD_EMOJIS_UPDATE", make_static_event<dpp::events::guild_emojis_update>() },
{ "GUILD_INTEGRATIONS_UPDATE", make_static_event<dpp::events::guild_integrations_update>() },
{ "INTEGRATION_CREATE", make_static_event<dpp::events::integration_create>() },
{ "INTEGRATION_UPDATE", make_static_event<dpp::events::integration_update>() },
{ "INTEGRATION_DELETE", make_static_event<dpp::events::integration_delete>() },
{ "GUILD_MEMBER_ADD", make_static_event<dpp::events::guild_member_add>() },
{ "GUILD_MEMBER_REMOVE", make_static_event<dpp::events::guild_member_remove>() },
{ "GUILD_MEMBERS_CHUNK", make_static_event<dpp::events::guild_members_chunk>() },
{ "GUILD_ROLE_CREATE", make_static_event<dpp::events::guild_role_create>() },
{ "GUILD_ROLE_UPDATE", make_static_event<dpp::events::guild_role_update>() },
{ "GUILD_ROLE_DELETE", make_static_event<dpp::events::guild_role_delete>() },
{ "VOICE_STATE_UPDATE", make_static_event<dpp::events::voice_state_update>() },
{ "VOICE_SERVER_UPDATE", make_static_event<dpp::events::voice_server_update>() },
{ "WEBHOOKS_UPDATE", make_static_event<dpp::events::webhooks_update>() },
{ "INVITE_CREATE", make_static_event<dpp::events::invite_create>() },
{ "INVITE_DELETE", make_static_event<dpp::events::invite_delete>() },
{ "INTERACTION_CREATE", make_static_event<dpp::events::interaction_create>() },
{ "USER_UPDATE", make_static_event<dpp::events::user_update>() },
{ "GUILD_JOIN_REQUEST_DELETE", make_static_event<dpp::events::guild_join_request_delete>() },
{ "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<dpp::events::stage_instance_create>() },
{ "STAGE_INSTANCE_UPDATE", make_static_event<dpp::events::stage_instance_update>() },
{ "STAGE_INSTANCE_DELETE", make_static_event<dpp::events::stage_instance_delete>() },
{ "THREAD_CREATE", make_static_event<dpp::events::thread_create>() },
{ "THREAD_UPDATE", make_static_event<dpp::events::thread_update>() },
{ "THREAD_DELETE", make_static_event<dpp::events::thread_delete>() },
{ "THREAD_LIST_SYNC", make_static_event<dpp::events::thread_list_sync>() },
{ "THREAD_MEMBER_UPDATE", make_static_event<dpp::events::thread_member_update>() },
{ "THREAD_MEMBERS_UPDATE", make_static_event<dpp::events::thread_members_update>() },
{ "GUILD_STICKERS_UPDATE", make_static_event<dpp::events::guild_stickers_update>() },
{ "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<dpp::events::guild_scheduled_event_create>() },
{ "GUILD_SCHEDULED_EVENT_UPDATE", make_static_event<dpp::events::guild_scheduled_event_update>() },
{ "GUILD_SCHEDULED_EVENT_DELETE", make_static_event<dpp::events::guild_scheduled_event_delete>() },
{ "GUILD_SCHEDULED_EVENT_USER_ADD", make_static_event<dpp::events::guild_scheduled_event_user_add>() },
{ "GUILD_SCHEDULED_EVENT_USER_REMOVE", make_static_event<dpp::events::guild_scheduled_event_user_remove>() },
{ "AUTO_MODERATION_RULE_CREATE", make_static_event<dpp::events::automod_rule_create>() },
{ "AUTO_MODERATION_RULE_UPDATE", make_static_event<dpp::events::automod_rule_update>() },
{ "AUTO_MODERATION_RULE_DELETE", make_static_event<dpp::events::automod_rule_delete>() },
{ "AUTO_MODERATION_ACTION_EXECUTION", make_static_event<dpp::events::automod_rule_execute>() },
{ "GUILD_AUDIT_LOG_ENTRY_CREATE", make_static_event<dpp::events::guild_audit_log_entry_create>() },
{ "ENTITLEMENT_CREATE", make_static_event<dpp::events::entitlement_create>() },
{ "ENTITLEMENT_UPDATE", make_static_event<dpp::events::entitlement_update>() },
{ "ENTITLEMENT_DELETE", make_static_event<dpp::events::entitlement_delete>() },
};

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.
Expand Down