[PATCH v1 2/8] migration: Don't use '#' flag of printf format

Bihong Yu posted 8 patches 5 years, 4 months ago
Maintainers: Juan Quintela <quintela@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>, Fam Zheng <fam@euphon.net>
There is a newer version of this series
[PATCH v1 2/8] migration: Don't use '#' flag of printf format
Posted by Bihong Yu 5 years, 4 months ago
Signed-off-by:Bihong Yu <yubihong@huawei.com>
Reviewed-by: Chuan Zheng <zhengchuan@huawei.com>
Signed-off-by: Bihong Yu <yubihong@huawei.com>
---
 migration/block.c | 2 +-
 migration/ram.c   | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/migration/block.c b/migration/block.c
index 4b8576b..399dfb8 100644
--- a/migration/block.c
+++ b/migration/block.c
@@ -998,7 +998,7 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
                    (addr == 100) ? '\n' : '\r');
             fflush(stdout);
         } else if (!(flags & BLK_MIG_FLAG_EOS)) {
-            fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
+            fprintf(stderr, "Unknown block migration flags: %0x\n", flags);
             return -EINVAL;
         }
         ret = qemu_file_get_error(f);
diff --git a/migration/ram.c b/migration/ram.c
index 433489d..59bdd15 100644
--- a/migration/ram.c
+++ b/migration/ram.c
@@ -3298,7 +3298,7 @@ static int ram_load_postcopy(QEMUFile *f)
             multifd_recv_sync_main();
             break;
         default:
-            error_report("Unknown combination of migration flags: %#x"
+            error_report("Unknown combination of migration flags: %0x"
                          " (postcopy mode)", flags);
             ret = -EINVAL;
             break;
@@ -3576,7 +3576,7 @@ static int ram_load_precopy(QEMUFile *f)
             if (flags & RAM_SAVE_FLAG_HOOK) {
                 ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL);
             } else {
-                error_report("Unknown combination of migration flags: %#x",
+                error_report("Unknown combination of migration flags: %0x",
                              flags);
                 ret = -EINVAL;
             }
-- 
1.8.3.1


Re: [PATCH v1 2/8] migration: Don't use '#' flag of printf format
Posted by Peter Maydell 5 years, 4 months ago
On Sun, 11 Oct 2020 at 14:52, Bihong Yu <yubihong@huawei.com> wrote:
> @@ -998,7 +998,7 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
>                     (addr == 100) ? '\n' : '\r');
>              fflush(stdout);
>          } else if (!(flags & BLK_MIG_FLAG_EOS)) {
> -            fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
> +            fprintf(stderr, "Unknown block migration flags: %0x\n", flags);

This doesn't look right. "%#x" will print a number in hex with a leading '0x'.
To get the same effect without using "#" you need "0x%x" (that is,
the format string provides the 0x characters literally).
What you've written is '%0x", which is a format string where the '0' is
a request to print with zero padding (which is ignored since there's no
field width given), so the result is the same as if you'd just said '%x',
and there is no '0x' in the output.

$ cat /tmp/zz9.c
#include <stdio.h>
int main(void) {
  printf("%#x\n", 42);
  printf("%0x\n", 42);
  printf("0x%x\n", 42);
  return 0;
}
$ gcc -g -Wall -o /tmp/zz9 /tmp/zz9.c
$ /tmp/zz9
0x2a
2a
0x2a

>          default:
> -            error_report("Unknown combination of migration flags: %#x"
> +            error_report("Unknown combination of migration flags: %0x"
>                           " (postcopy mode)", flags);
>              ret = -EINVAL;
>              break;
> @@ -3576,7 +3576,7 @@ static int ram_load_precopy(QEMUFile *f)
>              if (flags & RAM_SAVE_FLAG_HOOK) {
>                  ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL);
>              } else {
> -                error_report("Unknown combination of migration flags: %#x",
> +                error_report("Unknown combination of migration flags: %0x",
>                               flags);
>                  ret = -EINVAL;
>              }

These two similarly should be "0x%x".

thanks
-- PMM

Re: [PATCH v1 2/8] migration: Don't use '#' flag of printf format
Posted by Bihong Yu 5 years, 4 months ago
Thank you for your review. OK,I have fixed the problem in v2.

On 2020/10/12 2:19, Peter Maydell wrote:
> On Sun, 11 Oct 2020 at 14:52, Bihong Yu <yubihong@huawei.com> wrote:
>> @@ -998,7 +998,7 @@ static int block_load(QEMUFile *f, void *opaque, int version_id)
>>                     (addr == 100) ? '\n' : '\r');
>>              fflush(stdout);
>>          } else if (!(flags & BLK_MIG_FLAG_EOS)) {
>> -            fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
>> +            fprintf(stderr, "Unknown block migration flags: %0x\n", flags);
> 
> This doesn't look right. "%#x" will print a number in hex with a leading '0x'.
> To get the same effect without using "#" you need "0x%x" (that is,
> the format string provides the 0x characters literally).
> What you've written is '%0x", which is a format string where the '0' is
> a request to print with zero padding (which is ignored since there's no
> field width given), so the result is the same as if you'd just said '%x',
> and there is no '0x' in the output.
> 
> $ cat /tmp/zz9.c
> #include <stdio.h>
> int main(void) {
>   printf("%#x\n", 42);
>   printf("%0x\n", 42);
>   printf("0x%x\n", 42);
>   return 0;
> }
> $ gcc -g -Wall -o /tmp/zz9 /tmp/zz9.c
> $ /tmp/zz9
> 0x2a
> 2a
> 0x2a
> 
>>          default:
>> -            error_report("Unknown combination of migration flags: %#x"
>> +            error_report("Unknown combination of migration flags: %0x"
>>                           " (postcopy mode)", flags);
>>              ret = -EINVAL;
>>              break;
>> @@ -3576,7 +3576,7 @@ static int ram_load_precopy(QEMUFile *f)
>>              if (flags & RAM_SAVE_FLAG_HOOK) {
>>                  ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL);
>>              } else {
>> -                error_report("Unknown combination of migration flags: %#x",
>> +                error_report("Unknown combination of migration flags: %0x",
>>                               flags);
>>                  ret = -EINVAL;
>>              }
> 
> These two similarly should be "0x%x".
> 
> thanks
> -- PMM
> .
>