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] i2c_probe command: Use kstrtoint
Date: Fri, 14 Feb 2020 11:40:30 +0100	[thread overview]
Message-ID: <20200214104030.2149-1-s.hauer@pengutronix.de> (raw)

Use kstrtoint rather than simple_strtoul to catch erroneous input like
"i2c1" which previously was silently interpreted as "0".

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/i2c.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/commands/i2c.c b/commands/i2c.c
index 77d65e3fa3..77a8f7ff97 100644
--- a/commands/i2c.c
+++ b/commands/i2c.c
@@ -45,17 +45,37 @@ static int do_i2c_probe(int argc, char *argv[])
 {
 	struct i2c_adapter *adapter = NULL;
 	int startaddr = 4, stopaddr = 0x77;
+	int ret;
 
 	if (argc > 1) {
-		adapter = i2c_get_adapter(simple_strtoul(argv[1], NULL, 0));
+		int busnum;
+
+		ret = kstrtoint(argv[1], 0, &busnum);
+		if (ret) {
+			printf("Cannot parse \"%s\" as a number\n", argv[1]);
+			return ret;
+		}
+
+		adapter = i2c_get_adapter(busnum);
 		if (!adapter)
 			return -ENODEV;
 	}
 
-	if (argc > 2)
-		startaddr = simple_strtol(argv[2], NULL, 0);
-	if (argc > 3)
-		stopaddr = simple_strtol(argv[3], NULL, 0);
+	if (argc > 2) {
+		ret = kstrtoint(argv[2], 0, &startaddr);
+		if (ret) {
+			printf("Cannot parse \"%s\" as a number\n", argv[1]);
+			return ret;
+		}
+	}
+
+	if (argc > 3) {
+		ret = kstrtoint(argv[3], 0, &stopaddr);
+		if (ret) {
+			printf("Cannot parse \"%s\" as a number\n", argv[1]);
+			return ret;
+		}
+	}
 
 	if (stopaddr > 0x7f)
 		stopaddr = 0x7f;
-- 
2.25.0


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

                 reply	other threads:[~2020-02-14 10:40 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=20200214104030.2149-1-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