From a65e80e8b6907c5666ff7f41316b0b00b78760f0 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 28 Mar 2023 13:34:40 +0200 Subject: [PATCH] core: pre-allocate exact buffer size for output in nm_utils_spawn_helper() It's easy enough to know how many bytes are needed. Just allocate the right size (+1, because NMStrBuf really likes to reserve that extra byte for the trailing NUL, even if it's not needed in this case). --- src/core/nm-core-utils.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/nm-core-utils.c b/src/core/nm-core-utils.c index 3dfdffbfba..880483b817 100644 --- a/src/core/nm-core-utils.c +++ b/src/core/nm-core-utils.c @@ -5099,6 +5099,7 @@ nm_utils_spawn_helper(const char *const *args, int fd_flags; const char *const *arg; GMainContext *context; + gsize n; nm_assert(args && args[0]); @@ -5176,7 +5177,9 @@ nm_utils_spawn_helper(const char *const *args, fcntl(info->child_stderr, F_SETFL, fd_flags | O_NONBLOCK); /* Watch process stdin */ - info->out_buffer = NM_STR_BUF_INIT(NM_UTILS_GET_NEXT_REALLOC_SIZE_40, TRUE); + for (n = 1, arg = args; *arg; arg++) + n += strlen(*arg) + 1u; + info->out_buffer = NM_STR_BUF_INIT(n, TRUE); for (arg = args; *arg; arg++) { nm_str_buf_append(&info->out_buffer, *arg); nm_str_buf_append_c(&info->out_buffer, '\0');