All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andreas Fenkart <afenkart@gmail.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH 1/5] tools: env validate: pass values as 0-based array
Date: Tue, 24 Nov 2015 14:21:11 +0100	[thread overview]
Message-ID: <1448371275-900-2-git-send-email-andreas.fenkart@dev.digitalstrom.org> (raw)
In-Reply-To: <1448371275-900-1-git-send-email-andreas.fenkart@dev.digitalstrom.org>

passing argv/argc can produce off-by-one errors

Signed-off-by: Andreas Fenkart <andreas.fenkart@dev.digitalstrom.org>
---
 common/env_flags.c  | 14 +++++++-------
 include/env_flags.h |  2 +-
 tools/env/fw_env.c  | 11 +++++++----
 3 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/common/env_flags.c b/common/env_flags.c
index 985f92e..fb5ac40 100644
--- a/common/env_flags.c
+++ b/common/env_flags.c
@@ -359,21 +359,21 @@ int env_flags_validate_varaccess(const char *name, int check_mask)
 /*
  * Validate the parameters to "env set" directly
  */
-int env_flags_validate_env_set_params(int argc, char * const argv[])
+int env_flags_validate_env_set_params(char *name, char * const val[], int count)
 {
-	if ((argc >= 3) && argv[2] != NULL) {
-		enum env_flags_vartype type = env_flags_get_type(argv[1]);
+	if ((count >= 1) && val[0] != NULL) {
+		enum env_flags_vartype type = env_flags_get_type(name);
 
 		/*
 		 * we don't currently check types that need more than
 		 * one argument
 		 */
-		if (type != env_flags_vartype_string && argc > 3) {
-			printf("## Error: too many parameters for setting "
-				"\"%s\"\n", argv[1]);
+		if (type != env_flags_vartype_string && count > 1) {
+			printf("## Error: too many parameters for setting \"%s\"\n",
+			       name);
 			return -1;
 		}
-		return env_flags_validate_type(argv[1], argv[2]);
+		return env_flags_validate_type(name, val[0]);
 	}
 	/* ok */
 	return 0;
diff --git a/include/env_flags.h b/include/env_flags.h
index 3ef6311..b153a71 100644
--- a/include/env_flags.h
+++ b/include/env_flags.h
@@ -121,7 +121,7 @@ int env_flags_validate_varaccess(const char *name, int check_mask);
 /*
  * Validate the parameters passed to "env set" for type compliance
  */
-int env_flags_validate_env_set_params(int argc, char * const argv[]);
+int env_flags_validate_env_set_params(char *name, char *const val[], int count);
 
 #else /* !USE_HOSTCC */
 
diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index 1173eea..bcf3756 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -481,8 +481,9 @@ int fw_setenv(int argc, char *argv[])
 {
 	int i, rc;
 	size_t len;
-	char *name;
+	char *name, **valv;
 	char *value = NULL;
+	int valc;
 
 	if (argc < 2) {
 		errno = EINVAL;
@@ -513,13 +514,15 @@ int fw_setenv(int argc, char *argv[])
 	}
 
 	name = argv[1];
+	valv = argv + 2;
+	valc = argc - 2;
 
-	if (env_flags_validate_env_set_params(argc, argv) < 0)
+	if (env_flags_validate_env_set_params(name, valv, valc) < 0)
 		return 1;
 
 	len = 0;
-	for (i = 2; i < argc; ++i) {
-		char *val = argv[i];
+	for (i = 0; i < valc; ++i) {
+		char *val = valv[i];
 		size_t val_len = strlen(val);
 
 		if (value)
-- 
2.6.2

  reply	other threads:[~2015-11-24 13:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-24 13:21 [U-Boot] [PATCH 0/5] tools: env: simplify argument parsing Andreas Fenkart
2015-11-24 13:21 ` Andreas Fenkart [this message]
2015-11-24 13:21 ` [U-Boot] [PATCH 2/5] tools: env: make parse_aes_key stateless Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 3/5] tools: env: introduce setenv/printenv argument structs Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 4/5] tools: env: parse aes key / suppress flag into argument struct Andreas Fenkart
2015-11-24 13:21 ` [U-Boot] [PATCH 5/5] tools: env: shift optind arguments and fix argument indices Andreas Fenkart
2015-11-24 19:26 ` [U-Boot] [PATCH 0/5] tools: env: simplify argument parsing Michael Heimpold

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=1448371275-900-2-git-send-email-andreas.fenkart@dev.digitalstrom.org \
    --to=afenkart@gmail.com \
    --cc=u-boot@lists.denx.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.