]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
uapi: stddef.h: Fix __DECLARE_FLEX_ARRAY for C++
authorAlexey Dobriyan <adobriyan@gmail.com>
Tue, 12 Sep 2023 16:22:24 +0000 (19:22 +0300)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 23 Feb 2024 07:42:18 +0000 (08:42 +0100)
[ Upstream commit 32a4ec211d4164e667d9d0b807fadf02053cd2e9 ]

__DECLARE_FLEX_ARRAY(T, member) macro expands to

struct {
struct {} __empty_member;
T member[];
};

which is subtly wrong in C++ because sizeof(struct{}) is 1 not 0,
changing UAPI structures layouts.

This can be fixed by expanding to

T member[];

Now g++ doesn't like "T member[]" either, throwing errors on
the following code:

struct S {
union {
T1 member1[];
T2 member2[];
};
};

or

struct S {
T member[];
};

Use "T member[0];" which seems to work and does the right thing wrt
structure layout.

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
Fixes: 3080ea5553cc ("stddef: Introduce DECLARE_FLEX_ARRAY() helper")
Link: https://lore.kernel.org/r/97242381-f1ec-4a4a-9472-1a464f575657@p183
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/uapi/linux/stddef.h

index 7837ba4fe7289024c10942a7793411e0e3b27e7f..46c7bab501cbdb69f75b44150d5116c130ebc45f 100644 (file)
                struct TAG { MEMBERS } ATTRS NAME; \
        }
 
+#ifdef __cplusplus
+/* sizeof(struct{}) is 1 in C++, not 0, can't use C version of the macro. */
+#define __DECLARE_FLEX_ARRAY(T, member)        \
+       T member[0]
+#else
 /**
  * __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
  *
@@ -45,3 +50,5 @@
                TYPE NAME[]; \
        }
 #endif
+
+#endif