Skip to contentSkip to conversation

eal: fix pointer add/sub macros for large offsets values

PATCH In review Bruce Richardson 2 participants 3 messages Sep 11 – Sep 14

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal: fix pointer add/sub macros for large offsets values

The macros for adding or subtracting from pointers cast the pointer type to uintptr_t before doing the arithmetic. However, for 32-bit builds, if the value being added or subtracted is larger than uintptr_t, then we get errors when casting back to "void *" type. We fix this by casting the result to uintptr_t before casting to pointer.

Fixes af75078fece3 ("first public release")
---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/rte_common.h +2 -2
index f872d3eabb..c17f50e2fd 100644
@@ -572,12 +572,12 @@ static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
 /**
  * add a byte-value offset to a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
+#define RTE_PTR_ADD(ptr, x) ((void *)(uintptr_t)((uintptr_t)(ptr) + (x)))
 
 /**
  * subtract a byte-value offset from a pointer
  */
-#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)(uintptr_t)((uintptr_t)(ptr) - (x)))
 
 /**
  * get the difference between two pointer values, i.e. how far apart
Signature · 1 line
2.53.0
#1 lore raw
Quoted 6 lines

The macros for adding or subtracting from pointers cast the pointer type to uintptr_t before doing the arithmetic. However, for 32-bit builds, if the value being added or subtracted is larger than uintptr_t, then we get errors when casting back to "void *" type.

But probably error here in such case is a good thing, as it flags an overflow? Can't user explicitly convert x then to 32-bit type, if that was an intention?

Quoted 33 lines

We fix this by casting
the result to uintptr_t before casting to pointer.

Fixes af75078fece3 ("first public release")
---
 lib/eal/include/rte_common.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/eal/include/rte_common.h +2 -2
index f872d3eabb..c17f50e2fd 100644
@@ -572,12 +572,12 @@ static void __attribute__((destructor(RTE_PRIO(prio)),
 used)) func(void)
 /**
  * add a byte-value offset to a pointer
  */
-#define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
+#define RTE_PTR_ADD(ptr, x) ((void *)(uintptr_t)((uintptr_t)(ptr) + (x)))
 
 /**
  * subtract a byte-value offset from a pointer
  */
-#define RTE_PTR_SUB(ptr, x) ((void *)((uintptr_t)(ptr) - (x)))
+#define RTE_PTR_SUB(ptr, x) ((void *)(uintptr_t)((uintptr_t)(ptr) - (x)))
 
 /**

* get the difference between two pointer values, i.e. how far apart
--
2.53.0

#2 lore raw
Quoted 10 lines · Konstantin Ananyev
Quoted 6 lines

The macros for adding or subtracting from pointers cast the pointer type to uintptr_t before doing the arithmetic. However, for 32-bit builds, if the value being added or subtracted is larger than uintptr_t, then we get errors when casting back to "void *" type.

But probably error here in such case is a good thing, as it flags an overflow? Can't user explicitly convert x then to 32-bit type, if that was an intention?

Fair point, hadn't thought of that! Let me mark this as rejected so.

/Bruce