Skip to contentSkip to conversation

tools: env: support skip leading zero of env

PATCH Superseded zqb-all 3 participants 5 messages Nov 27, 2018 – Nov 28, 2018 1 message not in the archive

lore
Share & tools
lore

zqb-all

author
#0 lore raw

PATCH v2 1/1 tools: env: support skip leading zero of env

---
v2: fix all "env = environment.data" and "env = default_environment"

 tools/env/fw_env.c | 46 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)
tools/env/fw_env.c +40 -6
index a5d7595..60742b6 100644
@@ -398,7 +398,14 @@ char *fw_getenv(char *name)
 {
 	char *env, *nxt;
 
-	for (env = environment.data; *env; env = nxt + 1) {
+	for (nxt = environment.data; !(*nxt); ++nxt) {
+		if (nxt >= &environment.data[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"environment is empty\n");
+			return NULL;
+		}
+	}
+	for (env = nxt; *env; env = nxt + 1) {
 		char *val;
 
 		for (nxt = env; *nxt; ++nxt) {
@@ -423,8 +430,14 @@ char *fw_getenv(char *name)
 char *fw_getdefenv(char *name)
 {
 	char *env, *nxt;
-
-	for (env = default_environment; *env; env = nxt + 1) {
+	for (nxt = default_environment; !(*nxt); ++nxt) {
+		if (nxt >= &default_environment[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"default environment is empty\n");
+			return NULL;
+		}
+	}
+	for (env = nxt; *env; env = nxt + 1) {
 		char *val;
 
 		for (nxt = env; *nxt; ++nxt) {
@@ -464,7 +477,14 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
 
 	if (argc == 0) {	/* Print all env variables  */
 		char *env, *nxt;
-		for (env = environment.data; *env; env = nxt + 1) {
+		for (nxt = environment.data; !(*nxt); ++nxt) {
+			if (nxt >= &environment.data[ENV_SIZE]) {
+				fprintf (stderr, "## Error: "
+					"environment is empty\n");
+				return -1;
+			}
+		}
+		for (env = nxt; *env; env = nxt + 1) {
 			for (nxt = env; *nxt; ++nxt) {
 				if (nxt >= &environment.data[ENV_SIZE]) {
 					fprintf(stderr, "## Error: "
@@ -537,7 +557,14 @@ int fw_env_write(char *name, char *value)
 	/*
 	 * search if variable with this name already exists
 	 */
-	for (nxt = env = environment.data; *env; env = nxt + 1) {
+	for (nxt = environment.data; !(*nxt); ++nxt) {
+		if (nxt >= &environment.data[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"environment is empty\n");
+			return -1;
+		}
+	}
+	for (env = nxt; *env; env = nxt + 1) {
 		for (nxt = env; *nxt; ++nxt) {
 			if (nxt >= &environment.data[ENV_SIZE]) {
 				fprintf(stderr, "## Error: "
@@ -614,7 +641,14 @@ int fw_env_write(char *name, char *value)
 	/*
 	 * Append new definition at the end
 	 */
-	for (env = environment.data; *env || *(env + 1); ++env)
+	for (nxt = environment.data; !(*nxt); ++nxt) {
+		if (nxt >= &environment.data[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"environment is empty\n");
+			return -1;
+		}
+	}
+	for (env = nxt; *env || *(env + 1); ++env)
 		;
 	if (env > environment.data)
 		++env;
Signature · 1 line
1.9.1
#1 lore raw

PATCH v2 1/1 tools: env: support skip leading zero of env

Maybe I miss something, but why is this needed exactly?

--

Stefan

Quoted 92 lines · zqb-all

---
v2: fix all "env = environment.data" and "env = default_environment"

 tools/env/fw_env.c | 46 ++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 40 insertions(+), 6 deletions(-)
tools/env/fw_env.c +40 -6
index a5d7595..60742b6 100644
@@ -398,7 +398,14 @@ char *fw_getenv(char *name)
 {
 	char *env, *nxt;
 
-	for (env = environment.data; *env; env = nxt + 1) {
+	for (nxt = environment.data; !(*nxt); ++nxt) {
+		if (nxt >= &environment.data[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"environment is empty\n");
+			return NULL;
+		}
+	}
+	for (env = nxt; *env; env = nxt + 1) {
 		char *val;
 
 		for (nxt = env; *nxt; ++nxt) {
@@ -423,8 +430,14 @@ char *fw_getenv(char *name)
 char *fw_getdefenv(char *name)
 {
 	char *env, *nxt;
-
-	for (env = default_environment; *env; env = nxt + 1) {
+	for (nxt = default_environment; !(*nxt); ++nxt) {
+		if (nxt >= &default_environment[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"default environment is empty\n");
+			return NULL;
+		}
+	}
+	for (env = nxt; *env; env = nxt + 1) {
 		char *val;
 
 		for (nxt = env; *nxt; ++nxt) {
@@ -464,7 +477,14 @@ int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
 
 	if (argc == 0) {	/* Print all env variables  */
 		char *env, *nxt;
-		for (env = environment.data; *env; env = nxt + 1) {
+		for (nxt = environment.data; !(*nxt); ++nxt) {
+			if (nxt >= &environment.data[ENV_SIZE]) {
+				fprintf (stderr, "## Error: "
+					"environment is empty\n");
+				return -1;
+			}
+		}
+		for (env = nxt; *env; env = nxt + 1) {
 			for (nxt = env; *nxt; ++nxt) {
 				if (nxt >= &environment.data[ENV_SIZE]) {
 					fprintf(stderr, "## Error: "
@@ -537,7 +557,14 @@ int fw_env_write(char *name, char *value)
 	/*
 	 * search if variable with this name already exists
 	 */
-	for (nxt = env = environment.data; *env; env = nxt + 1) {
+	for (nxt = environment.data; !(*nxt); ++nxt) {
+		if (nxt >= &environment.data[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"environment is empty\n");
+			return -1;
+		}
+	}
+	for (env = nxt; *env; env = nxt + 1) {
 		for (nxt = env; *nxt; ++nxt) {
 			if (nxt >= &environment.data[ENV_SIZE]) {
 				fprintf(stderr, "## Error: "
@@ -614,7 +641,14 @@ int fw_env_write(char *name, char *value)
 	/*
 	 * Append new definition at the end
 	 */
-	for (env = environment.data; *env || *(env + 1); ++env)
+	for (nxt = environment.data; !(*nxt); ++nxt) {
+		if (nxt >= &environment.data[ENV_SIZE]) {
+			fprintf (stderr, "## Error: "
+				"environment is empty\n");
+			return -1;
+		}
+	}
+	for (env = nxt; *env || *(env + 1); ++env)
 		;
 	if (env > environment.data)
 		++env;
#2 lore raw

PATCH v2 1/1 tools: env: support skip leading zero of env

In reply to zqb-all

Dear zqb-all,

Quoted 1 line · In message <[email protected]> you wrote:

Umm... please add a commit message that explains which problem you are trying to fix, and why you think your new code helps to address this problem.

Thanks.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
One possible reason that things aren't going  according  to  plan  is
that there never was a plan in the first place.
author
#3 lore raw

PATCH v2 1/1 tools: env: support skip leading zero of env

Hi Wolfgang , Stefan

I try to use uboot envtools in allwinner's platform, then found that the env in allwinner's SDK have some leading zero, that prevent envtools to parse it.
Finally I add these code to fix it.

Quoted 23 lines · Wolfgang Denk <[email protected]> 于2018年11月27日周二 下午11:00写道:

Dear zqb-all,

In message <[email protected]> you

Quoted 1 line · wrote:

Umm... please add a commit message that explains which problem you are trying to fix, and why you think your new code helps to address this problem.

Thanks.

Best regards,

Wolfgang Denk

--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de One possible reason that things aren't going according to plan is that there never was a plan in the first place.

Signature · 1 line
Best regards

A message that is not in the archive (sent off-list or not yet received): CAHaqGhZq+qLNNUbS1UnPjQDYepf=aUqkdLUeZ0g+B0kYNNg96A@mail.gmail.com

#5 lore raw

PATCH v2 1/1 tools: env: support skip leading zero of env

Dear 庄秋彬,

Quoted 4 lines · In message <CAHaqGhZq+qLNNUbS1UnPjQDYepf=aUqkdLUeZ0g+B0kYNNg96A@mail.gmail.com>…

I try to use uboot envtools in allwinner's platform, then found that the env in allwinner's SDK have some leading zero, that prevent envtools to parse it. Finally I add these code to fix it.

This is the wrong approach.

Either you misunderstand the format (for example in case of a redundant environment where you have to take the flag byte into consideration), or there is a bug in the implementation of the Allwinner SDK if it creates such a an environment.

In any case, it is not legal for the U-Boot environment to have any leading zeros. By definition, a sequence of two NUL-Bytes will mark the end of the environment.

You should fix the cause of the problem, and not modify the code to work on random / incorrect data.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Anyone can count the seeds in an apple.
No one can count the apples in a seed.