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.
This commit is contained in:
Thomas Haller 2021-05-25 13:55:47 +02:00
parent ff9f2d27ec
commit 174f7bd27b
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -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)",