Skip to content

Commit 32a4ec2

Browse files
Alexey Dobriyankees
authored andcommitted
uapi: stddef.h: Fix __DECLARE_FLEX_ARRAY for C++
__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: 3080ea5 ("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>
1 parent 531108e commit 32a4ec2

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

include/uapi/linux/stddef.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
struct TAG { MEMBERS } ATTRS NAME; \
3030
}
3131

32+
#ifdef __cplusplus
33+
/* sizeof(struct{}) is 1 in C++, not 0, can't use C version of the macro. */
34+
#define __DECLARE_FLEX_ARRAY(T, member) \
35+
T member[0]
36+
#else
3237
/**
3338
* __DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union
3439
*
@@ -44,6 +49,7 @@
4449
struct { } __empty_ ## NAME; \
4550
TYPE NAME[]; \
4651
}
52+
#endif
4753

4854
#ifndef __counted_by
4955
#define __counted_by(m)

0 commit comments

Comments
 (0)