summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2020-10-22 11:50:23 +0200
committerThomas Haller <thaller@redhat.com>2020-10-22 22:58:02 +0200
commit67ad4166b66dd2c6ae9ff1e545574e1d4ff55717 (patch)
treeabfd7977cda00ed2cc08e8f6b5a5239f9bce4d6c
parent1d4e678252ba18c9579b199490f35653329b338d (diff)
downloadNetworkManager-67ad4166b66dd2c6ae9ff1e545574e1d4ff55717.tar.gz
shared: add nm_ptr_to_uintptr() helper
-rw-r--r--shared/nm-std-aux/nm-std-aux.h17
1 files changed, 16 insertions, 1 deletions
diff --git a/shared/nm-std-aux/nm-std-aux.h b/shared/nm-std-aux/nm-std-aux.h
index b181f93821..44d0dd273b 100644
--- a/shared/nm-std-aux/nm-std-aux.h
+++ b/shared/nm-std-aux/nm-std-aux.h
@@ -780,6 +780,21 @@ nm_steal_fd(int *p_fd)
/*****************************************************************************/
+static inline uintptr_t
+nm_ptr_to_uintptr(const void *p)
+{
+ /* in C, pointers can only be compared (with less-than or greater-than) under certain
+ * circumstances. Since uintptr_t is supposed to be able to represent the pointer
+ * as a plain integer and also support to convert the integer back to the pointer,
+ * it should be safer to compare the pointers directly.
+ *
+ * Of course, this function isn't very useful beyond that its use makes it clear
+ * that we want to compare pointers by value, which otherwise may not be valid. */
+ return (uintptr_t) p;
+}
+
+/*****************************************************************************/
+
#define NM_CMP_RETURN(c) \
do { \
const int _cc = (c); \
@@ -826,7 +841,7 @@ nm_steal_fd(int *p_fd)
* Avoid that by casting pointers to void* and then to uintptr_t. This comparison
* is not really meaningful, except that it provides some kind of stable sort order
* between pointers (that can otherwise not be compared). */
-#define NM_CMP_DIRECT_PTR(a, b) NM_CMP_DIRECT((uintptr_t)((void *) (a)), (uintptr_t)((void *) (b)))
+#define NM_CMP_DIRECT_PTR(a, b) NM_CMP_DIRECT(nm_ptr_to_uintptr(a), nm_ptr_to_uintptr(b))
#define NM_CMP_DIRECT_MEMCMP(a, b, size) NM_CMP_RETURN(memcmp((a), (b), (size)))