mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/3] commands/test: Improve option parsing to handle "]" less special
Date: Tue, 28 Jan 2020 14:08:00 +0100	[thread overview]
Message-ID: <20200128130801.24890-3-u.kleine-koenig@pengutronix.de> (raw)
In-Reply-To: <20200128130801.24890-1-u.kleine-koenig@pengutronix.de>

Testing for *ap[1] != ']' is bogus during option processing. Instead
test if there are options left to be processed.

This fixes the return value for e.g.

	test -z ']lala'

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 commands/test.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/commands/test.c b/commands/test.c
index ab108fc845d5..9070e4907496 100644
--- a/commands/test.c
+++ b/commands/test.c
@@ -129,8 +129,11 @@ static int do_test(int argc, char *argv[])
 		case OPT_ZERO:
 		case OPT_NONZERO:
 			adv = 2;
+			if (left < 2)
+				break;
 			zero = 1;
-			if (ap[1] && *ap[1] != ']' && strlen(ap[1]))
+
+			if (strlen(ap[1]))
 				zero = 0;
 
 			expr = (opt == OPT_ZERO) ? zero : !zero;
@@ -141,7 +144,9 @@ static int do_test(int argc, char *argv[])
 		case OPT_EXISTS:
 		case OPT_SYMBOLIC_LINK:
 			adv = 2;
-			if (ap[1] && *ap[1] != ']' && strlen(ap[1])) {
+			if (left < 2)
+				break;
+			if (strlen(ap[1])) {
 				expr = (opt == OPT_SYMBOLIC_LINK ? lstat : stat)(ap[1], &statbuf);
 				if (expr < 0) {
 					expr = 0;
@@ -170,10 +175,8 @@ static int do_test(int argc, char *argv[])
 		/* three argument options */
 		default:
 			adv = 3;
-			if (left < 3) {
-				expr = 1;
+			if (left < 3)
 				break;
-			}
 
 			a = simple_strtol(ap[0], NULL, 0);
 			b = simple_strtol(ap[2], NULL, 0);
-- 
2.24.0


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

  parent reply	other threads:[~2020-01-28 13:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-28 13:07 [PATCH 0/3] commands/test: some improvements Uwe Kleine-König
2020-01-28 13:07 ` [PATCH 1/3] commands/test: Bail out on incomplete command line options Uwe Kleine-König
2020-02-03  7:57   ` Sascha Hauer
2020-02-03  8:50     ` Uwe Kleine-König
2020-01-28 13:08 ` Uwe Kleine-König [this message]
2020-01-28 13:08 ` [PATCH 3/3] commands/test: Implement -b and -c to test for character and block devices Uwe Kleine-König
2020-02-04  7:40 ` [PATCH 0/3] commands/test: some improvements 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=20200128130801.24890-3-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@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