[U-Boot] [PATCH] tools: env: support skip leading zero of env

zqb-all zhuangqiubin at gmail.com
Sun Nov 18 05:52:20 UTC 2018


Signed-off-by: zqb-all <zhuangqiubin at gmail.com>
---

 tools/env/fw_env.c | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/tools/env/fw_env.c b/tools/env/fw_env.c
index a5d7595..4f4cd54 100644
--- a/tools/env/fw_env.c
+++ b/tools/env/fw_env.c
@@ -398,7 +398,14 @@ char *fw_getenv(char *name)
 {
 	char *env, *nxt;
 
-	for (env = environment.data; *env; env = nxt + 1) {
+	for (nxt = env = 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,15 @@ char *fw_getenv(char *name)
 char *fw_getdefenv(char *name)
 {
 	char *env, *nxt;
+	for (nxt = env = default_environment; !(*nxt); ++nxt) {
+		if (nxt >= &default_environment[ENV_SIZE]) {
+			fprintf(stderr,
+				"## Error: default environment is empty\n");
+			return NULL;
+		}
+	}
 
-	for (env = default_environment; *env; env = nxt + 1) {
+	for (env = nxt; *env; env = nxt + 1) {
 		char *val;
 
 		for (nxt = env; *nxt; ++nxt) {
@@ -464,7 +478,16 @@ 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 = env = 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 +560,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 = env = environment.data; !(*nxt); ++nxt) {
+		if (nxt >= &environment.data[ENV_SIZE]) {
+			fprintf(stderr, "## Error: environment is empty\n");
+			errno = EINVAL;
+			return -1;
+		}
+	}
+	for (env = nxt; *env; env = nxt + 1) {
 		for (nxt = env; *nxt; ++nxt) {
 			if (nxt >= &environment.data[ENV_SIZE]) {
 				fprintf(stderr, "## Error: "
-- 
1.9.1



More information about the U-Boot mailing list