]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: container_of() - Always return NULL if input pointer is NULL
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Mon, 27 Sep 2021 22:24:24 +0000 (01:24 +0300)
committertimo.sirainen <timo.sirainen@open-xchange.com>
Wed, 29 Sep 2021 14:44:22 +0000 (14:44 +0000)
src/lib/macros.h

index fb5923dc3c620289daca88e060a1ed1c626687b5..3a3db2c3ed91293a4a7f151abf0ab279ab9479cb 100644 (file)
 #define CONST_PTR_OFFSET(ptr, offset) \
        ((const void *) (((uintptr_t) (ptr)) + ((size_t) (offset))))
 
+static inline const char *container_of_ptr(const void *ptr, size_t offset)
+{
+       if (ptr == NULL)
+               return NULL;
+       return (const char *)ptr - offset;
+}
 #define container_of(ptr, type, name) \
-       (type *)((char *)(ptr) - offsetof(type, name) + \
-                COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(ptr, &((type *) 0)->name))
+       (type *)(container_of_ptr(ptr, offsetof(type, name) + \
+                COMPILE_ERROR_IF_TYPES_NOT_COMPATIBLE(ptr, &((type *) 0)->name)))
 
 /* Don't use simply MIN/MAX, as they're often defined elsewhere in include
    files that are included after this file generating tons of warnings. */