From dff374d5f393a6b4bec4877aa1c35ceb70e82843 Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Fri, 26 Feb 2016 11:08:55 +0100 Subject: lib, irmd: Address Dimitri's comments This addresses several comments Dimitri had on the sockets layer code. --- src/irmd/main.c | 10 ++++---- src/lib/irm.c | 4 ++-- src/lib/sockets.c | 70 ++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 59 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/irmd/main.c b/src/irmd/main.c index d2aa6cd9..137b2b61 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -29,6 +29,7 @@ #include #include #include +#include #define BUF_SIZE 256 @@ -55,10 +56,10 @@ int main() if (sockfd < 0) return -1; - buf = malloc(sizeof(buf) * BUF_SIZE); - if (!buf) { + buf = malloc(sizeof(*buf) * BUF_SIZE); + if (buf == NULL) { LOG_ERR("Cannot allocate memory"); - return -1; + return -ENOMEM; } while (true) { @@ -70,6 +71,7 @@ int main() cli_sockfd = accept(sockfd, 0, 0); if (cli_sockfd < 0) { LOG_ERR("Cannot accept new connection"); + continue; } count = read(cli_sockfd, buf, BUF_SIZE); @@ -77,7 +79,7 @@ int main() buffer.size = count; buffer.data = buf; msg = deserialize_irm_msg(&buffer); - if (!msg) + if (msg == NULL) continue; LOG_DBG("Got message code %d", msg->code); diff --git a/src/lib/irm.c b/src/lib/irm.c index 1af8ed2c..2c9b530c 100644 --- a/src/lib/irm.c +++ b/src/lib/irm.c @@ -34,7 +34,7 @@ int irm_create_ipcp(rina_name_t name, struct irm_msg msg; buffer_t * buf; - if (!ipcp_type) + if (ipcp_type == NULL) return -1; sockfd = client_socket_open(IRM_SOCK_PATH); @@ -46,7 +46,7 @@ int irm_create_ipcp(rina_name_t name, msg.msgs.create_ipcp.ipcp_type = ipcp_type; buf = serialize_irm_msg(&msg); - if (!buf) + if (buf == NULL) return -1; write(sockfd, buf->data, buf->size); diff --git a/src/lib/sockets.c b/src/lib/sockets.c index 9985cd56..095c9e5c 100644 --- a/src/lib/sockets.c +++ b/src/lib/sockets.c @@ -103,7 +103,7 @@ static int serialized_string_len(uint8_t * data) while (*seek != '\0') seek++; - return seek - data + 1; + return (seek - data) + 1; } static void ser_copy_value(size_t flen, @@ -124,15 +124,18 @@ static void deser_copy_value(size_t flen, *offset += flen; } -static void deser_copy_string(uint8_t * data, - char ** dst, - int * offset) +static int deser_copy_string(uint8_t * data, + char ** dst, + int * offset) { size_t flen; flen = serialized_string_len(data + *offset); - *dst = malloc(flen + 1); + *dst = malloc(sizeof(**dst) * (flen + 1)); + if (*dst == NULL) + return -1; deser_copy_value(flen, *dst, data, offset); + return 0; } static void deser_copy_int(uint8_t * data, @@ -159,8 +162,16 @@ buffer_t * serialize_irm_msg(struct irm_msg * msg) int i; char buffer[BUFFER_SIZE]; - buf = malloc(sizeof(buf)); + buf = malloc(sizeof(*buf)); + if (buf == NULL) + return NULL; + buf->data = malloc(BUFFER_SIZE); + if (buf->data == NULL) { + free(buf); + return NULL; + } + data = buf->data; ser_copy_value(sizeof(enum irm_msg_code), @@ -230,7 +241,8 @@ struct irm_msg * deserialize_irm_msg(buffer_t * data) int i; int offset = 0; - if (!data || !data->data) { + if (data == NULL || + data->data == NULL) { LOG_ERR("Got a null pointer"); return NULL; } @@ -242,8 +254,8 @@ struct irm_msg * deserialize_irm_msg(buffer_t * data) } LOG_DBGF("Got buffer %s", buffer); - msg = malloc(sizeof(msg)); - if (!msg) { + msg = malloc(sizeof(*msg)); + if (msg == NULL) { LOG_ERR("Failed to allocate memory"); return NULL; } @@ -255,27 +267,47 @@ struct irm_msg * deserialize_irm_msg(buffer_t * data) switch (msg->code) { case IRM_CREATE_IPCP: msg->msgs.create_ipcp.name = - malloc(sizeof(msg->msgs.create_ipcp.name)); + malloc(sizeof(*(msg->msgs.create_ipcp.name))); + if (!msg->msgs.create_ipcp.name) { + LOG_ERR("Failed to alloc memory"); + free(msg); + return NULL; + } - deser_copy_string(data->data, - &msg->msgs.create_ipcp.name->ap_name, - &offset); + if (deser_copy_string(data->data, + &msg->msgs.create_ipcp.name->ap_name, + &offset)) { + free(msg->msgs.create_ipcp.name); + free(msg); + return NULL; + } deser_copy_int(data->data, &msg->msgs.create_ipcp.name->api_id, &offset); - deser_copy_string(data->data, - &msg->msgs.create_ipcp.name->ae_name, - &offset); + if (deser_copy_string(data->data, + &msg->msgs.create_ipcp.name->ae_name, + &offset)) { + free(msg->msgs.create_ipcp.name->ap_name); + free(msg->msgs.create_ipcp.name); + free(msg); + return NULL; + } deser_copy_int(data->data, &msg->msgs.create_ipcp.name->aei_id, &offset); - deser_copy_string(data->data, - &msg->msgs.create_ipcp.ipcp_type, - &offset); + if (deser_copy_string(data->data, + &msg->msgs.create_ipcp.ipcp_type, + &offset)) { + free(msg->msgs.create_ipcp.name->ae_name); + free(msg->msgs.create_ipcp.name->ap_name); + free(msg->msgs.create_ipcp.name); + free(msg); + return NULL; + } break; default: LOG_ERR("Don't know that code"); -- cgit v1.2.3