]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
mem: make _gnutls_take_pointer type safer 2085/head
authorDaiki Ueno <ueno@gnu.org>
Tue, 17 Mar 2026 03:42:23 +0000 (12:42 +0900)
committerDaiki Ueno <ueno@gnu.org>
Thu, 26 Mar 2026 13:32:36 +0000 (22:32 +0900)
This changes the source parameter of _gnutls_take_pointer from "void **"
to "void *", while adding automatic cast using typeof.

Signed-off-by: Daiki Ueno <ueno@gnu.org>
lib/mem.h

index 4d9c7c6acfe15cefc009bd5b4616627fc163bbe0..5e5f02a95153f32890071e9bdb2fb087c3344ba7 100644 (file)
--- a/lib/mem.h
+++ b/lib/mem.h
@@ -82,11 +82,27 @@ static inline void _gnutls_memory_mark_defined(void *addr, size_t size)
 #endif
 }
 
-static inline ATTRIBUTE_NONNULL() void *_gnutls_take_pointer(void **src)
+static inline ATTRIBUTE_NONNULL() void *_gnutls_take_pointer(void *src)
 {
-       void *dst = *src;
-       *src = NULL;
+       void **ptr = (void **)src;
+       void *dst;
+
+       dst = *ptr;
+       *ptr = NULL;
+
        return dst;
 }
 
+/* Type-safety */
+#if (((defined __GNUC__ &&                                                    \
+       __GNUC__ + (__GNUC_MINOR__ >= 1) > 3) /* both C and C++ mode */        \
+      || (defined __clang__ && __clang_major__ >= 3 /* both C and C++ mode */ \
+         && !(defined __cplusplus &&                                         \
+              !defined __GNUC__))) /* except for clang-cl in C++ mode */     \
+     && !defined __STRICT_ANSI__) /* but not with -std=c99 or -std=c11 */     \
+       || (defined __SUNPRO_C && __SUNPRO_C >= 0x5110) /* C mode */          \
+       || __STDC_VERSION__ >= 202311L /* C mode */
+#define _gnutls_take_pointer(pp) ((typeof(*pp))(_gnutls_take_pointer)(pp))
+#endif /* HAVE_TYPEOF */
+
 #endif /* GNUTLS_LIB_MEM_H */