Skip to contentSkip to conversation

curses: fix wchar_t printf warning

PATCH Applied Gerd Hoffmann 4 participants 4 messages Apr 2, 2019 – Apr 12, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 curses: fix wchar_t printf warning

On some systems wchar_t is "long int", on others just "int". So go cast to "long int" and adjust the printf format accordingly.

---
 ui/curses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/curses.c +2 -2
index cc6d6da68463..fb63945188b2 100644
@@ -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;
     }
 
Signature · 1 line
2.18.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Gerd Hoffmann

On some systems wchar_t is "long int", on others just "int".

And elsewhere, it's 'short'.

Quoted 13 lines

So go cast to "long int" and adjust the printf format accordingly.

---
 ui/curses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/curses.c +0 -0
index cc6d6da68463..fb63945188b2 100644

ui/curses.c static uint16_t get_ucs(wchar_t wch, iconv_t conv) · line 457

Quoted 7 lines
ui/curses.c +2 -2
@@ -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.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 31 lines · Eric Blake
Quoted 1 line · Gerd Hoffmann

On some systems wchar_t is "long int", on others just "int".

And elsewhere, it's 'short'.

Quoted 13 lines

So go cast to "long int" and adjust the printf format accordingly.

---
 ui/curses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/curses.c +0 -0
index cc6d6da68463..fb63945188b2 100644

ui/curses.c static uint16_t get_ucs(wchar_t wch, iconv_t conv) · line 457

Quoted 7 lines
ui/curses.c +2 -2
@@ -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').

Quoted 5 lines

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

#3 lore raw
In reply to Gerd Hoffmann
Quoted 15 lines · Gerd Hoffmann

On some systems wchar_t is "long int", on others just "int". So go cast to "long int" and adjust the printf format accordingly.

---
 ui/curses.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ui/curses.c +0 -0
index cc6d6da68463..fb63945188b2 100644

ui/curses.c static uint16_t get_ucs(wchar_t wch, iconv_t conv) · line 460

Quoted 10 lines
ui/curses.c +2 -2
@@ -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