Skip to contentSkip to conversation

lib: parameter: Free previous string value in param_string_set

PATCH In review Robin van der Gracht 2 participants 3 messages Jun 16, 2022 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: parameter: Free previous string value in param_string_set

When the parameter has no set function the previous (saved_value) is not freed up on completion.

---
 lib/parameter.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/parameter.c +3 -1
index adc3c7cdea..fe7e7b1ba4 100644
@@ -262,8 +262,10 @@ static int param_string_set(struct device_d *dev, struct param_d *p, const char
 	value_new = strim(value_new);
 	*ps->value = value_new;
 
-	if (!ps->set)
+	if (!ps->set) {
+		free(value_save);
 		return 0;
+	}
 
 	ret = ps->set(p, p->driver_priv);
 	if (ret) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 13 lines · Robin van der Gracht

When the parameter has no set function the previous (saved_value) is not freed up on completion.

---
 lib/parameter.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/parameter.c +0 -0
index adc3c7cdea..fe7e7b1ba4 100644

lib/parameter.c static int param_string_set(struct device_d *dev, struct param_d *p, const char · line 268

Quoted 8 lines
lib/parameter.c +3 -1
@@ -262,8 +262,10 @@ static int param_string_set(struct device_d *dev, struct param_d *p, const char
 	value_new = strim(value_new);
 	*ps->value = value_new;
 
-	if (!ps->set)
+	if (!ps->set) {
+		free(value_save);
 		return 0;
+	}

Fixing this bug reveals another one. We do a:

	value_new = xstrdup(val);
	value_new = strim(value_new);

When 'val' has whitespaces at the beginning we no longer free the pointer originally returned from xstrdup, but the pointer with whitespaces stripped. I'll queue a fix for this before this patch.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#2 lore raw
Quoted 26 lines · Sascha Hauer
Quoted 14 lines · Robin van der Gracht

When the parameter has no set function the previous (saved_value) is not freed up on completion.

---
 lib/parameter.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
lib/parameter.c +0 -0
index adc3c7cdea..fe7e7b1ba4 100644
@@ -262,8 +262,10 @@ static int param_string_set(struct device_d *dev, 

struct param_d *p, const char

lib/parameter.c static int param_string_set(struct device_d *dev, struct param_d *p, const char · line 268

Quoted 8 lines
lib/parameter.c +3 -1
 	value_new = strim(value_new);
 	*ps->value = value_new;
 
-	if (!ps->set)
+	if (!ps->set) {
+		free(value_save);
 		return 0;
+	}

Fixing this bug reveals another one. We do a:

Quoted 6 lines
	value_new = xstrdup(val);
	value_new = strim(value_new);

When 'val' has whitespaces at the beginning we no longer free the pointer originally returned from xstrdup, but the pointer with whitespaces stripped. I'll queue a fix for this before this patch.

Good catch.

ACK

- Robin