[PATCH v2] param: add error check to __dev_add_param()

Masahiro Yamada yamada.m at jp.panasonic.com
Wed Jan 28 18:46:53 PST 2015


If the argument, name is given with NULL, it would be probably
unexpected behavior.  It should fail rather than register the
NULL-named parameter.

If strdup() fails with out-of-memory, it should also fail
with -ENOMEM.

Signed-off-by: Masahiro Yamada <yamada.m at jp.panasonic.com>
---

Changes in v2:
  - Fix the condition of returning -ENOMEM

 lib/parameter.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/parameter.c b/lib/parameter.c
index 71262c4..02a89bb 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -130,6 +130,13 @@ static int __dev_add_param(struct param_d *param, struct device_d *dev, const ch
 	if (get_param_by_name(dev, name))
 		return -EEXIST;
 
+	if (!name)
+		return -EINVAL;
+
+	param->name = strdup(name);
+	if (!param->name)
+		return -ENOMEM;
+
 	if (set)
 		param->set = set;
 	else
@@ -139,7 +146,6 @@ static int __dev_add_param(struct param_d *param, struct device_d *dev, const ch
 	else
 		param->get = param_get_generic;
 
-	param->name = strdup(name);
 	param->flags = flags;
 	param->dev = dev;
 	list_add_tail(&param->list, &dev->parameters);
-- 
1.9.1




More information about the barebox mailing list