diff options
author | Thomas Haller <thaller@redhat.com> | 2020-10-22 11:50:23 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-10-23 17:11:53 +0200 |
commit | c947c51651ad3a5a1914f368c4b3ca948f63e122 (patch) | |
tree | 8437e658917fb2c69b24e4ed1c8c5bab74817442 | |
parent | b38075b751710a1d954188e595e65749d67abf29 (diff) | |
download | NetworkManager-c947c51651ad3a5a1914f368c4b3ca948f63e122.tar.gz |
shared: add nm_ptr_to_uintptr() helper
-rw-r--r-- | shared/nm-std-aux/nm-std-aux.h | 17 |
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))) |