Skip to content

Commit

Permalink
refactor, fix: change interaction_response.msg from message* to messa…
Browse files Browse the repository at this point in the history
…ge, fixing double free on copy (#762)
  • Loading branch information
braindigitalis authored Aug 9, 2023
2 parents cdaaded + f670ce8 commit fb1a8c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
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 @@ -712,20 +712,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 @@ -734,10 +720,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 @@ -746,7 +731,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 @@ -772,7 +757,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

0 comments on commit fb1a8c8

Please sign in to comment.