qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings
@ 2020-02-24  6:51 pannengyuan
  2020-03-03 10:14 ` Laurent Vivier
  2020-03-03 10:47 ` Paolo Bonzini
  0 siblings, 2 replies; 4+ messages in thread
From: pannengyuan @ 2020-02-24  6:51 UTC (permalink / raw)
  To: pbonzini, rth, ehabkost
  Cc: qemu-trivial, euler.robot, Pan Nengyuan, qemu-devel, zhang.zhanghailiang

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;
 }
 
-- 
2.18.2



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings
  2020-02-24  6:51 [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings pannengyuan
@ 2020-03-03 10:14 ` Laurent Vivier
  2020-03-03 10:47 ` Paolo Bonzini
  1 sibling, 0 replies; 4+ messages in thread
From: Laurent Vivier @ 2020-03-03 10:14 UTC (permalink / raw)
  To: pannengyuan, pbonzini, rth, ehabkost
  Cc: qemu-trivial, zhang.zhanghailiang, qemu-devel, euler.robot

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings
  2020-02-24  6:51 [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings pannengyuan
  2020-03-03 10:14 ` Laurent Vivier
@ 2020-03-03 10:47 ` Paolo Bonzini
  2020-03-03 10:58   ` Pan Nengyuan
  1 sibling, 1 reply; 4+ messages in thread
From: Paolo Bonzini @ 2020-03-03 10:47 UTC (permalink / raw)
  To: pannengyuan, rth, ehabkost
  Cc: qemu-trivial, euler.robot, qemu-devel, zhang.zhanghailiang

On 24/02/20 07:51, pannengyuan@huawei.com wrote:
> 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;
>  }
>  
> 

Julio Faracco has posted a fix for the same bug.  The best change is
actually to switch to g_strdup_printf.

Paolo



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings
  2020-03-03 10:47 ` Paolo Bonzini
@ 2020-03-03 10:58   ` Pan Nengyuan
  0 siblings, 0 replies; 4+ messages in thread
From: Pan Nengyuan @ 2020-03-03 10:58 UTC (permalink / raw)
  To: Paolo Bonzini, rth, ehabkost
  Cc: qemu-trivial, euler.robot, qemu-devel, zhang.zhanghailiang



On 3/3/2020 6:47 PM, Paolo Bonzini wrote:
> On 24/02/20 07:51, pannengyuan@huawei.com wrote:
>> 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;
>>  }
>>  
>>
> 
> Julio Faracco has posted a fix for the same bug.  The best change is
> actually to switch to g_strdup_printf.

Okay, Thanks.

> 
> Paolo
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-03-03 10:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-24  6:51 [PATCH] target/i386/hax-posix: fix two 'format-truncation' compile warnings pannengyuan
2020-03-03 10:14 ` Laurent Vivier
2020-03-03 10:47 ` Paolo Bonzini
2020-03-03 10:58   ` Pan Nengyuan

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).