mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] param_enum: protect against invalid values
Date: Tue, 23 Jun 2015 09:23:00 +0200	[thread overview]
Message-ID: <1435044180-3923-1-git-send-email-s.hauer@pengutronix.de> (raw)

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 0ac3b90..9e9f993 100644
--- a/lib/parameter.c
+++ b/lib/parameter.c
@@ -381,7 +381,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);
@@ -433,7 +433,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.1.4


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

                 reply	other threads:[~2015-06-23  7:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1435044180-3923-1-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox