mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] i2c_probe command: Use kstrtoint
@ 2020-02-14 10:40 Sascha Hauer
  0 siblings, 0 replies; only message in thread
From: Sascha Hauer @ 2020-02-14 10:40 UTC (permalink / raw)
  To: Barebox List

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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-02-14 10:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-14 10:40 [PATCH] i2c_probe command: Use kstrtoint Sascha Hauer

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