[Qemu-devel] [PATCH v3 16/25] tpm: Use size_t to hold sizes

Philippe Mathieu-Daudé posted 25 patches 6 years, 8 months ago
Maintainers: Anthony Perard <anthony.perard@citrix.com>, Li Zhijian <lizhijian@cn.fujitsu.com>, Jason Wang <jasowang@redhat.com>, Cornelia Huck <cohuck@redhat.com>, Amit Shah <amit@kernel.org>, "Michael S. Tsirkin" <mst@redhat.com>, Paul Durrant <paul.durrant@citrix.com>, Halil Pasic <pasic@linux.ibm.com>, Christian Borntraeger <borntraeger@de.ibm.com>, David Gibson <david@gibson.dropbear.id.au>, Zhang Chen <zhangckid@gmail.com>, Corey Minyard <minyard@acm.org>, Gerd Hoffmann <kraxel@redhat.com>, Stefano Stabellini <sstabellini@kernel.org>, Paolo Bonzini <pbonzini@redhat.com>, Stefan Berger <stefanb@linux.ibm.com>, Samuel Thibault <samuel.thibault@ens-lyon.org>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, Pavel Dovgalyuk <pavel.dovgaluk@ispras.ru>
[Qemu-devel] [PATCH v3 16/25] tpm: Use size_t to hold sizes
Posted by Philippe Mathieu-Daudé 6 years, 8 months ago
Avoid to use a signed type to hold an unsigned value.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 hw/tpm/tpm_emulator.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c
index 70f4b10284..931e56f6ed 100644
--- a/hw/tpm/tpm_emulator.c
+++ b/hw/tpm/tpm_emulator.c
@@ -87,17 +87,18 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
 {
     CharBackend *dev = &tpm->ctrl_chr;
     uint32_t cmd_no = cpu_to_be32(cmd);
-    ssize_t n = sizeof(uint32_t) + msg_len_in;
+    size_t sz = sizeof(uint32_t) + msg_len_in;
+    ssize_t n;
     uint8_t *buf = NULL;
     int ret = -1;
 
     qemu_mutex_lock(&tpm->mutex);
 
-    buf = g_alloca(n);
+    buf = g_alloca(sz);
     memcpy(buf, &cmd_no, sizeof(cmd_no));
     memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
 
-    n = qemu_chr_fe_write_all(dev, buf, n);
+    n = qemu_chr_fe_write_all(dev, buf, sz);
     if (n <= 0) {
         goto end;
     }
-- 
2.20.1


Re: [Qemu-devel] [PATCH v3 16/25] tpm: Use size_t to hold sizes
Posted by Marc-André Lureau 6 years, 8 months ago
On Wed, Feb 20, 2019 at 2:06 AM Philippe Mathieu-Daudé
<philmd@redhat.com> wrote:
>
> Avoid to use a signed type to hold an unsigned value.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>


Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

> ---
>  hw/tpm/tpm_emulator.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/hw/tpm/tpm_emulator.c b/hw/tpm/tpm_emulator.c
> index 70f4b10284..931e56f6ed 100644
> --- a/hw/tpm/tpm_emulator.c
> +++ b/hw/tpm/tpm_emulator.c
> @@ -87,17 +87,18 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
>  {
>      CharBackend *dev = &tpm->ctrl_chr;
>      uint32_t cmd_no = cpu_to_be32(cmd);
> -    ssize_t n = sizeof(uint32_t) + msg_len_in;
> +    size_t sz = sizeof(uint32_t) + msg_len_in;
> +    ssize_t n;
>      uint8_t *buf = NULL;
>      int ret = -1;
>
>      qemu_mutex_lock(&tpm->mutex);
>
> -    buf = g_alloca(n);
> +    buf = g_alloca(sz);
>      memcpy(buf, &cmd_no, sizeof(cmd_no));
>      memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
>
> -    n = qemu_chr_fe_write_all(dev, buf, n);
> +    n = qemu_chr_fe_write_all(dev, buf, sz);
>      if (n <= 0) {
>          goto end;
>      }
> --
> 2.20.1
>