Skip to contentSkip to conversation

Fix %#08 misuses

PATCH In review Dr. David Alan Gilbert (git) 3 participants 4 messages Oct 14, 2021 – Oct 18, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix %#08 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. It looks like we use '%#.8x' in some places and that seems like it works:

printf(".: %#.8x\n.: %#.8x\n", 0xabcdef01,1);
.: 0xabcdef01
.: 0x00000001

Replace all the '%#08' cases by '%#.8' ; I've not attacked the other lengths yet.

Although I'm tempted to think that perhaps we should just prefer '0x%08' which seems clearer.

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

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

---
 disas/alpha.c           | 2 +-
 disas/sparc.c           | 2 +-
 hw/arm/omap1.c          | 2 +-
 hw/timer/a9gtimer.c     | 4 ++--
 include/hw/arm/omap.h   | 4 ++--
 softmmu/device_tree.c   | 2 +-
 target/openrisc/disas.c | 2 +-
 7 files changed, 9 insertions(+), 9 deletions(-)
disas/alpha.c +1 -1
index 3db90fa665..6da5c473d8 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 %#.8x", insn);
 
   return 4;
 
disas/sparc.c +1 -1
index 5689533ce1..9f1160ab1c 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 %#.8lx", insn);
   return sizeof (buffer);
 }
hw/arm/omap1.c +1 -1
index 180d3788f8..cff8ecb7d6 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 %#.8" HWADDR_PRIx "\n",
                   funcname, 8 * sz, addr);
 }
 
hw/timer/a9gtimer.c +2 -2
index 7233068a37..c53edd8b44 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:%#x data:%#.8" 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:%#x data:%#.8" PRIx64 "\n", (unsigned)addr, value);
 
     switch (addr) {
     case R_COUNTER_HI:
include/hw/arm/omap.h +2 -2
index ff6a173f8a..47616bbba3 100644
@@ -1003,10 +1003,10 @@ 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 %#.8"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 %#.8" \
                                        HWADDR_PRIx "\n", \
                       __func__, paddr)
 
softmmu/device_tree.c +1 -1
index b621f63fba..bd271ab66e 100644
@@ -367,7 +367,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 = %#.8x: %s", __func__,
                      node_path, property, val, fdt_strerror(r));
         exit(1);
     }
target/openrisc/disas.c +1 -1
index dc025bd64d..024f243a45 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", "%#.8x", insn);
     }
     return 4;
 }
Signature · 1 line
2.31.1
#1 lore raw
Quoted 2 lines · Dr. David Alan Gilbert (git)

Although I'm tempted to think that perhaps we should just prefer '0x%08' which seems clearer.

That's what I think we should prefer.

r~

#2 lore raw
Quoted 5 lines · Richard Henderson
Quoted 2 lines · Dr. David Alan Gilbert (git)

Although I'm tempted to think that perhaps we should just prefer '0x%08' which seems clearer.

That's what I think we should prefer.

I agree, it's always more clear to me too.

-Stafford

#3 lore raw
Quoted 8 lines · * Stafford Horne ([email protected]) wrote:
Quoted 5 lines · Richard Henderson
Quoted 2 lines · Dr. David Alan Gilbert (git)

Although I'm tempted to think that perhaps we should just prefer '0x%08' which seems clearer.

That's what I think we should prefer.

I agree, it's always more clear to me too.

OK, I'll rework it.

Dave

Quoted 2 lines

-Stafford

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