Remove uses of new

- Reduce chance of leak from exceptions being thrown
This commit is contained in:
Mike Wallio
2020-10-24 09:21:02 -04:00
parent edb1a864f0
commit 51a5ba57cc
3 changed files with 32 additions and 37 deletions

View File

@@ -3,6 +3,7 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <memory> #include <memory>
#include <vector>
#include <stdexcept> #include <stdexcept>
// extern "C" { // extern "C" {
@@ -93,8 +94,7 @@ enum class ReplyType : uint32_t {
* @brief i3 IPC message buffer * @brief i3 IPC message buffer
*/ */
struct buf_t { struct buf_t {
uint32_t size; ///< @brief Size of whole buffer std::vector<uint8_t> data;
uint8_t* data; ///< @brief Pointer to the message
/** /**
* @brief i3 IPC message header * @brief i3 IPC message header
@@ -110,8 +110,7 @@ struct buf_t {
*/ */
char* payload; char* payload;
buf_t(uint32_t payload_size); explicit buf_t(uint32_t payload_size);
~buf_t();
/** /**
* @brief Resize payload to the payload_size in the header * @brief Resize payload to the payload_size in the header

View File

@@ -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"; static const std::string g_i3_ipc_magic = "i3-ipc";
buf_t::buf_t(uint32_t payload_size) : size(sizeof(header_t) + payload_size) { buf_t::buf_t(uint32_t payload_size) :
data = new uint8_t[size]; data(sizeof(header_t) + payload_size, 0),
header = (header_t*)data; header(reinterpret_cast<header_t*>(data.data())),
payload = (char*)(data + sizeof(header_t)); payload(reinterpret_cast<char*>(data.data() + sizeof(header_t)))
strncpy(header->magic, g_i3_ipc_magic.c_str(), sizeof(header->magic)); {
std::copy_n(std::begin(g_i3_ipc_magic), sizeof(header->magic), header->magic);
header->size = payload_size; header->size = payload_size;
header->type = 0x0; header->type = 0x0;
} }
buf_t::~buf_t() {
delete[] data;
}
void buf_t::realloc_payload_to_header() { void buf_t::realloc_payload_to_header() {
uint8_t* new_data = new uint8_t[sizeof(header_t) + header->size]; data.resize(sizeof(*header) + header->size);
memcpy(new_data, header, sizeof(header_t)); header = reinterpret_cast<header_t*>(data.data());
delete[] data; payload = reinterpret_cast<char*>(data.data() + sizeof(header_t));
data = new_data;
header = (header_t*)data;
payload = (char*)(data + sizeof(header_t));
} }
@@ -78,10 +73,11 @@ void i3_disconnect(const int32_t sockfd) {
std::shared_ptr<buf_t> i3_pack(const ClientMessageType type, const std::string& payload) { std::shared_ptr<buf_t> i3_pack(const ClientMessageType type, const std::string& payload) {
buf_t* buff = new buf_t(payload.length()); auto buff{std::make_shared<buf_t>(payload.length())};
buff->header->type = static_cast<uint32_t>(type); buff->header->type = static_cast<uint32_t>(type);
strncpy(buff->payload, payload.c_str(), buff->header->size); std::copy_n(std::begin(payload), buff->header->size, buff->payload);
return std::shared_ptr<buf_t>(buff);
return buff;
} }
ssize_t writeall(int fd, const uint8_t* buf, size_t count) { 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) { 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<buf_t> i3_recv(const int32_t sockfd) { std::shared_ptr<buf_t> i3_recv(const int32_t sockfd) {
buf_t* buff = new buf_t(0); auto buff{std::make_shared<buf_t>(0)};
const uint32_t header_size = sizeof(header_t); const uint32_t header_size = sizeof(header_t);
{ {
uint8_t* header = (uint8_t*)buff->header; uint8_t* header = reinterpret_cast<uint8_t*>(buff->header);
uint32_t readed = 0; uint32_t readed = 0;
while (readed < header_size) { while (readed < header_size) {
int n = read(sockfd, header + readed, header_size - readed); int n = read(sockfd, header + readed, header_size - readed);
@@ -155,7 +151,7 @@ std::shared_ptr<buf_t> i3_recv(const int32_t sockfd) {
} }
} }
return std::shared_ptr<buf_t>(buff); return buff;
} }

View File

@@ -76,8 +76,8 @@ window_properties_t parse_window_props_from_json(const Json::Value& value) {
static std::shared_ptr<container_t> parse_container_from_json(const Json::Value& o) { static std::shared_ptr<container_t> parse_container_from_json(const Json::Value& o) {
#define i3IPC_TYPE_STR "PARSE CONTAINER FROM JSON" #define i3IPC_TYPE_STR "PARSE CONTAINER FROM JSON"
if (o.isNull()) if (o.isNull())
return std::shared_ptr<container_t>(); return nullptr;
std::shared_ptr<container_t> container (new container_t()); auto container{std::make_shared<container_t>()};
IPC_JSON_ASSERT_TYPE_OBJECT(o, "o") IPC_JSON_ASSERT_TYPE_OBJECT(o, "o")
container->id = o["id"].asUInt64(); container->id = o["id"].asUInt64();
@@ -152,7 +152,7 @@ static std::shared_ptr<container_t> parse_container_from_json(const Json::Value
static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value& value) { static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value& value) {
if (value.isNull()) if (value.isNull())
return std::shared_ptr<workspace_t>(); return nullptr;
Json::Value num = value["num"]; Json::Value num = value["num"];
Json::Value name = value["name"]; Json::Value name = value["name"];
Json::Value visible = value["visible"]; Json::Value visible = value["visible"];
@@ -161,7 +161,7 @@ static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value
Json::Value rect = value["rect"]; Json::Value rect = value["rect"];
Json::Value output = value["output"]; Json::Value output = value["output"];
std::shared_ptr<workspace_t> p (new workspace_t()); auto p{std::make_shared<workspace_t>()};
p->num = num.asInt(); p->num = num.asInt();
p->name = name.asString(); p->name = name.asString();
p->visible = visible.asBool(); p->visible = visible.asBool();
@@ -174,14 +174,14 @@ static std::shared_ptr<workspace_t> parse_workspace_from_json(const Json::Value
static std::shared_ptr<output_t> parse_output_from_json(const Json::Value& value) { static std::shared_ptr<output_t> parse_output_from_json(const Json::Value& value) {
if (value.isNull()) if (value.isNull())
return std::shared_ptr<output_t>(); return nullptr;
Json::Value name = value["name"]; Json::Value name = value["name"];
Json::Value active = value["active"]; Json::Value active = value["active"];
Json::Value primary = value["primary"]; Json::Value primary = value["primary"];
Json::Value current_workspace = value["current_workspace"]; Json::Value current_workspace = value["current_workspace"];
Json::Value rect = value["rect"]; Json::Value rect = value["rect"];
std::shared_ptr<output_t> p (new output_t()); auto p{std::make_shared<output_t>()};
p->name = name.asString(); p->name = name.asString();
p->active = active.asBool(); p->active = active.asBool();
p->primary = primary.asBool(); p->primary = primary.asBool();
@@ -193,9 +193,9 @@ static std::shared_ptr<output_t> parse_output_from_json(const Json::Value& val
static std::shared_ptr<binding_t> parse_binding_from_json(const Json::Value& value) { static std::shared_ptr<binding_t> parse_binding_from_json(const Json::Value& value) {
#define i3IPC_TYPE_STR "PARSE BINDING FROM JSON" #define i3IPC_TYPE_STR "PARSE BINDING FROM JSON"
if (value.isNull()) if (value.isNull())
return std::shared_ptr<binding_t>(); return nullptr;
IPC_JSON_ASSERT_TYPE_OBJECT(value, "binding") IPC_JSON_ASSERT_TYPE_OBJECT(value, "binding")
std::shared_ptr<binding_t> b (new binding_t()); auto b{std::make_shared<binding_t>()};
b->command = value["command"].asString(); b->command = value["command"].asString();
b->symbol = value["symbol"].asString(); b->symbol = value["symbol"].asString();
@@ -225,11 +225,11 @@ static std::shared_ptr<binding_t> parse_binding_from_json(const Json::Value& v
static std::shared_ptr<mode_t> parse_mode_from_json(const Json::Value& value) { static std::shared_ptr<mode_t> parse_mode_from_json(const Json::Value& value) {
if (value.isNull()) if (value.isNull())
return std::shared_ptr<mode_t>(); return nullptr;
Json::Value change = value["change"]; Json::Value change = value["change"];
Json::Value pango_markup = value["pango_markup"]; Json::Value pango_markup = value["pango_markup"];
std::shared_ptr<mode_t> p (new mode_t()); auto p{std::make_shared<mode_t>()};
p->change = change.asString(); p->change = change.asString();
p->pango_markup = pango_markup.asBool(); p->pango_markup = pango_markup.asBool();
return p; return p;
@@ -239,9 +239,9 @@ static std::shared_ptr<mode_t> parse_mode_from_json(const Json::Value& value)
static std::shared_ptr<bar_config_t> parse_bar_config_from_json(const Json::Value& value) { static std::shared_ptr<bar_config_t> parse_bar_config_from_json(const Json::Value& value) {
#define i3IPC_TYPE_STR "PARSE BAR CONFIG FROM JSON" #define i3IPC_TYPE_STR "PARSE BAR CONFIG FROM JSON"
if (value.isNull()) if (value.isNull())
return std::shared_ptr<bar_config_t>(); return nullptr;
IPC_JSON_ASSERT_TYPE_OBJECT(value, "(root)") IPC_JSON_ASSERT_TYPE_OBJECT(value, "(root)")
std::shared_ptr<bar_config_t> bc (new bar_config_t()); auto bc{std::make_shared<bar_config_t>()};
bc->id = value["id"].asString(); bc->id = value["id"].asString();
bc->status_command = value["status_command"].asString(); bc->status_command = value["status_command"].asString();