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).
This commit is contained in:
Thomas Haller 2023-03-28 13:34:40 +02:00
parent 346196792c
commit a65e80e8b6
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -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');