qcow2: Use a GString in report_unsupported_feature()
PATCH v2 1/1 qcow2: Use a GString in report_unsupported_feature()
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.
--- block/qcow2.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
v2: Use g_autoptr and update commit message
block/qcow2.c +11 -12
@@ -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); }/*
Signature · 1 line
2.20.1
Quoted 53 lines · Alberto Garcia
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é <[email protected]>Signed-off-by Alberto Garcia <[email protected]>Reviewed-by Alex Bennée <[email protected]>--- block/qcow2.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)v2: Use
g_autoptrand update commit messageblock/qcow2.c +8
-10@@ -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,", " ); + }
block/qcow2.c static void report_unsupported_feature(Error **errp, Qcow2Feature *table, · line 482
Quoted 10 lines
block/qcow2.c +3
-2+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); }/*
Quoted 6 lines · Alberto Garcia
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.
Quoted 6 lines
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Alberto Garcia <[email protected]>Reviewed-by Alex Bennée <[email protected]>--- block/qcow2.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
Quoted 54 lines
v2: Use
g_autoptrand update commit messageblock/qcow2.c +11
-12@@ -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); }/* Signature · 1 line
2.20.1
Max Reitz
AppliedQuoted 5 lines · Alberto Garcia
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.))
Quoted 6 lines
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Alberto Garcia <[email protected]>Reviewed-by Alex Bennée <[email protected]>--- 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
Quoted 4 lines
block/qcow2.c +0
-0
[...]
block/qcow2.c static void report_unsupported_feature(Error **errp, Qcow2Feature *table, · line 476
Quoted 13 lines
block/qcow2.c +5
-4@@ -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
Quoted 9 lines · Max Reitz
Quoted 5 lines · Alberto Garcia
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.
Quoted 4 lines
((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