Skip to contentSkip to conversation

Fix %#0 misuses

PATCH In review Dr. David Alan Gilbert (git) 2 participants 3 messages Feb 2, 2022 – Feb 3, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Fix %#0 misuses

From: "Dr. David Alan Gilbert" <[email protected]>

Joe Perches pointed out on lkml [1] that the format special %# (which adds 0x) is included in the character count, i.e.:

printf("0: %#08x\n0: %#08x\n", 0xabcdef01,1);
gives:
0: 0xabcdef01
0: 0x000001

rather than padding to the expected 8 data characters.

Replace all the '%#08' cases by '0x%08' and also handle a '%#02'; there are some other cases mostly in testing and a few that look like the authors have thought about the size, that I've ignored for now.

(Note I've not managed to test most of these)

[1] https://lore.kernel.org/lkml/[email protected]/

Fixup %#

---
 chardev/baum.c          | 2 +-
 disas/alpha.c           | 2 +-
 disas/sparc.c           | 2 +-
 hw/arm/omap1.c          | 2 +-
 hw/timer/a9gtimer.c     | 4 ++--
 include/hw/arm/omap.h   | 5 +++--
 softmmu/device_tree.c   | 2 +-
 target/openrisc/disas.c | 2 +-
 8 files changed, 11 insertions(+), 10 deletions(-)
chardev/baum.c +1 -1
index 79d618e350..9a842c2bbb 100644
@@ -366,7 +366,7 @@ static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len)
         if (!len--) \
             return 0; \
         if (*cur++ != ESC) { \
-            DPRINTF("Broken packet %#2x, tossing\n", req); \
+            DPRINTF("Broken packet 0x%02x, tossing\n", req); \
             if (timer_pending(baum->cellCount_timer)) {    \
                 timer_del(baum->cellCount_timer);     \
                 baum_cellCount_timer_cb(baum);             \
disas/alpha.c +1 -1
index 3db90fa665..f218bf023a 100644
@@ -1851,7 +1851,7 @@ print_insn_alpha (bfd_vma memaddr, struct disassemble_info *info)
     }
 
   /* No instruction found */
-  (*info->fprintf_func) (info->stream, ".long %#08x", insn);
+  (*info->fprintf_func) (info->stream, ".long 0x%08x", insn);
 
   return 4;
 
disas/sparc.c +1 -1
index 5689533ce1..adc8438aa2 100644
@@ -3231,6 +3231,6 @@ print_insn_sparc (bfd_vma memaddr, disassemble_info *info)
     }
 
   info->insn_type = dis_noninsn;        /* Mark as non-valid instruction.  */
-  (*info->fprintf_func) (stream, ".long %#08lx", insn);
+  (*info->fprintf_func) (stream, ".long 0x%08lx", insn);
   return sizeof (buffer);
 }
hw/arm/omap1.c +1 -1
index 9852c2a07e..c9e0ae87b1 100644
@@ -42,7 +42,7 @@
 
 static inline void omap_log_badwidth(const char *funcname, hwaddr addr, int sz)
 {
-    qemu_log_mask(LOG_GUEST_ERROR, "%s: %d-bit register %#08" HWADDR_PRIx "\n",
+    qemu_log_mask(LOG_GUEST_ERROR, "%s: %d-bit register 0x%08" HWADDR_PRIx "\n",
                   funcname, 8 * sz, addr);
 }
 
hw/timer/a9gtimer.c +2 -2
index 7233068a37..2ea51a137e 100644
@@ -169,7 +169,7 @@ static uint64_t a9_gtimer_read(void *opaque, hwaddr addr, unsigned size)
         return 0;
     }
 
-    DB_PRINT("addr:%#x data:%#08" PRIx64 "\n", (unsigned)addr, ret);
+    DB_PRINT("addr:0x%x data:0x%08" PRIx64 "\n", (unsigned)addr, ret);
     return ret;
 }
 
@@ -180,7 +180,7 @@ static void a9_gtimer_write(void *opaque, hwaddr addr, uint64_t value,
     A9GTimerState *s = gtb->parent;
     int shift = 0;
 
-    DB_PRINT("addr:%#x data:%#08" PRIx64 "\n", (unsigned)addr, value);
+    DB_PRINT("addr:0x%x data:0x%08" PRIx64 "\n", (unsigned)addr, value);
 
     switch (addr) {
     case R_COUNTER_HI:
include/hw/arm/omap.h +3 -2
index ff6a173f8a..8b1c2365d5 100644
@@ -1003,10 +1003,11 @@ void omap_badwidth_write32(void *opaque, hwaddr addr,
 void omap_mpu_wakeup(void *opaque, int irq, int req);
 
 # define OMAP_BAD_REG(paddr)		\
-        qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad register %#08"HWADDR_PRIx"\n", \
+        qemu_log_mask(LOG_GUEST_ERROR,  \
+                      "%s: Bad register 0x%08"HWADDR_PRIx"\n", \
                       __func__, paddr)
 # define OMAP_RO_REG(paddr)		\
-        qemu_log_mask(LOG_GUEST_ERROR, "%s: Read-only register %#08" \
+        qemu_log_mask(LOG_GUEST_ERROR, "%s: Read-only register 0x%08" \
                                        HWADDR_PRIx "\n", \
                       __func__, paddr)
 
softmmu/device_tree.c +1 -1
index 6ca3fad285..338e1c03fd 100644
@@ -369,7 +369,7 @@ int qemu_fdt_setprop_cell(void *fdt, const char *node_path,
 
     r = fdt_setprop_cell(fdt, findnode_nofail(fdt, node_path), property, val);
     if (r < 0) {
-        error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
+        error_report("%s: Couldn't set %s/%s = 0x%08x: %s", __func__,
                      node_path, property, val, fdt_strerror(r));
         exit(1);
     }
target/openrisc/disas.c +1 -1
index dc025bd64d..72f1476a13 100644
@@ -45,7 +45,7 @@ int print_insn_or1k(bfd_vma addr, disassemble_info *info)
     insn = bfd_getb32(buffer);
 
     if (!decode(info, insn)) {
-        output(".long", "%#08x", insn);
+        output(".long", "0x%08x", insn);
     }
     return 4;
 }
Signature · 1 line
2.34.1
Reviewed-by Philippe Mathieu-Daudé on v2 1/1
#1 lore raw
Quoted 24 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Joe Perches pointed out on lkml [1] that the format special %# (which adds 0x) is included in the character count, i.e.:

   printf("0: %#08x\n0: %#08x\n", 0xabcdef01,1);

gives:
0: 0xabcdef01
0: 0x000001

rather than padding to the expected 8 data characters.

Replace all the '%#08' cases by '0x%08' and also handle a '%#02'; there are some other cases mostly in testing and a few that look like the authors have thought about the size, that I've ignored for now.

(Note I've not managed to test most of these)

[1] https://lore.kernel.org/lkml/[email protected]/

Fixup %#

Quoted 10 lines
---
  chardev/baum.c          | 2 +-
  disas/alpha.c           | 2 +-
  disas/sparc.c           | 2 +-
  hw/arm/omap1.c          | 2 +-
  hw/timer/a9gtimer.c     | 4 ++--
  include/hw/arm/omap.h   | 5 +++--
  softmmu/device_tree.c   | 2 +-
  target/openrisc/disas.c | 2 +-
  8 files changed, 11 insertions(+), 10 deletions(-)

Few more:

$ git grep -E '\%#[0-9]{1,2}[dDxX]'
chardev/baum.c:369:            DPRINTF("Broken packet %#2x, tossing\n", req); \
disas/alpha.c:1854:  (*info->fprintf_func) (info->stream, ".long %#08x", insn);
hw/audio/ac97.c:244:    dolog ("bd %2d addr=%#x ctl=%#06x len=%#x(%d bytes)\n",
hw/dma/i8257.c:265:        linfo ("write_cont: nport %#06x, ichan % 2d, val %#06x\n",
hw/dma/i8257.c:290:    ldebug ("read_cont: nport %#06x, iport %#04x val %#x\n", nport, iport, val);
softmmu/device_tree.c:372:        error_report("%s: Couldn't set %s/%s = %#08x: %s", __func__,
target/openrisc/disas.c:48:        output(".long", "%#08x", insn);
tests/qemu-iotests/nbd-fault-injector.py:187: print('unrecognized command type %#02x' % req.type)
tests/tcg/arm/fcvt.c:60:    printf("%02d   HALF: %#04x  (%#x => %s)\n",
tests/tcg/multiarch/libs/float_helpers.c:72:    asprintf(&fmt, "f16(%#04x)", num);

In mood to add a regexp to checkpatch? :)

#2 lore raw
Quoted 36 lines · * Philippe Mathieu-Daudé ([email protected]) wrote:
Quoted 24 lines · Dr. David Alan Gilbert (git)

From: "Dr. David Alan Gilbert" <[email protected]>

Joe Perches pointed out on lkml [1] that the format special %# (which adds 0x) is included in the character count, i.e.:

   printf("0: %#08x\n0: %#08x\n", 0xabcdef01,1);

gives:
0: 0xabcdef01
0: 0x000001

rather than padding to the expected 8 data characters.

Replace all the '%#08' cases by '0x%08' and also handle a '%#02'; there are some other cases mostly in testing and a few that look like the authors have thought about the size, that I've ignored for now.

(Note I've not managed to test most of these)

[1] https://lore.kernel.org/lkml/[email protected]/

Fixup %#

Quoted 10 lines
---
  chardev/baum.c          | 2 +-
  disas/alpha.c           | 2 +-
  disas/sparc.c           | 2 +-
  hw/arm/omap1.c          | 2 +-
  hw/timer/a9gtimer.c     | 4 ++--
  include/hw/arm/omap.h   | 5 +++--
  softmmu/device_tree.c   | 2 +-
  target/openrisc/disas.c | 2 +-
  8 files changed, 11 insertions(+), 10 deletions(-)
Quoted 3 lines

Few more:

$ git grep -E '\%#[0-9]{1,2}[dDxX]'

Thanks, actually 1,3 is worth it :-)

Quoted 2 lines

chardev/baum.c:369: DPRINTF("Broken packet %#2x, tossing\n",
req); \

Interestingly without the 0

Quoted 8 lines

disas/alpha.c:1854: (*info->fprintf_func) (info->stream, ".long %#08x", insn);
hw/audio/ac97.c:244: dolog ("bd %2d addr=%#x ctl=%#06x len=%#x(%d
bytes)\n",
hw/dma/i8257.c:265: linfo ("write_cont: nport %#06x, ichan % 2d, val
%#06x\n",
hw/dma/i8257.c:290: ldebug ("read_cont: nport %#06x, iport %#04x val
%#x\n", nport, iport, val);

So some of these are a bit weird and might need thinking about; I *think* they're 06 because the author realised the 0x was counted and bumped it up from 04; but that actually means I'd need to understand the device code before changing it.

Quoted 10 lines

softmmu/device_tree.c:372: error_report("%s: Couldn't set %s/%s =
%#08x: %s", __func__,
target/openrisc/disas.c:48: output(".long", "%#08x", insn);
tests/qemu-iotests/nbd-fault-injector.py:187: print('unrecognized command type %#02x' % req.type)
tests/tcg/arm/fcvt.c:60: printf("%02d HALF: %#04x (%#x => %s)\n",
tests/tcg/multiarch/libs/float_helpers.c:72: asprintf(&fmt, "f16(%#04x)",
num);

In mood to add a regexp to checkpatch? :)

I think it already has one for new patches.

Quoted 1 line

Thanks!

Dave

Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK