qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7
@ 2017-07-17 15:12 Max Reitz
  2017-07-17 15:22 ` Eric Blake
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Max Reitz @ 2017-07-17 15:12 UTC (permalink / raw)
  To: qemu-block; +Cc: qemu-devel, Max Reitz, Kevin Wolf, Hervé Poussineau

gcc 7 complains that the sprintf() might write a null byte beyond the
end of the tail buffer.  That is wrong, but we can silence it by making
i unsigned (it can never be negative anyway, see the if condition right
before).  For some reason, this allows gcc to suddenly accurately
calculate the range of i so we can give the tail[] array the exact size
it needs to have (which is 8 bytes) without gcc complaining.

In addition, let us convert the sprintf() to snprintf(), because that is
always nicer, and add an assertion about the range of the return value
afterwards so we can see that "8 - len" will never be negative and thus
"entry->name + MIN(j, 8 - len)" will never be out of bounds.

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/vvfat.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/block/vvfat.c b/block/vvfat.c
index 6b11596..a9e207f 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -549,7 +549,7 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
     const gchar *p, *last_dot = NULL;
     gunichar c;
     bool lossy_conversion = false;
-    char tail[11];
+    char tail[8];
 
     if (!entry) {
         return NULL;
@@ -614,7 +614,8 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
     for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
         direntry_t *entry1;
         if (i > 0) {
-            int len = sprintf(tail, "~%d", i);
+            int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
+            assert(len <= 7);
             memcpy(entry->name + MIN(j, 8 - len), tail, len);
         }
         for (entry1 = array_get(&(s->directory), directory_start);
-- 
2.9.4

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

* Re: [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7
  2017-07-17 15:12 [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7 Max Reitz
@ 2017-07-17 15:22 ` Eric Blake
  2017-07-17 15:57 ` Kevin Wolf
  2017-07-17 18:40 ` Hervé Poussineau
  2 siblings, 0 replies; 5+ messages in thread
From: Eric Blake @ 2017-07-17 15:22 UTC (permalink / raw)
  To: Max Reitz, qemu-block; +Cc: Kevin Wolf, Hervé Poussineau, qemu-devel

[-- Attachment #1: Type: text/plain, Size: 1084 bytes --]

On 07/17/2017 10:12 AM, Max Reitz wrote:
> gcc 7 complains that the sprintf() might write a null byte beyond the
> end of the tail buffer.  That is wrong, but we can silence it by making
> i unsigned (it can never be negative anyway, see the if condition right
> before).  For some reason, this allows gcc to suddenly accurately
> calculate the range of i so we can give the tail[] array the exact size
> it needs to have (which is 8 bytes) without gcc complaining.
> 
> In addition, let us convert the sprintf() to snprintf(), because that is
> always nicer, and add an assertion about the range of the return value
> afterwards so we can see that "8 - len" will never be negative and thus
> "entry->name + MIN(j, 8 - len)" will never be out of bounds.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/vvfat.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]

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

* Re: [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7
  2017-07-17 15:12 [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7 Max Reitz
  2017-07-17 15:22 ` Eric Blake
@ 2017-07-17 15:57 ` Kevin Wolf
  2017-07-17 18:40 ` Hervé Poussineau
  2 siblings, 0 replies; 5+ messages in thread
From: Kevin Wolf @ 2017-07-17 15:57 UTC (permalink / raw)
  To: Max Reitz; +Cc: qemu-block, qemu-devel, Hervé Poussineau

Am 17.07.2017 um 17:12 hat Max Reitz geschrieben:
> gcc 7 complains that the sprintf() might write a null byte beyond the
> end of the tail buffer.  That is wrong, but we can silence it by making
> i unsigned (it can never be negative anyway, see the if condition right
> before).  For some reason, this allows gcc to suddenly accurately
> calculate the range of i so we can give the tail[] array the exact size
> it needs to have (which is 8 bytes) without gcc complaining.
> 
> In addition, let us convert the sprintf() to snprintf(), because that is
> always nicer, and add an assertion about the range of the return value
> afterwards so we can see that "8 - len" will never be negative and thus
> "entry->name + MIN(j, 8 - len)" will never be out of bounds.
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>

Thanks, applied to the block branch.

Kevin

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

* Re: [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7
  2017-07-17 15:12 [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7 Max Reitz
  2017-07-17 15:22 ` Eric Blake
  2017-07-17 15:57 ` Kevin Wolf
@ 2017-07-17 18:40 ` Hervé Poussineau
  2017-07-17 19:12   ` Max Reitz
  2 siblings, 1 reply; 5+ messages in thread
From: Hervé Poussineau @ 2017-07-17 18:40 UTC (permalink / raw)
  To: Max Reitz, qemu-block; +Cc: qemu-devel, Kevin Wolf

Le 17/07/2017 à 17:12, Max Reitz a écrit :
> gcc 7 complains that the sprintf() might write a null byte beyond the
> end of the tail buffer.  That is wrong, but we can silence it by making
> i unsigned (it can never be negative anyway, see the if condition right
> before).  For some reason, this allows gcc to suddenly accurately
> calculate the range of i so we can give the tail[] array the exact size
> it needs to have (which is 8 bytes) without gcc complaining.
>
> In addition, let us convert the sprintf() to snprintf(), because that is
> always nicer, and add an assertion about the range of the return value
> afterwards so we can see that "8 - len" will never be negative and thus
> "entry->name + MIN(j, 8 - len)" will never be out of bounds.
>
> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  block/vvfat.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/block/vvfat.c b/block/vvfat.c
> index 6b11596..a9e207f 100644
> --- a/block/vvfat.c
> +++ b/block/vvfat.c
> @@ -549,7 +549,7 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
>      const gchar *p, *last_dot = NULL;
>      gunichar c;
>      bool lossy_conversion = false;
> -    char tail[11];
> +    char tail[8];
>
>      if (!entry) {
>          return NULL;
> @@ -614,7 +614,8 @@ static direntry_t *create_short_filename(BDRVVVFATState *s,
>      for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
>          direntry_t *entry1;
>          if (i > 0) {
> -            int len = sprintf(tail, "~%d", i);
> +            int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
> +            assert(len <= 7);

As i is on minimum between 0 or 1 and on maximum equal at 999999, does it work if you change the type of i from int to unsigned int?
That way, you probably won't need the cast to unsigned in the s(n)printf.

>              memcpy(entry->name + MIN(j, 8 - len), tail, len);
>          }
>          for (entry1 = array_get(&(s->directory), directory_start);
>

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

* Re: [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7
  2017-07-17 18:40 ` Hervé Poussineau
@ 2017-07-17 19:12   ` Max Reitz
  0 siblings, 0 replies; 5+ messages in thread
From: Max Reitz @ 2017-07-17 19:12 UTC (permalink / raw)
  To: Hervé Poussineau, qemu-block; +Cc: qemu-devel, Kevin Wolf

[-- Attachment #1: Type: text/plain, Size: 2210 bytes --]

On 2017-07-17 20:40, Hervé Poussineau wrote:
> Le 17/07/2017 à 17:12, Max Reitz a écrit :
>> gcc 7 complains that the sprintf() might write a null byte beyond the
>> end of the tail buffer.  That is wrong, but we can silence it by making
>> i unsigned (it can never be negative anyway, see the if condition right
>> before).  For some reason, this allows gcc to suddenly accurately
>> calculate the range of i so we can give the tail[] array the exact size
>> it needs to have (which is 8 bytes) without gcc complaining.
>>
>> In addition, let us convert the sprintf() to snprintf(), because that is
>> always nicer, and add an assertion about the range of the return value
>> afterwards so we can see that "8 - len" will never be negative and thus
>> "entry->name + MIN(j, 8 - len)" will never be out of bounds.
>>
>> Signed-off-by: Max Reitz <mreitz@redhat.com>
>> ---
>>  block/vvfat.c | 5 +++--
>>  1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/block/vvfat.c b/block/vvfat.c
>> index 6b11596..a9e207f 100644
>> --- a/block/vvfat.c
>> +++ b/block/vvfat.c
>> @@ -549,7 +549,7 @@ static direntry_t
>> *create_short_filename(BDRVVVFATState *s,
>>      const gchar *p, *last_dot = NULL;
>>      gunichar c;
>>      bool lossy_conversion = false;
>> -    char tail[11];
>> +    char tail[8];
>>
>>      if (!entry) {
>>          return NULL;
>> @@ -614,7 +614,8 @@ static direntry_t
>> *create_short_filename(BDRVVVFATState *s,
>>      for (i = lossy_conversion ? 1 : 0; i < 999999; i++) {
>>          direntry_t *entry1;
>>          if (i > 0) {
>> -            int len = sprintf(tail, "~%d", i);
>> +            int len = snprintf(tail, sizeof(tail), "~%u", (unsigned)i);
>> +            assert(len <= 7);
> 
> As i is on minimum between 0 or 1 and on maximum equal at 999999, does
> it work if you change the type of i from int to unsigned int?
> That way, you probably won't need the cast to unsigned in the s(n)printf.

Hm... It works in a way, but then gcc likes to think tail[] needs to be
9 bytes long (for whatever reason). So... It works in a sense, but not
quite as well.

So I'm not quite sure which way is better. :-)

Max


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 498 bytes --]

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

end of thread, other threads:[~2017-07-17 19:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-07-17 15:12 [Qemu-devel] [PATCH] block/vvfat: Fix compiler warning with gcc 7 Max Reitz
2017-07-17 15:22 ` Eric Blake
2017-07-17 15:57 ` Kevin Wolf
2017-07-17 18:40 ` Hervé Poussineau
2017-07-17 19:12   ` Max Reitz

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