mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] param_enum: Make name strings const
@ 2015-09-01  6:18 Sascha Hauer
  2015-09-01  6:18 ` [PATCH 2/2] param_enum: protect against invalid values Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Sascha Hauer @ 2015-09-01  6:18 UTC (permalink / raw)
  To: Barebox List

Not only the array containing the pointers should be const but
also the strings themselves, so instead of using const char **
use const char * const *.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/globalvar.h | 4 ++--
 include/param.h     | 4 ++--
 lib/parameter.c     | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/include/globalvar.h b/include/globalvar.h
index 6e10956..e77209b 100644
--- a/include/globalvar.h
+++ b/include/globalvar.h
@@ -60,7 +60,7 @@ static inline int globalvar_add_simple_bool(const char *name,
 }
 
 static inline int globalvar_add_simple_enum(const char *name,
-		int *value, const char **names, int max)
+		int *value, const char * const *names, int max)
 {
 	struct param_d *p;
 
@@ -117,7 +117,7 @@ static inline int globalvar_add_simple_bool(const char *name,
 }
 
 static inline int globalvar_add_simple_enum(const char *name,
-		int *value, const char **names, int max)
+		int *value, const char * const *names, int max)
 {
 	return 0;
 }
diff --git a/include/param.h b/include/param.h
index 3a851fc..3fb4740 100644
--- a/include/param.h
+++ b/include/param.h
@@ -50,7 +50,7 @@ struct param_d *dev_add_param_bool(struct device_d *dev, const char *name,
 struct param_d *dev_add_param_enum(struct device_d *dev, const char *name,
 		int (*set)(struct param_d *p, void *priv),
 		int (*get)(struct param_d *p, void *priv),
-		int *value, const char **names, int max, void *priv);
+		int *value, const char * const *names, int max, void *priv);
 
 struct param_d *dev_add_param_int_ro(struct device_d *dev, const char *name,
 		int value, const char *format);
@@ -120,7 +120,7 @@ static inline struct param_d *dev_add_param_int(struct device_d *dev, const char
 static inline struct param_d *dev_add_param_enum(struct device_d *dev, const char *name,
 		int (*set)(struct param_d *p, void *priv),
 		int (*get)(struct param_d *p, void *priv),
-		int *value, const char **names, int max, void *priv)
+		int *value, const char * const *names, int max, void *priv)
 
 {
 	return ERR_PTR(-ENOSYS);
diff --git a/lib/parameter.c b/lib/parameter.c
index b238859..868b810 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -390,7 +390,7 @@ struct param_d *dev_add_param_int(struct device_d *dev, const char *name,
 struct param_enum {
 	struct param_d param;
 	int *value;
-	const char **names;
+	const char * const *names;
 	int num_names;
 	int (*set)(struct param_d *p, void *priv);
 	int (*get)(struct param_d *p, void *priv);
@@ -467,7 +467,7 @@ static void param_enum_info(struct param_d *p)
 struct param_d *dev_add_param_enum(struct device_d *dev, const char *name,
 		int (*set)(struct param_d *p, void *priv),
 		int (*get)(struct param_d *p, void *priv),
-		int *value, const char **names, int num_names, void *priv)
+		int *value, const char * const *names, int num_names, void *priv)
 {
 	struct param_enum *pe;
 	struct param_d *p;
-- 
2.5.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 2/2] param_enum: protect against invalid values
  2015-09-01  6:18 [PATCH 1/2] param_enum: Make name strings const Sascha Hauer
@ 2015-09-01  6:18 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2015-09-01  6:18 UTC (permalink / raw)
  To: Barebox List

Since dev_add_param_enum is passed a pointer containing the actual value
it can contain an invalid value. Protect against it so that we do not
access invalid array elements.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 lib/parameter.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lib/parameter.c b/lib/parameter.c
index 868b810..fd05b49 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -389,7 +389,7 @@ struct param_d *dev_add_param_int(struct device_d *dev, const char *name,
 
 struct param_enum {
 	struct param_d param;
-	int *value;
+	unsigned int *value;
 	const char * const *names;
 	int num_names;
 	int (*set)(struct param_d *p, void *priv);
@@ -441,7 +441,11 @@ static const char *param_enum_get(struct device_d *dev, struct param_d *p)
 	}
 
 	free(p->value);
-	p->value = strdup(pe->names[*pe->value]);
+
+	if (*pe->value >= pe->num_names)
+		p->value = asprintf("invalid:%d", *pe->value);
+	else
+		p->value = strdup(pe->names[*pe->value]);
 
 	return p->value;
 }
-- 
2.5.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-09-01  6:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-01  6:18 [PATCH 1/2] param_enum: Make name strings const Sascha Hauer
2015-09-01  6:18 ` [PATCH 2/2] param_enum: protect against invalid values Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox