From 174f7bd27b04a57c965cb52bf39dfd9b7c1582a5 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 25 May 2021 13:55:47 +0200 Subject: [PATCH] core: rework string handling in enslave_slave() Coverity doesn't like the previous code: Error: RESOURCE_LEAK (CWE-772): [#def34] [important] NetworkManager-1.31.5/src/core/devices/team/nm-device-team.c:835: alloc_fn: Storage is returned from allocation function "g_strdup". NetworkManager-1.31.5/src/core/devices/team/nm-device-team.c:835: noescape: Resource "g_strdup(config)" is not freed or pointed-to in "g_strdelimit". NetworkManager-1.31.5/src/core/devices/team/nm-device-team.c:835: leaked_storage: Failing to save or free storage allocated by "g_strdup(config)" leaks it. # 833| char *sanitized_config; # 834| # 835|-> sanitized_config = g_strdelimit(g_strdup(config), "\r\n", ' '); # 836| err = teamdctl_port_config_update_raw(priv->tdc, slave_iface, sanitized_config); # 837| g_free(sanitized_config); Maybe this works better. --- src/core/devices/team/nm-device-team.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/devices/team/nm-device-team.c b/src/core/devices/team/nm-device-team.c index 9c78987aee..0f1a8c4cec 100644 --- a/src/core/devices/team/nm-device-team.c +++ b/src/core/devices/team/nm-device-team.c @@ -829,12 +829,12 @@ enslave_slave(NMDevice *device, NMDevice *slave, NMConnection *connection, gbool "enslaved team port %s config not changed, not connected to teamd", slave_iface); } else { - int err; - char *sanitized_config; + gs_free char *sanitized_config = NULL; + int err; - sanitized_config = g_strdelimit(g_strdup(config), "\r\n", ' '); + sanitized_config = g_strdup(config); + g_strdelimit(sanitized_config, "\r\n", ' '); err = teamdctl_port_config_update_raw(priv->tdc, slave_iface, sanitized_config); - g_free(sanitized_config); if (err != 0) { _LOGE(LOGD_TEAM, "failed to update config for port %s (err=%d)",