From c91dfb850ea1360d462ffdd1d9535b9c6da02ded Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 16 Mar 2021 11:37:56 +0100 Subject: initrd: avoid cloning string in reader_parse_rd_znet() The code did: key = g_strndup(tmp, val - tmp); val[0] = '\0'; That is pointless. If we strndup the key, we don't need to truncate the string at the '='. It might be nicer not to mutate the input string, however, the entire code with "argument" parsing is about mutating the input string, so that is something we apparently are fine with. As such, don't clone the string anymore. --- src/nm-initrd-generator/nmi-cmdline-reader.c | 30 +++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/nm-initrd-generator/nmi-cmdline-reader.c b/src/nm-initrd-generator/nmi-cmdline-reader.c index 2f50426d8d..d6b84c2e57 100644 --- a/src/nm-initrd-generator/nmi-cmdline-reader.c +++ b/src/nm-initrd-generator/nmi-cmdline-reader.c @@ -969,23 +969,25 @@ reader_parse_rd_znet(Reader *reader, char *argument, gboolean net_ifnames) NULL); while ((tmp = get_word(&argument, ',')) != NULL) { - char *val; + const char *key; + char * val; val = strchr(tmp, '='); - if (val) { - gs_free char *key = NULL; - - key = g_strndup(tmp, val - tmp); - val[0] = '\0'; - val++; - if (!_nm_setting_wired_is_valid_s390_option(key) - || !_nm_setting_wired_is_valid_s390_option_value(key, val)) { - /* Invalid setting. Silently ignore, but also ensure we - * didn't already set it. */ - nm_setting_wired_remove_s390_option(s_wired, key); - } else - nm_setting_wired_add_s390_option(s_wired, key, val); + if (!val) { + /* an invalid (or empty) entry. Ignore. */ + continue; } + + key = tmp; + val[0] = '\0'; + val++; + if (!_nm_setting_wired_is_valid_s390_option(key) + || !_nm_setting_wired_is_valid_s390_option_value(key, val)) { + /* Invalid setting. Silently ignore, but also ensure we + * didn't already set it. */ + nm_setting_wired_remove_s390_option(s_wired, key); + } else + nm_setting_wired_add_s390_option(s_wired, key, val); } } -- cgit v1.2.1