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>
Cc: mfe@pengutronix.de
Subject: [PATCH] commands: mw: Use kstrto*() instead of simple_strtoul()
Date: Thu, 23 May 2019 16:02:33 +0200	[thread overview]
Message-ID: <20190523140233.8507-1-s.hauer@pengutronix.de> (raw)

Now that we have the kstrto*() functions we can use them to detect
various errors in passing numbers on the command line. Start with
the 'mw' command and make it more robust against illegal numbers.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/mw.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/commands/mw.c b/commands/mw.c
index 2912997a31..3ed6c820d0 100644
--- a/commands/mw.c
+++ b/commands/mw.c
@@ -63,23 +63,31 @@ static int do_mem_mw(int argc, char *argv[])
 		u64 val64;
 		switch (mode) {
 		case O_RWSIZE_1:
-			val8 = simple_strtoul(argv[optind], NULL, 0);
+			ret = kstrtou8(argv[optind], 0, &val8);
+			if (ret)
+				goto illegal_number;
 			ret = write(fd, &val8, 1);
 			break;
 		case O_RWSIZE_2:
-			val16 = simple_strtoul(argv[optind], NULL, 0);
+			ret = kstrtou16(argv[optind], 0, &val16);
+			if (ret)
+				goto illegal_number;
 			if (swab)
 				val16 = __swab16(val16);
 			ret = write(fd, &val16, 2);
 			break;
 		case O_RWSIZE_4:
-			val32 = simple_strtoul(argv[optind], NULL, 0);
+			ret = kstrtou32(argv[optind], 0, &val32);
+			if (ret)
+				goto illegal_number;
 			if (swab)
 				val32 = __swab32(val32);
 			ret = write(fd, &val32, 4);
 			break;
 		case O_RWSIZE_8:
-			val64 = simple_strtoull(argv[optind], NULL, 0);
+			ret = kstrtou64(argv[optind], 0, &val64);
+			if (ret)
+				goto illegal_number;
 			if (swab)
 				val64 = __swab64(val64);
 			ret = write(fd, &val64, 8);
@@ -96,6 +104,14 @@ static int do_mem_mw(int argc, char *argv[])
 	close(fd);
 
 	return ret ? 1 : 0;
+
+illegal_number:
+	printf("\"%s\" is not a valid %d bit number\n", argv[optind],
+	       (mode >> O_RWSIZE_SHIFT) * 8);
+
+	close(fd);
+
+	return 1;
 }
 
 BAREBOX_CMD_HELP_START(mw)
-- 
2.20.1


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

                 reply	other threads:[~2019-05-23 14:02 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=20190523140233.8507-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=mfe@pengutronix.de \
    /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