diff mbox series

[ovs-dev,v4,6/7] util: Avoid UB when iterating collections.

Message ID 20220225171503.16476.73840.stgit@dceara.remote.csb
State Changes Requested
Headers show
Series Fix UndefinedBehaviorSanitizer reported issues and enable it in CI. | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/intel-ovs-compilation success test: success
ovsrobot/github-robot-_Build_and_Test success github build: passed

Commit Message

Dumitru Ceara Feb. 25, 2022, 5:15 p.m. UTC
UB Sanitizer was flagging OBJECT_CONTAINING() as undefined behavior
due to the way it's used when iterating through collections, e.g.,
HMAP_FOR_EACH_INIT().  However, in such cases we don't actually
dereference the pointer, we just use its value.  Avoid the undefined
behavior by casting to 'void *' first.

NOTE: This patch is incomplete and hides some more UB.  It's added just
to make sure tests pass when the last patch of the series is applied.
The real solution is implemented by Adrian here:

https://patchwork.ozlabs.org/project/openvswitch/list/?series=286494&state=*

Acked-by: Aaron Conole <aconole@redhat.com>
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
---
v4:
- Rebased.
- Changed commit subject, UBSan doesn't really have false positives.
- Added note about the fact that the patch shouldn't really be applied
  as-is.
---
 include/openvswitch/util.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/include/openvswitch/util.h b/include/openvswitch/util.h
index f45dc505164c..9a22ed56c505 100644
--- a/include/openvswitch/util.h
+++ b/include/openvswitch/util.h
@@ -128,7 +128,7 @@  OVS_NO_RETURN void ovs_assert_failure(const char *, const char *, const char *);
  * from the type of '*OBJECT'. */
 #define OBJECT_CONTAINING(POINTER, OBJECT, MEMBER)                      \
     ((OVS_TYPEOF(OBJECT)) (void *)                                      \
-     ((char *) (POINTER) - OBJECT_OFFSETOF(OBJECT, MEMBER)))
+     ((uintptr_t)(void *)(POINTER) - OBJECT_OFFSETOF(OBJECT, MEMBER)))
 
 /* Given POINTER, the address of the given MEMBER within an object of the type
  * that that OBJECT points to, assigns the address of the outer object to