[Qemu-devel] [PATCH] curses: fix wchar_t printf warning

Gerd Hoffmann posted 1 patch 5 years ago
Test docker-mingw@fedora passed
Test docker-clang@ubuntu passed
Test checkpatch passed
Test asan passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20190402073018.17747-1-kraxel@redhat.com
Maintainers: Gerd Hoffmann <kraxel@redhat.com>
ui/curses.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[Qemu-devel] [PATCH] curses: fix wchar_t printf warning
Posted by Gerd Hoffmann 5 years ago
On some systems wchar_t is "long int", on others just "int".
So go cast to "long int" and adjust the printf format accordingly.

Reported-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 ui/curses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/ui/curses.c b/ui/curses.c
index cc6d6da68463..fb63945188b2 100644
--- a/ui/curses.c
+++ b/ui/curses.c
@@ -453,8 +453,8 @@ static uint16_t get_ucs(wchar_t wch, iconv_t conv)
     swch = sizeof(wch);
 
     if (iconv(conv, &pwch, &swch, &pch, &sch) == (size_t) -1) {
-        fprintf(stderr, "Could not convert 0x%02x from WCHAR_T to UCS-2: %s\n",
-                        wch, strerror(errno));
+        fprintf(stderr, "Could not convert 0x%02lx from WCHAR_T to UCS-2: %s\n",
+                (unsigned long)wch, strerror(errno));
         return 0xFFFD;
     }
 
-- 
2.18.1


Re: [Qemu-devel] [PATCH] curses: fix wchar_t printf warning
Posted by Eric Blake 5 years ago
On 4/2/19 2:30 AM, Gerd Hoffmann wrote:
> On some systems wchar_t is "long int", on others just "int".

And elsewhere, it's 'short'.

> So go cast to "long int" and adjust the printf format accordingly.
> 
> Reported-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  ui/curses.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/ui/curses.c b/ui/curses.c
> index cc6d6da68463..fb63945188b2 100644
> --- a/ui/curses.c
> +++ b/ui/curses.c
> @@ -453,8 +453,8 @@ static uint16_t get_ucs(wchar_t wch, iconv_t conv)
>      swch = sizeof(wch);
>  
>      if (iconv(conv, &pwch, &swch, &pch, &sch) == (size_t) -1) {
> -        fprintf(stderr, "Could not convert 0x%02x from WCHAR_T to UCS-2: %s\n",
> -                        wch, strerror(errno));
> +        fprintf(stderr, "Could not convert 0x%02lx from WCHAR_T to UCS-2: %s\n",
> +                (unsigned long)wch, strerror(errno));

Sadly, I think you are right that a cast is necessary; there is no
reserved printf character for printing wchar_t as an integer, and the
width of wchar_t is unspecified (on 32-bit systems where it is 'long
int', it does not promote to 'int').

You could have also stuck with %02x and (int)wch, for less typing, but
it's not worth the respin.

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

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

Re: [Qemu-devel] [PATCH] curses: fix wchar_t printf warning
Posted by Philippe Mathieu-Daudé 5 years ago
On 4/2/19 2:57 PM, Eric Blake wrote:
> On 4/2/19 2:30 AM, Gerd Hoffmann wrote:
>> On some systems wchar_t is "long int", on others just "int".
> 
> And elsewhere, it's 'short'.
> 
>> So go cast to "long int" and adjust the printf format accordingly.
>>
>> Reported-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
>> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
>> ---
>>  ui/curses.c | 4 ++--
>>  1 file changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/ui/curses.c b/ui/curses.c
>> index cc6d6da68463..fb63945188b2 100644
>> --- a/ui/curses.c
>> +++ b/ui/curses.c
>> @@ -453,8 +453,8 @@ static uint16_t get_ucs(wchar_t wch, iconv_t conv)
>>      swch = sizeof(wch);
>>  
>>      if (iconv(conv, &pwch, &swch, &pch, &sch) == (size_t) -1) {
>> -        fprintf(stderr, "Could not convert 0x%02x from WCHAR_T to UCS-2: %s\n",
>> -                        wch, strerror(errno));
>> +        fprintf(stderr, "Could not convert 0x%02lx from WCHAR_T to UCS-2: %s\n",
>> +                (unsigned long)wch, strerror(errno));
> 
> Sadly, I think you are right that a cast is necessary; there is no
> reserved printf character for printing wchar_t as an integer, and the
> width of wchar_t is unspecified (on 32-bit systems where it is 'long
> int', it does not promote to 'int').
> 
> You could have also stuck with %02x and (int)wch, for less typing, but
> it's not worth the respin.
> 
> Reviewed-by: Eric Blake <eblake@redhat.com>
> 

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Re: [Qemu-devel] [PATCH] curses: fix wchar_t printf warning
Posted by Peter Maydell 5 years ago
On Tue, 2 Apr 2019 at 08:34, Gerd Hoffmann <kraxel@redhat.com> wrote:
>
> On some systems wchar_t is "long int", on others just "int".
> So go cast to "long int" and adjust the printf format accordingly.
>
> Reported-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  ui/curses.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/ui/curses.c b/ui/curses.c
> index cc6d6da68463..fb63945188b2 100644
> --- a/ui/curses.c
> +++ b/ui/curses.c
> @@ -453,8 +453,8 @@ static uint16_t get_ucs(wchar_t wch, iconv_t conv)
>      swch = sizeof(wch);
>
>      if (iconv(conv, &pwch, &swch, &pch, &sch) == (size_t) -1) {
> -        fprintf(stderr, "Could not convert 0x%02x from WCHAR_T to UCS-2: %s\n",
> -                        wch, strerror(errno));
> +        fprintf(stderr, "Could not convert 0x%02lx from WCHAR_T to UCS-2: %s\n",
> +                (unsigned long)wch, strerror(errno));
>          return 0xFFFD;
>      }
>

Applied to master since we needed an rc4 for other reasons.

thanks
-- PMM