]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
env: Use memcpy() instead of ad-hoc code to copy variable value
authorMarek Behún <marek.behun@nic.cz>
Sun, 17 Oct 2021 15:36:36 +0000 (17:36 +0200)
committerSimon Glass <sjg@chromium.org>
Thu, 21 Oct 2021 18:50:48 +0000 (12:50 -0600)
Copy the value of the found variable into given buffer with memcpy()
instead of ad-hoc code.

Signed-off-by: Marek Behún <marek.behun@nic.cz>
Reviewed-by: Simon Glass <sjg@chromium.org>
cmd/nvedit.c

index 527b522e9e9bc1a621e3eafc306678540d4f4596..ffcfb55f1057552abf017fc5d0fe8975d9e73e59 100644 (file)
@@ -735,7 +735,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)
@@ -746,20 +746,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;
        }