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

refactor, fix: change interaction_response.msg from message* to message, fixing double free on copy #762

Merged
Merged
Show file tree
Hide file tree
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
25 changes: 16 additions & 9 deletions include/dpp/appcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,38 +315,45 @@ struct DPP_EXPORT interaction_response : public json_interface<interaction_respo
* Should be one of ir_pong, ir_channel_message_with_source,
* or ir_deferred_channel_message_with_source.
*/
interaction_response_type type;
interaction_response_type type{};

/**
* @brief A message object. This pointer is always valid
* while the containing interaction_response exists.
* @brief Message tied to this response.
*/
struct message* msg;
message msg{};

/**
* @brief Array of up to 25 autocomplete choices
*/
std::vector<command_option_choice> autocomplete_choices;
std::vector<command_option_choice> autocomplete_choices{};

/**
* @brief Construct a new interaction response object
*/
interaction_response();
interaction_response() = default;

/**
* @brief Construct a new interaction response object
*
* @param t Type of reply
*/
interaction_response(interaction_response_type t);

/**
* @brief Construct a new interaction response object
*
* @param t Type of reply
* @param m Message to reply with
*/
interaction_response(interaction_response_type t, const struct message& m);
interaction_response(interaction_response_type t, const message& m);

/**
* @brief Construct a new interaction response object
*
* @param t Type of reply
* @param m Message to reply with
*/
interaction_response(interaction_response_type t);
interaction_response(interaction_response_type t, message&& m);

/**
* @brief Fill object properties from JSON
Expand Down Expand Up @@ -374,7 +381,7 @@ struct DPP_EXPORT interaction_response : public json_interface<interaction_respo
/**
* @brief Destroy the interaction response object
*/
virtual ~interaction_response();
virtual ~interaction_response() = default;

};

Expand Down
2 changes: 1 addition & 1 deletion src/dpp/cluster/appcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void cluster::interaction_response_create(snowflake interaction_id, const std::s
if (callback) {
callback(confirmation_callback_t(this, confirmation(), http));
}
}, r.msg->filename, r.msg->filecontent, r.msg->filemimetype);
}, r.msg.filename, r.msg.filecontent, r.msg.filemimetype);
}

void cluster::interaction_response_edit(const std::string &token, const message &m, command_completion_event_t callback) {
Expand Down
25 changes: 5 additions & 20 deletions src/dpp/slashcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,20 +724,6 @@ void from_json(const nlohmann::json& j, interaction& i) {
}
}

interaction_response::interaction_response() : msg(nullptr) {
try {
msg = new message();
}
catch (std::bad_alloc&) {
delete msg;
throw;
}
}

interaction_response::~interaction_response() {
delete msg;
}

interaction_response& interaction_response::add_autocomplete_choice(const command_option_choice& achoice) {
if (autocomplete_choices.size() < AUTOCOMPLETE_MAX_CHOICES) {
this->autocomplete_choices.emplace_back(achoice);
Expand All @@ -746,10 +732,9 @@ interaction_response& interaction_response::add_autocomplete_choice(const comman
}


interaction_response::interaction_response(interaction_response_type t, const struct message& m) : interaction_response() {
type = t;
*msg = m;
}
interaction_response::interaction_response(interaction_response_type t, const message& m) : type{t}, msg{m} {}

interaction_response::interaction_response(interaction_response_type t, message&& m) : type{t}, msg{m} {}

interaction_response::interaction_response(interaction_response_type t) : interaction_response() {
type = t;
Expand All @@ -758,7 +743,7 @@ interaction_response::interaction_response(interaction_response_type t) : intera
interaction_response& interaction_response::fill_from_json(nlohmann::json* j) {
type = (interaction_response_type)int8_not_null(j, "type");
if (j->contains("data")) {
msg->fill_from_json(&((*j)["data"]));
msg.fill_from_json(&((*j)["data"]));
}
return *this;
}
Expand All @@ -784,7 +769,7 @@ std::string interaction_response::build_json(bool with_id) const {
json j;
j["type"] = this->type;
if (this->autocomplete_choices.empty()) {
json msg_json = json::parse(msg->build_json(false, true));
json msg_json = json::parse(msg.build_json(false, true));
auto cid = msg_json.find("channel_id");
if (cid != msg_json.end()) {
msg_json.erase(cid);
Expand Down
Loading