From 51a5ba57cc767d61e69fab87234f29d3c1e27008 Mon Sep 17 00:00:00 2001 From: Mike Wallio Date: Sat, 24 Oct 2020 09:21:02 -0400 Subject: [PATCH] Remove uses of new - Reduce chance of leak from exceptions being thrown --- include/i3ipc++/ipc-util.hpp | 7 +++---- src/ipc-util.cpp | 38 ++++++++++++++++-------------------- src/ipc.cpp | 24 +++++++++++------------ 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/include/i3ipc++/ipc-util.hpp b/include/i3ipc++/ipc-util.hpp index c4894a6..b0f284d 100644 --- a/include/i3ipc++/ipc-util.hpp +++ b/include/i3ipc++/ipc-util.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include // extern "C" { @@ -93,8 +94,7 @@ enum class ReplyType : uint32_t { * @brief i3 IPC message buffer */ struct buf_t { - uint32_t size; ///< @brief Size of whole buffer - uint8_t* data; ///< @brief Pointer to the message + std::vector data; /** * @brief i3 IPC message header @@ -110,8 +110,7 @@ struct buf_t { */ char* payload; - buf_t(uint32_t payload_size); - ~buf_t(); + explicit buf_t(uint32_t payload_size); /** * @brief Resize payload to the payload_size in the header diff --git a/src/ipc-util.cpp b/src/ipc-util.cpp index 7c1f28f..2ba7fe1 100644 --- a/src/ipc-util.cpp +++ b/src/ipc-util.cpp @@ -30,25 +30,20 @@ errno_error::errno_error(const std::string& msg) : ipc_error(format_errno(msg)) static const std::string g_i3_ipc_magic = "i3-ipc"; -buf_t::buf_t(uint32_t payload_size) : size(sizeof(header_t) + payload_size) { - data = new uint8_t[size]; - header = (header_t*)data; - payload = (char*)(data + sizeof(header_t)); - strncpy(header->magic, g_i3_ipc_magic.c_str(), sizeof(header->magic)); +buf_t::buf_t(uint32_t payload_size) : + data(sizeof(header_t) + payload_size, 0), + header(reinterpret_cast(data.data())), + payload(reinterpret_cast(data.data() + sizeof(header_t))) +{ + std::copy_n(std::begin(g_i3_ipc_magic), sizeof(header->magic), header->magic); header->size = payload_size; header->type = 0x0; } -buf_t::~buf_t() { - delete[] data; -} void buf_t::realloc_payload_to_header() { - uint8_t* new_data = new uint8_t[sizeof(header_t) + header->size]; - memcpy(new_data, header, sizeof(header_t)); - delete[] data; - data = new_data; - header = (header_t*)data; - payload = (char*)(data + sizeof(header_t)); + data.resize(sizeof(*header) + header->size); + header = reinterpret_cast(data.data()); + payload = reinterpret_cast(data.data() + sizeof(header_t)); } @@ -78,10 +73,11 @@ void i3_disconnect(const int32_t sockfd) { std::shared_ptr i3_pack(const ClientMessageType type, const std::string& payload) { - buf_t* buff = new buf_t(payload.length()); + auto buff{std::make_shared(payload.length())}; buff->header->type = static_cast(type); - strncpy(buff->payload, payload.c_str(), buff->header->size); - return std::shared_ptr(buff); + std::copy_n(std::begin(payload), buff->header->size, buff->payload); + + return buff; } ssize_t writeall(int fd, const uint8_t* buf, size_t count) { @@ -112,15 +108,15 @@ ssize_t swrite(int fd, const uint8_t* buf, size_t count) { } void i3_send(const int32_t sockfd, const buf_t& buff) { - swrite(sockfd, buff.data, buff.size); + swrite(sockfd, buff.data.data(), buff.data.size()); } std::shared_ptr i3_recv(const int32_t sockfd) { - buf_t* buff = new buf_t(0); + auto buff{std::make_shared(0)}; const uint32_t header_size = sizeof(header_t); { - uint8_t* header = (uint8_t*)buff->header; + uint8_t* header = reinterpret_cast(buff->header); uint32_t readed = 0; while (readed < header_size) { int n = read(sockfd, header + readed, header_size - readed); @@ -155,7 +151,7 @@ std::shared_ptr i3_recv(const int32_t sockfd) { } } - return std::shared_ptr(buff); + return buff; } diff --git a/src/ipc.cpp b/src/ipc.cpp index e4f06bd..c7d2513 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -76,8 +76,8 @@ window_properties_t parse_window_props_from_json(const Json::Value& value) { static std::shared_ptr parse_container_from_json(const Json::Value& o) { #define i3IPC_TYPE_STR "PARSE CONTAINER FROM JSON" if (o.isNull()) - return std::shared_ptr(); - std::shared_ptr container (new container_t()); + return nullptr; + auto container{std::make_shared()}; IPC_JSON_ASSERT_TYPE_OBJECT(o, "o") container->id = o["id"].asUInt64(); @@ -152,7 +152,7 @@ static std::shared_ptr parse_container_from_json(const Json::Value static std::shared_ptr parse_workspace_from_json(const Json::Value& value) { if (value.isNull()) - return std::shared_ptr(); + return nullptr; Json::Value num = value["num"]; Json::Value name = value["name"]; Json::Value visible = value["visible"]; @@ -161,7 +161,7 @@ static std::shared_ptr parse_workspace_from_json(const Json::Value Json::Value rect = value["rect"]; Json::Value output = value["output"]; - std::shared_ptr p (new workspace_t()); + auto p{std::make_shared()}; p->num = num.asInt(); p->name = name.asString(); p->visible = visible.asBool(); @@ -174,14 +174,14 @@ static std::shared_ptr parse_workspace_from_json(const Json::Value static std::shared_ptr parse_output_from_json(const Json::Value& value) { if (value.isNull()) - return std::shared_ptr(); + return nullptr; Json::Value name = value["name"]; Json::Value active = value["active"]; Json::Value primary = value["primary"]; Json::Value current_workspace = value["current_workspace"]; Json::Value rect = value["rect"]; - std::shared_ptr p (new output_t()); + auto p{std::make_shared()}; p->name = name.asString(); p->active = active.asBool(); p->primary = primary.asBool(); @@ -193,9 +193,9 @@ static std::shared_ptr parse_output_from_json(const Json::Value& val static std::shared_ptr parse_binding_from_json(const Json::Value& value) { #define i3IPC_TYPE_STR "PARSE BINDING FROM JSON" if (value.isNull()) - return std::shared_ptr(); + return nullptr; IPC_JSON_ASSERT_TYPE_OBJECT(value, "binding") - std::shared_ptr b (new binding_t()); + auto b{std::make_shared()}; b->command = value["command"].asString(); b->symbol = value["symbol"].asString(); @@ -225,11 +225,11 @@ static std::shared_ptr parse_binding_from_json(const Json::Value& v static std::shared_ptr parse_mode_from_json(const Json::Value& value) { if (value.isNull()) - return std::shared_ptr(); + return nullptr; Json::Value change = value["change"]; Json::Value pango_markup = value["pango_markup"]; - std::shared_ptr p (new mode_t()); + auto p{std::make_shared()}; p->change = change.asString(); p->pango_markup = pango_markup.asBool(); return p; @@ -239,9 +239,9 @@ static std::shared_ptr parse_mode_from_json(const Json::Value& value) static std::shared_ptr parse_bar_config_from_json(const Json::Value& value) { #define i3IPC_TYPE_STR "PARSE BAR CONFIG FROM JSON" if (value.isNull()) - return std::shared_ptr(); + return nullptr; IPC_JSON_ASSERT_TYPE_OBJECT(value, "(root)") - std::shared_ptr bc (new bar_config_t()); + auto bc{std::make_shared()}; bc->id = value["id"].asString(); bc->status_command = value["status_command"].asString();