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

Mark Nelson posted 1 patch 4 years, 7 months ago
Test checkpatch passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20210709120600.11080-1-mdnelson8@gmail.com
util/guest-random.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] util/guest-random: Fix size arg to tail memcpy
Posted by Mark Nelson 4 years, 7 months ago
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.

Signed-off-by: Mark Nelson <mdnelson8@gmail.com>
---
 util/guest-random.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/guest-random.c b/util/guest-random.c
index 086115bd67..23643f86cc 100644
--- a/util/guest-random.c
+++ b/util/guest-random.c
@@ -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;
 }
-- 
2.30.2


Re: [PATCH] util/guest-random: Fix size arg to tail memcpy
Posted by Richard Henderson 4 years, 7 months ago
On 7/9/21 5:06 AM, Mark Nelson wrote:
> 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.
> 
> Signed-off-by: Mark Nelson<mdnelson8@gmail.com>
> ---
>   util/guest-random.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~

Re: [PATCH] util/guest-random: Fix size arg to tail memcpy
Posted by Philippe Mathieu-Daudé 4 years, 7 months ago
On 7/9/21 2:06 PM, Mark Nelson wrote:
> 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")
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> Signed-off-by: Mark Nelson <mdnelson8@gmail.com>
> ---
>  util/guest-random.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/util/guest-random.c b/util/guest-random.c
> index 086115bd67..23643f86cc 100644
> --- a/util/guest-random.c
> +++ b/util/guest-random.c
> @@ -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;
>  }
> 


Re: [PATCH] util/guest-random: Fix size arg to tail memcpy
Posted by Laurent Vivier 4 years, 7 months ago
Le 09/07/2021 à 14:06, Mark Nelson a écrit :
> 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.
> 
> Signed-off-by: Mark Nelson <mdnelson8@gmail.com>
> ---
>  util/guest-random.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/util/guest-random.c b/util/guest-random.c
> index 086115bd67..23643f86cc 100644
> --- a/util/guest-random.c
> +++ b/util/guest-random.c
> @@ -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