From: Marek Behún <[email protected]>

Copy the value of the found variable into given buffer with memcpy()
instead of ad-hoc code.

Signed-off-by: Marek Behún <[email protected]>
---
 cmd/nvedit.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/cmd/nvedit.c b/cmd/nvedit.c
index 8989c85d20..7f094b3cd7 100644
--- a/cmd/nvedit.c
+++ b/cmd/nvedit.c
@@ -736,7 +736,7 @@ int env_get_f(const char *name, char *buf, unsigned len)
 
        for (p = env; *p != '\0'; p = end + 1) {
                const char *value;
-               int n, res;
+               unsigned res;
 
                for (end = p; *end != '\0'; ++end)
                        if (end - env >= CONFIG_ENV_SIZE)
@@ -747,20 +747,14 @@ int env_get_f(const char *name, char *buf, unsigned len)
                        continue;
 
                res = end - value;
+               memcpy(buf, value, min(len, res + 1));
 
-               /* found; copy out */
-               for (n = 0; n < len; ++n, ++buf) {
-                       *buf = *value++;
-                       if (*buf == '\0')
-                               return res;
+               if (len <= res) {
+                       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 res;
        }
 
-- 
2.32.0

Reply via email to