qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Vivier <laurent@vivier.eu>
To: pannengyuan@huawei.com, pbonzini@redhat.com, rth@twiddle.net,
	ehabkost@redhat.com
Cc: qemu-trivial@nongnu.org, zhang.zhanghailiang@huawei.com,
	qemu-devel@nongnu.org, euler.robot@huawei.com
Subject: Re: [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings
Date: Tue, 3 Mar 2020 11:14:01 +0100	[thread overview]
Message-ID: <e87fbecb-c6fe-e491-76e8-166e15ef977b@vivier.eu> (raw)
In-Reply-To: <20200224065139.19567-1-pannengyuan@huawei.com>

Le 24/02/2020 à 07:51, pannengyuan@huawei.com a écrit :
> From: Pan Nengyuan <pannengyuan@huawei.com>
> 
> Fix compile warnings:
> /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:124:56: error: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size 3 [-Werror=format-truncation=]
>      snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
>                                                         ^~~~
> /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:124:41: note: directive argument in the range [-2147483648, 64]
>      snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
>                                          ^~~~~~~~~~~~~~~~~~~~
> In file included from /usr/include/stdio.h:873,
>                  from /mnt/sdb/qemu-new/qemu_test/qemu/include/qemu/osdep.h:99,
>                  from /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:14:
> /usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 17 and 26 bytes into a destination of size 17
>    return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
>           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>         __bos (__s), __fmt, __va_arg_pack ());
>         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c: In function ‘hax_vcpu_devfs_string’:
> /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:55: error: ‘%02d’ directive output may be truncated writing between 2 and 11 bytes into a region of size 10 [-Werror=format-truncation=]
>      snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
>                                                        ^~~~
> /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:43: note: directive argument in the range [-2147483648, 64]
>      snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
>                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~
> /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:143:43: note: directive argument in the range [-2147483648, 64]
> In file included from /usr/include/stdio.h:873,
>                  from /mnt/sdb/qemu-new/qemu_test/qemu/include/qemu/osdep.h:99,
>                  from /mnt/sdb/qemu-new/qemu_test/qemu/target/i386/hax-posix.c:14:
> /usr/include/bits/stdio2.h:67:10: note: ‘__builtin___snprintf_chk’ output between 21 and 39 bytes into a destination of size 21
>    return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
>           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>         __bos (__s), __fmt, __va_arg_pack ());
> 
> We know that we have checked the vm_id and vcpu_id in the first(less than 0x40), it will never be truncated in snprintf().
> Thus, this patch add an assertion to clear this false-positive warning.
> 
> Reported-by: Euler Robot <euler.robot@huawei.com>
> Signed-off-by: Pan Nengyuan <pannengyuan@huawei.com>
> ---
>  target/i386/hax-posix.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/target/i386/hax-posix.c b/target/i386/hax-posix.c
> index a5426a6dac..197d5bc0f9 100644
> --- a/target/i386/hax-posix.c
> +++ b/target/i386/hax-posix.c
> @@ -121,7 +121,8 @@ static char *hax_vm_devfs_string(int vm_id)
>          return NULL;
>      }
>  
> -    snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
> +    int len = snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
> +    assert(len < sizeof HAX_VM_DEVFS);
>      return name;
>  }
>  
> @@ -140,8 +141,9 @@ static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id)
>          return NULL;
>      }
>  
> -    snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
> -             vm_id, vcpu_id);
> +    int len = snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
> +                       vm_id, vcpu_id);
> +    assert(len < sizeof HAX_VCPU_DEVFS);
>      return name;
>  }
>  
> 

You should check instead that vm_id and vcpu_id are >= 0 where they are
checked to be <= MAX_VM_ID and MAX_VCPU_ID, this will avoid the overflow
of "%02d" and should remove the compile warning.

Thanks,
Laurent


  reply	other threads:[~2020-03-03 10:19 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-24  6:51 [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings pannengyuan
2020-03-03 10:14 ` Laurent Vivier [this message]
2020-03-03 10:47 ` Paolo Bonzini
2020-03-03 10:58   ` Pan Nengyuan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e87fbecb-c6fe-e491-76e8-166e15ef977b@vivier.eu \
    --to=laurent@vivier.eu \
    --cc=ehabkost@redhat.com \
    --cc=euler.robot@huawei.com \
    --cc=pannengyuan@huawei.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-trivial@nongnu.org \
    --cc=rth@twiddle.net \
    --cc=zhang.zhanghailiang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).