From 6d0fe8cffafc02c61d3c4a038ff016fb3baeae37 Mon Sep 17 00:00:00 2001 From: Jan Vaclav Date: Tue, 20 Aug 2024 16:23:23 +0200 Subject: [PATCH] nmcli/edit: fix memory leak in extract_setting_and_property In case the user selects a setting/property with "goto" command, and then attempts to tab-complete a setting/property pair, the original sett and prop strings are overriden without freeing: nmcli > goto 802-1x.pac-file nmcli 802-1x.pac-file> set 802-1.lal Fixes: 79bc2716852a ('cli: TAB-completion for enum-style property values (rh #1034126)') (cherry picked from commit ca47fd882ed14fc70586d257b37b8d8178d1cd49) (cherry picked from commit 796844dc096980815db2db81904170630cac468a) (cherry picked from commit d13f0c04edf30fef739ab3fab4f173c51cc65849) --- src/nmcli/connections.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/nmcli/connections.c b/src/nmcli/connections.c index 2618741aab..4890c3e8a9 100644 --- a/src/nmcli/connections.c +++ b/src/nmcli/connections.c @@ -6565,8 +6565,15 @@ extract_setting_and_property(const char *prompt, const char *line, char **settin p2 = dot + 1; num1 = strcspn(p1, "."); num2 = len > num1 + 1 ? len - num1 - 1 : 0; - sett = num1 > 0 ? g_strndup(p1, num1) : sett; - prop = num2 > 0 ? g_strndup(p2, num2) : prop; + if (num1 > 0) { + g_free(sett); + sett = g_strndup(p1, num1); + } + + if (num2 > 0) { + g_free(prop); + prop = g_strndup(p2, num2); + } } else { if (!prop) prop = len > 0 ? g_strndup(p1, len) : NULL;