[ovs-dev] [PATCH] packet: proper return type for vlan_tci_to_pcp()

Shu Shen shu.shen at gmail.com
Fri Jan 13 21:42:19 UTC 2017


The return type of vlan_tci_to_pcp() was int where it's expected to be
uint_8 and causing implicit truncation when the function is used. On
some platforms such as macOS, where PRIu8 is defined as "hhx" and no
promotion of short to int is done, the compiler might throw out Wformat
message for ds_put_format() calls on the returns value of
vlan_tci_to_pcp().

vlan_tci_to_cfi() is also fixed with unit_8 as return type although the
function is not currently being used anywhere.

Format strings in ds_put_format() for printing out returned values from
vlan_tci_to_pcp() were updated to ensure PRIu8 or PRIx8 are used for
portability.

Signed-off-by: Shu Shen <shu.shen at gmail.com>
---
 lib/odp-util.c | 4 ++--
 lib/packets.h  | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/odp-util.c b/lib/odp-util.c
index 1e70e3a89..31d486f3c 100644
--- a/lib/odp-util.c
+++ b/lib/odp-util.c
@@ -368,9 +368,9 @@ format_vlan_tci(struct ds *ds, ovs_be16 tci, ovs_be16 mask, bool verbose)
         ds_put_char(ds, ',');
     }
     if (verbose || vlan_tci_to_pcp(tci) || vlan_tci_to_pcp(mask)) {
-        ds_put_format(ds, "pcp=%d", vlan_tci_to_pcp(tci));
+        ds_put_format(ds, "pcp=%"PRIu8, vlan_tci_to_pcp(tci));
         if (vlan_tci_to_pcp(mask) != (VLAN_PCP_MASK >> VLAN_PCP_SHIFT)) {
-            ds_put_format(ds, "/0x%x", vlan_tci_to_pcp(mask));
+            ds_put_format(ds, "/0x%"PRIx8, vlan_tci_to_pcp(mask));
         }
         ds_put_char(ds, ',');
     }
diff --git a/lib/packets.h b/lib/packets.h
index c4d379967..2cf5419d6 100644
--- a/lib/packets.h
+++ b/lib/packets.h
@@ -421,7 +421,7 @@ vlan_tci_to_vid(ovs_be16 vlan_tci)
 
 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
  * returns the priority code point (PCP) in host byte order. */
-static inline int
+static inline uint8_t
 vlan_tci_to_pcp(ovs_be16 vlan_tci)
 {
     return (ntohs(vlan_tci) & VLAN_PCP_MASK) >> VLAN_PCP_SHIFT;
@@ -429,7 +429,7 @@ vlan_tci_to_pcp(ovs_be16 vlan_tci)
 
 /* Given the vlan_tci field from an 802.1Q header, in network byte order,
  * returns the Canonical Format Indicator (CFI). */
-static inline int
+static inline uint8_t
 vlan_tci_to_cfi(ovs_be16 vlan_tci)
 {
     return (vlan_tci & htons(VLAN_CFI)) != 0;
-- 
2.11.0



More information about the dev mailing list