migration: Fix possible access out of bounds
cover migration: Fix possible access out of bounds
Fix the issue detected by Coverity in format_time_str() and move the function into the generic utils as suggested.
Fabiano Rosas (2):
migration: Fix postcopy latency distribution formatting computation
cutils: Add time_us_to_str
include/qemu/cutils.h | 1 + migration/migration-hmp-cmds.c | 19 ++----------------- util/cutils.c | 13 +++++++++++++ 3 files changed, 16 insertions(+), 17 deletions(-)
Signature · 1 line
2.35.3
PATCH 1/2 migration: Fix postcopy latency distribution formatting computation
Coverity has caught a bug in the formatting of time intervals for postcopy latency distribution display in 'info migrate'.
While bounds checking the labels array, sizeof is incorrectly being used. ARRAY_SIZE is the correct form of obtaining the size of an array.
Resolves: Coverity CID 1612248
--- migration/migration-hmp-cmds.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
migration/migration-hmp-cmds.c +2 -4
@@ -57,11 +57,9 @@ static const gchar *format_time_str(uint64_t us)const char *units[] = {"us" ,"ms" ,"sec" };int index =0 ; -while (us >1000 ) { +while (us >1000 && index +1 <ARRAY_SIZE (units)) { us /=1000 ; -if (++index >= (sizeof (units) -1 )) { -break ; - } + index++; }return g_strdup_printf ("%" PRIu64" %s" , us, units[index]);
Signature · 1 line
2.35.3
Re: [PATCH 1/2] migration: Fix postcopy latency distribution formatting computation
Quoted 7 lines · Fabiano Rosas
Coverity has caught a bug in the formatting of time intervals for postcopy latency distribution display in 'info migrate'.
While bounds checking the labels array, sizeof is incorrectly being used.
ARRAY_SIZEis the correct form of obtaining the size of an array.
Quoted 7 lines
Fixes 3345fb3b6d ("migration/postcopy: Add latency distribution report for blocktime")Resolves: Coverity CID 1612248
Suggested-by Peter Maydell <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>--- migration/migration-hmp-cmds.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
Re: [PATCH 1/2] migration: Fix postcopy latency distribution formatting computation
In reply to Fabiano RosasQuoted 4 lines · Fabiano Rosas
migration/migration-hmp-cmds.c +0
-0@@ -57,11 +57,9 @@ static const gchar *format_time_str(uint64_t us)const char *units[] = {"us" ,"ms" ,"sec" };int index =0 ;
migration/migration-hmp-cmds.c static const gchar *format_time_str(uint64_t us) · line 65
Quoted 10 lines
migration/migration-hmp-cmds.c +2
-4-while (us >1000 ) { +while (us >1000 && index +1 <ARRAY_SIZE (units)) { us /=1000 ; -if (++index >= (sizeof (units) -1 )) { -break ; - } + index++; }return g_strdup_printf ("%" PRIu64" %s" , us, units[index]);
* This loop is rather confusing.
* Is the while loop converting microseconds (us) to seconds with: us /= 1000 ? ie. index shall mostly be 2 = "sec", except for the range = 1000000 - 1000999, when us / 1000 => 1000 would break the while loop and it'd return string "1000 ms".
===
#define MS (1000) #define US (MS * 1000) #define NS (US * 1000)
if (n >= NS)
n /= NS;
else if (n >= US)
n /= US;
else if (n >= MS)
n /= MS;
return g_strdup_printf("%"PRIu64" sec", n);===
* Does the above simplification look right? It shall always return seconds as: "<n> sec"
Thank you.
---
- Prasad
Re: [PATCH 1/2] migration: Fix postcopy latency distribution formatting computation
Quoted 14 lines · Prasad Pandit
Quoted 13 lines · Fabiano Rosas
migration/migration-hmp-cmds.c +2
-4@@ -57,11 +57,9 @@ static const gchar *format_time_str(uint64_t us)const char *units[] = {"us" ,"ms" ,"sec" };int index =0 ; -while (us >1000 ) { +while (us >1000 && index +1 <ARRAY_SIZE (units)) { us /=1000 ; -if (++index >= (sizeof (units) -1 )) { -break ; - } + index++; }
Quoted 8 lines
migration/migration-hmp-cmds.c static const gchar *format_time_str(uint64_t us) · line 65
Quoted 1 line
migration/migration-hmp-cmds.c +0
-0return g_strdup_printf ("%" PRIu64" %s" , us, units[index]);* This loop is rather confusing.
* Is the while loop converting microseconds (us) to seconds with: us /= 1000 ? ie. index shall mostly be 2 = "sec", except for the range = 1000000 - 1000999, when us / 1000 => 1000 would break the while loop and it'd return string "1000 ms".
Good catch. The condition should be >=.
Quoted 13 lines
===
#define MS (1000) #define US (MS * 1000) #define NS (US * 1000)if (n >= NS) n /= NS; else if (n >= US) n /= US; else if (n >= MS) n /= MS; return g_strdup_printf("%"PRIu64" sec", n);
Quoted 5 lines
===
* Does the above simplification look right? It shall always return seconds as: "<n> sec"
But then that's "0 sec" for 1000000 us.
Quoted 4 lines
Thank you.
---
- Prasad
Re: [PATCH 1/2] migration: Fix postcopy latency distribution formatting computation
Quoted 1 line · Fabiano Rosas
The condition should be >=.
* I'm thinking of doing away with the loop and the string array. The array has 3 values of which only one gets used due to conversion to seconds.
Quoted 1 line
But then that's "0 sec" for 1000000 us.
#define US (MS * 1000) => 1000000
When us = 1000000, us / US should return "1 sec", no?
Thank you.
---
- Prasad
Re: [PATCH 1/2] migration: Fix postcopy latency distribution formatting computation
Quoted 6 lines · Prasad Pandit
Quoted 1 line · Fabiano Rosas
The condition should be >=.
* I'm thinking of doing away with the loop and the string array. The array has 3 values of which only one gets used due to conversion to seconds.
The point is not to convert to seconds, but to fit the number of microseconds in a range. Take a look at the output of 'info migrate -a' with postcopy-blocktime capability enabled.
Quoted 7 lines
Quoted 2 lines
But then that's "0 sec" for 1000000 us.
#define US (MS * 1000) => 1000000
When us = 1000000, us / US should return "1 sec", no?
Sorry, I meant when the number of microseconds is smaller than a second.
Quoted 3 lines
Thank you.
---
- Prasad
Re: [PATCH 1/2] migration: Fix postcopy latency distribution formatting computation
In reply to Fabiano RosasQuoted 23 lines · Fabiano Rosas
Coverity has caught a bug in the formatting of time intervals for postcopy latency distribution display in 'info migrate'.
While bounds checking the labels array, sizeof is incorrectly being used.
ARRAY_SIZEis the correct form of obtaining the size of an array.Fixes 3345fb3b6d ("migration/postcopy: Add latency distribution report for blocktime")Resolves: Coverity CID 1612248
Suggested-by Peter Maydell <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>--- migration/migration-hmp-cmds.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)migration/migration-hmp-cmds.c +0
-0@@ -57,11 +57,9 @@ static const gchar *format_time_str(uint64_t us)const char *units[] = {"us" ,"ms" ,"sec" };int index =0 ;
migration/migration-hmp-cmds.c static const gchar *format_time_str(uint64_t us) · line 65
Quoted 10 lines
migration/migration-hmp-cmds.c +2
-4-while (us >1000 ) { +while (us >1000 && index +1 <ARRAY_SIZE (units)) { us /=1000 ; -if (++index >= (sizeof (units) -1 )) { -break ; - } + index++; }return g_strdup_printf ("%" PRIu64" %s" , us, units[index]);
It occurrs to me that this is the same basic algorithmic problem as converting storage sizes from bytes to KB/MB/GB/etc.
We have size_to_str() which does this conversion without needing any loop at all.
Then there is freq_to_str() which has similar purpose but still uses a loop, instead of the shortcut size_to_str has.
IMHO we should have a 'scaled_int_to_str' method that is common to any place we need such scaled integer string conversions, and then wrappers like "size_to_str", "freq_to_str" and "duration_to_str" that pass in the required list of unit strings, and the divisor and requested decimal precision, etc.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
PATCH 2/2 cutils: Add time_us_to_str
In reply to Fabiano RosasThe migration code has a function that converts a time value (us) to a string with the proper suffix. Move it to cutils since it's generic enough that it could be reused.
--- include/qemu/cutils.h | 1 + migration/migration-hmp-cmds.c | 17 ++--------------- util/cutils.c | 13 +++++++++++++ 3 files changed, 16 insertions(+), 15 deletions(-)
include/qemu/cutils.h +1 -0
@@ -171,6 +171,7 @@ int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);int qemu_strtosz_metric (const char *nptr,const char **end,uint64_t *result);char *size_to_str (uint64_t val); +char *time_us_to_str (uint64_t val);/** * freq_to_str:
migration/migration-hmp-cmds.c +2 -15
@@ -52,19 +52,6 @@ static void migration_global_dump(Monitor *mon) ms->clear_bitmap_shift); } -static const gchar *format_time_str (uint64_t us) -{ -const char *units[] = {"us" ,"ms" ,"sec" }; -int index =0 ; - -while (us >1000 && index +1 <ARRAY_SIZE (units)) { - us /=1000 ; - index++; - } - -return g_strdup_printf ("%" PRIu64" %s" , us, units[index]); -} -static void migration_dump_blocktime (Monitor *mon,MigrationInfo *info) {if (info->has_postcopy_blocktime) {
@@ -121,8 +108,8 @@ static void migration_dump_blocktime(Monitor *mon, MigrationInfo *info)monitor_printf (mon,"Postcopy Latency Distribution:\n" );while (item) { - g_autofree const gchar *from = format_time_str(1UL << count); - g_autofree const gchar *to = format_time_str(1UL << (count + 1)); + g_autofree const gchar *from = time_us_to_str(1UL << count); + g_autofree const gchar *to = time_us_to_str(1UL << (count + 1));monitor_printf (mon," [ %8s - %8s ]: %10" PRIu64"\n" , from, to, item->value);
util/cutils.c +13 -0
@@ -1004,6 +1004,19 @@ char *freq_to_str(uint64_t freq_hz)return g_strdup_printf ("%0.3g %sHz" , freq,si_prefix (exp10)); } +char *time_us_to_str (uint64_t us) +{ +const char *units[] = {"us" ,"ms" ,"sec" }; +int index =0 ; + +while (us >1000 && index +1 <ARRAY_SIZE (units)) { + us /=1000 ; + index++; + } + +return g_strdup_printf ("%" PRIu64" %s" , us, units[index]); +} +int qemu_pstrcmp0 (const char **str1,const char **str2) {return g_strcmp0 (*str1, *str2);
Signature · 1 line
2.35.3
Re: [PATCH 2/2] cutils: Add time_us_to_str
Quoted 17 lines · Fabiano Rosas
The migration code has a function that converts a time value (us) to a string with the proper suffix. Move it to cutils since it's generic enough that it could be reused.
Suggested-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Fabiano Rosas <[email protected]>--- include/qemu/cutils.h | 1 + migration/migration-hmp-cmds.c | 17 ++--------------- util/cutils.c | 13 +++++++++++++ 3 files changed, 16 insertions(+), 15 deletions(-)include/qemu/cutils.h +0
-0
include/qemu/cutils.h int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result); · line 174
Quoted 4 lines
include/qemu/cutils.h +1
-0@@ -171,6 +171,7 @@ int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result);int qemu_strtosz_metric (const char *nptr,const char **end,uint64_t *result);char *size_to_str (uint64_t val); +char *time_us_to_str (uint64_t val);
s/val/us/
Quoted 43 lines
include/qemu/cutils.h +0
-0/** * freq_to_str: migration/migration-hmp-cmds.c +2
-15@@ -52,19 +52,6 @@ static void migration_global_dump(Monitor *mon) ms->clear_bitmap_shift); } -static const gchar *format_time_str (uint64_t us) -{ -const char *units[] = {"us" ,"ms" ,"sec" }; -int index =0 ; - -while (us >1000 && index +1 <ARRAY_SIZE (units)) { - us /=1000 ; - index++; - } - -return g_strdup_printf ("%" PRIu64" %s" , us, units[index]); -} -static void migration_dump_blocktime (Monitor *mon,MigrationInfo *info) {if (info->has_postcopy_blocktime) {@@ -121,8 +108,8 @@ static void migration_dump_blocktime(Monitor *mon, MigrationInfo *info)monitor_printf (mon,"Postcopy Latency Distribution:\n" );while (item) { - g_autofree const gchar *from = format_time_str(1UL << count); - g_autofree const gchar *to = format_time_str(1UL << (count + 1)); + g_autofree const gchar *from = time_us_to_str(1UL << count); + g_autofree const gchar *to = time_us_to_str(1UL << (count + 1));monitor_printf (mon," [ %8s - %8s ]: %10" PRIu64"\n" , from, to, item->value);util/cutils.c +0
-0
util/cutils.c char *freq_to_str(uint64_t freq_hz) · line 1009
Quoted 6 lines
util/cutils.c +3
-0@@ -1004,6 +1004,19 @@ char *freq_to_str(uint64_t freq_hz)return g_strdup_printf ("%0.3g %sHz" , freq,si_prefix (exp10)); } +char *time_us_to_str (uint64_t us) +{ +const char *units[] = {"us" ,"ms" ,"sec" };
(static)
util/cutils.c char *freq_to_str(uint64_t freq_hz) · line 1018
Quoted 9 lines
util/cutils.c +9
-0+int index =0 ; + +while (us >1000 && index +1 <ARRAY_SIZE (units)) { + us /=1000 ; + index++; + } + +return g_strdup_printf ("%" PRIu64" %s" , us, units[index]); +}
Thanks!