[PATCH 08/10] env: Use strncpy() instead of ad-hoc code to copy variable value

Marek Behún kabel at kernel.org
Tue Oct 12 13:04:59 CEST 2021


From: Marek Behún <marek.behun at nic.cz>

Copy the value of the found variable into given buffer with strncpy().

Signed-off-by: Marek Behún <marek.behun at nic.cz>
---
 cmd/nvedit.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)

diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index 412918a000..51b9e4ffa4 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -746,18 +746,13 @@ int env_get_f(const char *name, char *buf, unsigned len)
 			continue;
 
 		/* found; copy out */
-		for (n = 0; n < len; ++n, ++buf) {
-			*buf = env[val++];
-			if (*buf == '\0')
-				return n;
+		n = strncpy(buf, &env[val], len) - buf;
+		if (len && n == len) {
+			buf[len - 1] = '\0';
+			printf("env_buf [%u bytes] too small for value of \"%s\"\n",
+			       len, name);
 		}
 
-		if (n)
-			*--buf = '\0';
-
-		printf("env_buf [%u bytes] too small for value of \"%s\"\n",
-		       len, name);
-
 		return n;
 	}
 
-- 
2.32.0



More information about the U-Boot mailing list