Skip to contentSkip to conversation

spapr_drc: Fix potential undefined behaviour

RFC In review David Gibson 2 participants 2 messages Sep 1, 2015 – Sep 2, 2015

lore
Share & tools
lore
#0 lore raw

RFC 1/1 spapr_drc: Fix potential undefined behaviour

The DRC_INDEX_ID_MASK macro does a left shift on ~0, which is a signed quantity, and therefore undefined behaviour according to the C spec. In particular this causes warnings from the clang sanitizer.

This fixes it by calculating the same mask without using ~0 (I think the new method is a more common idiom for generating masks anyway). For good measure I also use 1ULL to force the expression's type to unsigned long long, which should be good for assigning to anything we're going to want to.

---
 hw/ppc/spapr_drc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

I'm hoping to get some Reviewed-bys for this patch so that I'm ready to merge my spapr-next queue.

hw/ppc/spapr_drc.c +1 -1
index ee87432..8cbcf4d 100644
@@ -32,7 +32,7 @@
 
 #define DRC_CONTAINER_PATH "/dr-connector"
 #define DRC_INDEX_TYPE_SHIFT 28
-#define DRC_INDEX_ID_MASK (~(~0 << DRC_INDEX_TYPE_SHIFT))
+#define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
 
 static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
 {
Signature · 1 line
2.4.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · David Gibson

The DRC_INDEX_ID_MASK macro does a left shift on ~0, which is a signed quantity, and therefore undefined behaviour according to the C spec. In particular this causes warnings from the clang sanitizer.

Quoted 8 lines

This fixes it by calculating the same mask without using ~0 (I think the new method is a more common idiom for generating masks anyway). For good measure I also use 1ULL to force the expression's type to unsigned long long, which should be good for assigning to anything we're going to want to.

Quoted 21 lines
---
  hw/ppc/spapr_drc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

I'm hoping to get some Reviewed-bys for this patch so that I'm ready to merge my spapr-next queue.

hw/ppc/spapr_drc.c +1 -1
index ee87432..8cbcf4d 100644
@@ -32,7 +32,7 @@
 
  #define DRC_CONTAINER_PATH "/dr-connector"
  #define DRC_INDEX_TYPE_SHIFT 28
-#define DRC_INDEX_ID_MASK (~(~0 << DRC_INDEX_TYPE_SHIFT))
+#define DRC_INDEX_ID_MASK ((1ULL << DRC_INDEX_TYPE_SHIFT) - 1)
 
  static sPAPRDRConnectorTypeShift get_type_shift(sPAPRDRConnectorType type)
  {
Signature · 1 line
Alexey