summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2023-03-28 13:34:40 +0200
committerThomas Haller <thaller@redhat.com>2023-04-03 10:27:43 +0200
commita65e80e8b6907c5666ff7f41316b0b00b78760f0 (patch)
tree94d1ca0aa2c0d0c4920c72db1e001536ad0f49b2
parent346196792c36a87e6c2c521c16611467207d88b9 (diff)
downloadNetworkManager-a65e80e8b6907c5666ff7f41316b0b00b78760f0.tar.gz
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).
-rw-r--r--src/core/nm-core-utils.c5
1 files changed, 4 insertions, 1 deletions
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');