Skip to contentSkip to conversation

util/guest-random: Fix size arg to tail memcpy

PATCH Applied Mark Nelson 4 participants 4 messages Jul 9, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/guest-random: Fix size arg to tail memcpy

We know that in the body of this if statement i is less than len, so we really should be copying len - i bytes not i - len bytes.

Fix this typo.

---
 util/guest-random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/guest-random.c +1 -1
index 086115bd67..23643f86cc 100644
@@ -38,7 +38,7 @@ static int glib_random_bytes(void *buf, size_t len)
     }
     if (i < len) {
         x = g_rand_int(rand);
-        __builtin_memcpy(buf + i, &x, i - len);
+        __builtin_memcpy(buf + i, &x, len - i);
     }
     return 0;
 }
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#2 lore raw
In reply to Mark Nelson
Quoted 5 lines · Mark Nelson

We know that in the body of this if statement i is less than len, so we really should be copying len - i bytes not i - len bytes.

Fix this typo.

Oops.

Fixes 8d8404f1564 ("util: Add qemu_guest_getrandom and associated

routines")

Quoted 19 lines
---
 util/guest-random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/guest-random.c +1 -1
index 086115bd67..23643f86cc 100644
@@ -38,7 +38,7 @@ static int glib_random_bytes(void *buf, size_t len)
     }
     if (i < len) {
         x = g_rand_int(rand);
-        __builtin_memcpy(buf + i, &x, i - len);
+        __builtin_memcpy(buf + i, &x, len - i);
     }
     return 0;
 }
#3 lore raw
In reply to Mark Nelson

Le 09/07/2021 à 14:06, Mark Nelson a écrit :

Quoted 16 lines

We know that in the body of this if statement i is less than len, so we really should be copying len - i bytes not i - len bytes.

Fix this typo.

---
 util/guest-random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/guest-random.c +0 -0
index 086115bd67..23643f86cc 100644

util/guest-random.c static int glib_random_bytes(void *buf, size_t len) · line 44

Quoted 8 lines
util/guest-random.c +1 -1
@@ -38,7 +38,7 @@ static int glib_random_bytes(void *buf, size_t len)
     }
     if (i < len) {
         x = g_rand_int(rand);
-        __builtin_memcpy(buf + i, &x, i - len);
+        __builtin_memcpy(buf + i, &x, len - i);
     }
     return 0;
 }

Applied to my trivial-patches branch.

Thanks,
Laurent