Skip to contentSkip to conversation

use %z for formatting

PATCH Applied Stephen Hemminger 2 participants 4 messages Feb 15, 2018 – Apr 4, 2018

lore
Share & tools
lore
#0 lore raw

cover use %z for formatting

Better to use %z and avoid any 32 bit platform woes; and not cast unnecessarily

Stephen Hemminger (2):
pci: use %z to format size_t
eal: use %zu to format size_t

 lib/librte_eal/linuxapp/eal/eal_memory.c |  8 ++++----
 lib/librte_pci/rte_pci.c                 | 11 ++++++-----
 2 files changed, 10 insertions(+), 9 deletions(-)
Signature · 1 line
2.15.1
#1 lore raw

PATCH 1/2 pci: use %z to format size_t

This addresses potential issues where size_t and off_t can vary on some platforms. For size_t the best way to format the value is to use the z modifier to printf. For off_t need to cast to long long to handle 64 bit offset on 32 bit platforms.

---
 lib/librte_pci/rte_pci.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
lib/librte_pci/rte_pci.c +6 -5
index 1a6d748568ad..530738dbdf47 100644
@@ -155,9 +155,10 @@ pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
 	mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
 			MAP_SHARED | additional_flags, fd, offset);
 	if (mapaddr == MAP_FAILED) {
-		RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
-			__func__, fd, requested_addr,
-			(unsigned long)size, (unsigned long)offset,
+		RTE_LOG(ERR, EAL,
+			"%s(): cannot mmap(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
+			__func__, fd, requested_addr, size,
+			(unsigned long long)offset,
 			strerror(errno), mapaddr);
 	} else
 		RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
@@ -174,8 +175,8 @@ pci_unmap_resource(void *requested_addr, size_t size)
 
 	/* Unmap the PCI memory resource of device */
 	if (munmap(requested_addr, size)) {
-		RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
-			__func__, requested_addr, (unsigned long)size,
+		RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, %#zx): %s\n",
+			__func__, requested_addr, size,
 			strerror(errno));
 	} else
 		RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
Signature · 1 line
2.15.1
#2 lore raw

PATCH 2/2 eal: use %zu to format size_t

In reply to Stephen Hemminger

The recommended way to format size_t in printf is to use the z modifier which handles the case where size_t maybe 32 or 64 bits.

---
 lib/librte_eal/linuxapp/eal/eal_memory.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
lib/librte_eal/linuxapp/eal/eal_memory.c +4 -4
index 38853b753aab..86df67480201 100644
@@ -1390,16 +1390,16 @@ rte_eal_hugepage_attach(void)
 			max_seg = s;
 			if (base_addr != MAP_FAILED) {
 				/* errno is stale, don't use */
-				RTE_LOG(ERR, EAL, "Could not mmap %llu bytes "
+				RTE_LOG(ERR, EAL, "Could not mmap %zu bytes "
 					"in /dev/zero at [%p], got [%p] - "
 					"please use '--base-virtaddr' option\n",
-					(unsigned long long)mcfg->memseg[s].len,
+					mcfg->memseg[s].len,
 					mcfg->memseg[s].addr, base_addr);
 				munmap(base_addr, mcfg->memseg[s].len);
 			} else {
-				RTE_LOG(ERR, EAL, "Could not mmap %llu bytes "
+				RTE_LOG(ERR, EAL, "Could not mmap %zu bytes "
 					"in /dev/zero at [%p]: '%s'\n",
-					(unsigned long long)mcfg->memseg[s].len,
+					mcfg->memseg[s].len,
 					mcfg->memseg[s].addr, strerror(errno));
 			}
 			if (aslr_enabled() > 0) {
Signature · 1 line
2.15.1
#3 lore raw
In reply to Stephen Hemminger

15/02/2018 23:25, Stephen Hemminger:

Quoted 10 lines

Better to use %z and avoid any 32 bit platform woes; and not cast unnecessarily

Stephen Hemminger (2):
pci: use %z to format size_t
eal: use %zu to format size_t

 lib/librte_eal/linuxapp/eal/eal_memory.c |  8 ++++----
 lib/librte_pci/rte_pci.c                 | 11 ++++++-----
 2 files changed, 10 insertions(+), 9 deletions(-)

Applied, thanks