mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marc Kleine-Budde <mkl@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Jan Luebbe <jluebbe@debian.org>
Subject: [PATCH 3/3] state: uint8: add range check for uint8
Date: Mon, 15 Jun 2015 15:29:06 +0200	[thread overview]
Message-ID: <1434374946-7212-4-git-send-email-mkl@pengutronix.de> (raw)
In-Reply-To: <1434374946-7212-1-git-send-email-mkl@pengutronix.de>

Bail out, if the user tries to set a value > 255.

Cc: Jan Luebbe <jluebbe@debian.org>
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 common/state.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/common/state.c b/common/state.c
index e30f72660797..8fa027a847b9 100644
--- a/common/state.c
+++ b/common/state.c
@@ -113,6 +113,7 @@ static int state_set_dirty(struct param_d *p, void *priv)
 struct state_uint32 {
 	struct state_variable var;
 	struct param_d *param;
+	struct state *state;
 	uint32_t value;
 	uint32_t value_default;
 };
@@ -163,6 +164,17 @@ static int state_uint32_import(struct state_variable *sv,
 	return 0;
 }
 
+static int state_uint8_set(struct param_d *p, void *priv)
+{
+	struct state_uint32 *su32 = priv;
+	struct state *state = su32->state;
+
+	if (su32->value > 255)
+		return -ERANGE;
+
+	return state_set_dirty(p, state);
+}
+
 static struct state_variable *state_uint8_create(struct state *state,
 		const char *name, struct device_node *node)
 {
@@ -171,8 +183,8 @@ static struct state_variable *state_uint8_create(struct state *state,
 
 	su32 = xzalloc(sizeof(*su32));
 
-	param = dev_add_param_int(&state->dev, name, state_set_dirty,
-				  NULL, &su32->value, "%u", state);
+	param = dev_add_param_int(&state->dev, name, state_uint8_set,
+				  NULL, &su32->value, "%u", su32);
 	if (IS_ERR(param)) {
 		free(su32);
 		return ERR_CAST(param);
@@ -185,6 +197,7 @@ static struct state_variable *state_uint8_create(struct state *state,
 #else
 	su32->var.raw = &su32->value + 3;
 #endif
+	su32->state = state;
 
 	return &su32->var;
 }
-- 
2.1.4


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

  parent reply	other threads:[~2015-06-15 13:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-15 13:29 [PATCH 0/3] state: cleanup uint8 support Marc Kleine-Budde
2015-06-15 13:29 ` [PATCH 1/3] state: Documentation: add newly implemented "uint8" types Marc Kleine-Budde
2015-06-15 13:29 ` [PATCH 2/3] state: uint8: use %u as format string Marc Kleine-Budde
2015-06-15 13:29 ` Marc Kleine-Budde [this message]
2015-06-17  5:49 ` [PATCH 0/3] state: cleanup uint8 support Sascha Hauer

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=1434374946-7212-4-git-send-email-mkl@pengutronix.de \
    --to=mkl@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=jluebbe@debian.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