From 0a8680a7e870ad9e6328f50bbb29e515db763bb7 Mon Sep 17 00:00:00 2001 From: Karchnu Date: Wed, 8 Jul 2020 13:38:48 +0200 Subject: [PATCH] usock doesn't handle memory allocation anymore --- README.md | 8 ++++++++ src/usocket.c | 17 ----------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d926e16..84f7960 100644 --- a/README.md +++ b/README.md @@ -32,3 +32,11 @@ To remove logs: `make LDFLAGS=-DIPC_WITHOUT_ERRORS` # Planning for 1.0 - `libipc` should have usable bindings in several languages + + +# Implementation design + +## Memory management + +1. Prefer stack over mallocs. +2. Basic functions (such as *usock_*) should not handle memory allocation. diff --git a/src/usocket.c b/src/usocket.c index e462ec8..51e39fe 100644 --- a/src/usocket.c +++ b/src/usocket.c @@ -40,15 +40,6 @@ struct ipc_error usock_recv (const int32_t fd, char **buf, size_t * len) if (*len == 0) *len = IPC_MAX_MESSAGE_SIZE; - if (*buf == NULL) { - // do not allocate too much memory - if (*len > IPC_MAX_MESSAGE_SIZE) { - *len = IPC_MAX_MESSAGE_SIZE; - } - SECURE_BUFFER_HEAP_ALLOCATION (*buf, *len + IPC_HEADER_SIZE,, - IPC_RETURN_ERROR (IPC_ERROR_USOCK_RECV__HEAP_ALLOCATION)); - } - uint32_t msize = 0; uint32_t msize_read = 0; @@ -76,10 +67,6 @@ struct ipc_error usock_recv (const int32_t fd, char **buf, size_t * len) T_R ((msize > IPC_MAX_MESSAGE_SIZE), IPC_ERROR_USOCK_RECV__MESSAGE_SIZE); msize_read += ret_recv - IPC_HEADER_SIZE; } else if (ret_recv < 0) { - if (*buf != NULL) { - free (*buf); - *buf = NULL; - } *len = 0; switch (errno) { @@ -130,10 +117,6 @@ struct ipc_error usock_recv (const int32_t fd, char **buf, size_t * len) // 1 on none byte received, indicates a closed recipient if (ret_recv == 0) { - if (*buf != NULL) { - free (*buf); - *buf = NULL; - } *len = 0; IPC_RETURN_ERROR (IPC_ERROR_CLOSED_RECIPIENT); }