From 404fcb1c102af07a6760a80fa994d20e9a4de7f7 Mon Sep 17 00:00:00 2001 From: George Kiagiadakis Date: Fri, 9 Jul 2021 13:35:36 +0300 Subject: lib: avoid static buffers, use alloca() more Signed-off-by: George Kiagiadakis --- lib/client.c | 3 ++- lib/server.c | 7 ++++--- lib/utils.c | 7 +++++-- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/client.c b/lib/client.c index f1c4da1..d101223 100644 --- a/lib/client.c +++ b/lib/client.c @@ -6,6 +6,7 @@ * SPDX-License-Identifier: MIT */ +#include #include "private.h" #include "protocol.h" #include "sender.h" @@ -50,7 +51,7 @@ bool icipc_client_send_request( return false; const size_t size = icipc_protocol_calculate_request_size(name, args); - uint8_t buffer[size]; + uint8_t *buffer = alloca(size); icipc_protocol_build_request(buffer, size, name, args); return icipc_sender_send(base, buffer, size, reply, data); } diff --git a/lib/server.c b/lib/server.c index 6229ecd..6a67395 100644 --- a/lib/server.c +++ b/lib/server.c @@ -8,6 +8,7 @@ #include #include +#include #include "private.h" #include "protocol.h" @@ -69,7 +70,7 @@ static bool handle_message( if (!icipc_protocol_parse_request(buffer, size, &name, &args)) { const char *msg = "could not parse request"; const size_t s = icipc_protocol_calculate_reply_error_size(msg); - uint8_t b[s]; + uint8_t *b = alloca(s); icipc_protocol_build_reply_error(b, s, msg); return icipc_socket_write(sender_fd, b, s) == (ssize_t) s; } @@ -234,7 +235,7 @@ bool icipc_server_reply_ok( int client_fd, const struct icipc_data *value) { const size_t s = icipc_protocol_calculate_reply_ok_size(value); - uint8_t b[s]; + uint8_t *b = alloca(s); icipc_protocol_build_reply_ok(b, s, value); return icipc_socket_write(client_fd, b, s) == (ssize_t) s; } @@ -247,7 +248,7 @@ bool icipc_server_reply_error( return false; const size_t s = icipc_protocol_calculate_reply_error_size(msg); - uint8_t b[s]; + uint8_t *b = alloca(s); icipc_protocol_build_reply_error(b, s, msg); return icipc_socket_write(client_fd, b, s) == (ssize_t) s; } diff --git a/lib/utils.c b/lib/utils.c index 4d91241..37bcce9 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -91,7 +91,6 @@ int icipc_construct_socket_path(const char *name, char *buf, size_t buf_size) { bool path_is_absolute; const char *runtime_dir = NULL; struct passwd pwd, *result = NULL; - char buffer[4096]; int name_size; path_is_absolute = name[0] == '/'; @@ -105,8 +104,12 @@ int icipc_construct_socket_path(const char *name, char *buf, size_t buf_size) { if (runtime_dir == NULL) runtime_dir = getenv("USERPROFILE"); if (runtime_dir == NULL) { + long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); + if (bufsize == -1) + bufsize = 4096; + char *buffer = alloca(bufsize); if (getpwuid_r - (getuid(), &pwd, buffer, sizeof(buffer), + (getuid(), &pwd, buffer, bufsize, &result) == 0) runtime_dir = result ? result->pw_dir : NULL; } -- cgit 1.2.3-korg