diff mbox series

[v2] qcow2: Use a GString in report_unsupported_feature()

Message ID 20200115135626.19442-1-berto@igalia.com (mailing list archive)
State New, archived
Headers show
Series [v2] qcow2: Use a GString in report_unsupported_feature() | expand

Commit Message

Alberto Garcia Jan. 15, 2020, 1:56 p.m. UTC
This is a bit more efficient than having to allocate and free memory
for each item.

The default size (60) is enough for all the existing incompatible
features or the "Unknown incompatible feature" message.

Suggested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Alberto Garcia <berto@igalia.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
---
 block/qcow2.c | 23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

v2: Use g_autoptr and update commit message

Comments

Philippe Mathieu-Daudé Jan. 15, 2020, 2:23 p.m. UTC | #1
On 1/15/20 2:56 PM, Alberto Garcia wrote:
> This is a bit more efficient than having to allocate and free memory
> for each item.
> 
> The default size (60) is enough for all the existing incompatible
> features or the "Unknown incompatible feature" message.
> 
> Suggested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>   block/qcow2.c | 23 +++++++++++------------
>   1 file changed, 11 insertions(+), 12 deletions(-)
> 
> v2: Use g_autoptr and update commit message
> 
> diff --git a/block/qcow2.c b/block/qcow2.c
> index cef9d72b3a..e29fc07068 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -453,16 +453,15 @@ static void cleanup_unknown_header_ext(BlockDriverState *bs)
>   static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
>                                          uint64_t mask)
>   {
> -    char *features = g_strdup("");
> -    char *old;
> +    g_autoptr(GString) features = g_string_sized_new(60);
>   
>       while (table && table->name[0] != '\0') {
>           if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
>               if (mask & (1ULL << table->bit)) {
> -                old = features;
> -                features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
> -                                           table->name);
> -                g_free(old);
> +                if (features->len > 0) {
> +                    g_string_append(features, ", ");
> +                }
> +                g_string_append_printf(features, "%.46s", table->name);
>                   mask &= ~(1ULL << table->bit);
>               }
>           }
> @@ -470,14 +469,14 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
>       }
>   
>       if (mask) {
> -        old = features;
> -        features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
> -                                   old, *old ? ", " : "", mask);
> -        g_free(old);
> +        if (features->len > 0) {
> +            g_string_append(features, ", ");
> +        }
> +        g_string_append_printf(features,
> +                               "Unknown incompatible feature: %" PRIx64, mask);
>       }
>   
> -    error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
> -    g_free(features);
> +    error_setg(errp, "Unsupported qcow2 feature(s): %s", features->str);
>   }
>   
>   /*
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Stefano Garzarella Jan. 16, 2020, 10:40 a.m. UTC | #2
On Wed, Jan 15, 2020 at 02:56:26PM +0100, Alberto Garcia wrote:
> This is a bit more efficient than having to allocate and free memory
> for each item.
> 
> The default size (60) is enough for all the existing incompatible
> features or the "Unknown incompatible feature" message.
> 
> Suggested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  block/qcow2.c | 23 +++++++++++------------
>  1 file changed, 11 insertions(+), 12 deletions(-)

Reviewed-by: Stefano Garzarella <sgarzare@redhat.com>

> 
> v2: Use g_autoptr and update commit message
> 
> diff --git a/block/qcow2.c b/block/qcow2.c
> index cef9d72b3a..e29fc07068 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -453,16 +453,15 @@ static void cleanup_unknown_header_ext(BlockDriverState *bs)
>  static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
>                                         uint64_t mask)
>  {
> -    char *features = g_strdup("");
> -    char *old;
> +    g_autoptr(GString) features = g_string_sized_new(60);
>  
>      while (table && table->name[0] != '\0') {
>          if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
>              if (mask & (1ULL << table->bit)) {
> -                old = features;
> -                features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
> -                                           table->name);
> -                g_free(old);
> +                if (features->len > 0) {
> +                    g_string_append(features, ", ");
> +                }
> +                g_string_append_printf(features, "%.46s", table->name);
>                  mask &= ~(1ULL << table->bit);
>              }
>          }
> @@ -470,14 +469,14 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
>      }
>  
>      if (mask) {
> -        old = features;
> -        features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
> -                                   old, *old ? ", " : "", mask);
> -        g_free(old);
> +        if (features->len > 0) {
> +            g_string_append(features, ", ");
> +        }
> +        g_string_append_printf(features,
> +                               "Unknown incompatible feature: %" PRIx64, mask);
>      }
>  
> -    error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
> -    g_free(features);
> +    error_setg(errp, "Unsupported qcow2 feature(s): %s", features->str);
>  }
>  
>  /*
> -- 
> 2.20.1
> 
>
Max Reitz Jan. 16, 2020, 12:19 p.m. UTC | #3
On 15.01.20 14:56, Alberto Garcia wrote:
> This is a bit more efficient than having to allocate and free memory
> for each item.
> 
> The default size (60) is enough for all the existing incompatible
> features or the "Unknown incompatible feature" message.

That doesn’t make sense to me.  The existing incompatible features are
known to qemu, and as such will never be printed here.

But just because it doesn’t make sense doesn’t make it wrong.  I suppose
we can assume that if new features are added, they will have a similar
length like the ones we have now.

(Well, it does make “60” a weirdly specific number, but whatever.)

((I’d just have created an empty string.  Considering this is part of
the explanation for a fatal error, nobody cares whether this function
takes a couple of microseconds more.  Or maybe a length of 47, so it can
definitely hold exactly one incompatible feature name.))

> Suggested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>  block/qcow2.c | 23 +++++++++++------------
>  1 file changed, 11 insertions(+), 12 deletions(-)

Thanks, applied to my block branch:

https://git.xanclic.moe/XanClic/qemu/commits/branch/block

> diff --git a/block/qcow2.c b/block/qcow2.c
> index cef9d72b3a..e29fc07068 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c

[...]

> @@ -470,14 +469,14 @@ static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
>      }
>  
>      if (mask) {
> -        old = features;
> -        features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
> -                                   old, *old ? ", " : "", mask);
> -        g_free(old);
> +        if (features->len > 0) {
> +            g_string_append(features, ", ");
> +        }
> +        g_string_append_printf(features,
> +                               "Unknown incompatible feature: %" PRIx64, mask);

Existing problem: This can lead to stuff like

“Unsupported qcow2 feature(s): feat1, feat2, Unknown incompatible
feature: 0xec0”

(i.e., singular “feature” when there are multiple unknown features, and
capitalization of “Unknown”)

O:-)

But whatever.  It’s unlikely there’s going to be more than one
incompatible feature, and it’s extremely unlikely it won’t have a string
description.

Max
Alberto Garcia Jan. 16, 2020, 12:27 p.m. UTC | #4
On Thu 16 Jan 2020 01:19:34 PM CET, Max Reitz wrote:
> On 15.01.20 14:56, Alberto Garcia wrote:
>> This is a bit more efficient than having to allocate and free memory
>> for each item.
>> 
>> The default size (60) is enough for all the existing incompatible
>> features or the "Unknown incompatible feature" message.
>
> That doesn’t make sense to me.  The existing incompatible features are
> known to qemu, and as such will never be printed here.

I know, it's sort of an arbitrary number, I just chose a default size
that is big enough for the strings we have.

> ((I’d just have created an empty string.  Considering this is part of
> the explanation for a fatal error, nobody cares whether this function
> takes a couple of microseconds more.  Or maybe a length of 47, so it
> can definitely hold exactly one incompatible feature name.))

That would be fine with me as well, I don't think it matters so much
either way. Feel free to change the default size or remove the
explanation from the commit message if you want.

Berto
diff mbox series

Patch

diff --git a/block/qcow2.c b/block/qcow2.c
index cef9d72b3a..e29fc07068 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -453,16 +453,15 @@  static void cleanup_unknown_header_ext(BlockDriverState *bs)
 static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
                                        uint64_t mask)
 {
-    char *features = g_strdup("");
-    char *old;
+    g_autoptr(GString) features = g_string_sized_new(60);
 
     while (table && table->name[0] != '\0') {
         if (table->type == QCOW2_FEAT_TYPE_INCOMPATIBLE) {
             if (mask & (1ULL << table->bit)) {
-                old = features;
-                features = g_strdup_printf("%s%s%.46s", old, *old ? ", " : "",
-                                           table->name);
-                g_free(old);
+                if (features->len > 0) {
+                    g_string_append(features, ", ");
+                }
+                g_string_append_printf(features, "%.46s", table->name);
                 mask &= ~(1ULL << table->bit);
             }
         }
@@ -470,14 +469,14 @@  static void report_unsupported_feature(Error **errp, Qcow2Feature *table,
     }
 
     if (mask) {
-        old = features;
-        features = g_strdup_printf("%s%sUnknown incompatible feature: %" PRIx64,
-                                   old, *old ? ", " : "", mask);
-        g_free(old);
+        if (features->len > 0) {
+            g_string_append(features, ", ");
+        }
+        g_string_append_printf(features,
+                               "Unknown incompatible feature: %" PRIx64, mask);
     }
 
-    error_setg(errp, "Unsupported qcow2 feature(s): %s", features);
-    g_free(features);
+    error_setg(errp, "Unsupported qcow2 feature(s): %s", features->str);
 }
 
 /*