Skip to content

Commit 066c680

Browse files
mcharlebbehanw
authored andcommitted
net: netfilter: LLVMLinux: vlais-netfilter
Replaced non-standard C use of Variable Length Arrays In Structs (VLAIS) in xt_repldata.h with a C99 compliant flexible array member and then calculated offsets to the other struct members. These other members aren't referenced by name in this code, however this patch maintains the same memory layout and padding as was previously accomplished using VLAIS. Had the original structure been ordered differently, with the entries VLA at the end, then it could have been a flexible member, and this patch would have been a lot simpler. However since the data stored in this structure is ultimately exported to userspace, the order of this structure can't be changed. This patch makes no attempt to change the existing behavior, merely the way in which the current layout is accomplished using standard C99 constructs. As such the code can now be compiled with either gcc or clang. This version of the patch removes the trailing alignment that the VLAIS structure would allocate in order to simplify the patch. Author: Mark Charlebois <[email protected]> Signed-off-by: Mark Charlebois <[email protected]> Signed-off-by: Behan Webster <[email protected]> Signed-off-by: Vinícius Tinti <[email protected]>
1 parent 66d8ea5 commit 066c680

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

net/netfilter/xt_repldata.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,35 @@
55
* they serve as the hanging-off data accessed through repl.data[].
66
*/
77

8+
/* tbl has the following structure equivalent, but is C99 compliant:
9+
* struct {
10+
* struct type##_replace repl;
11+
* struct type##_standard entries[nhooks];
12+
* struct type##_error term;
13+
* } *tbl;
14+
*/
15+
816
#define xt_alloc_initial_table(type, typ2) ({ \
917
unsigned int hook_mask = info->valid_hooks; \
1018
unsigned int nhooks = hweight32(hook_mask); \
1119
unsigned int bytes = 0, hooknum = 0, i = 0; \
1220
struct { \
1321
struct type##_replace repl; \
14-
struct type##_standard entries[nhooks]; \
15-
struct type##_error term; \
16-
} *tbl = kzalloc(sizeof(*tbl), GFP_KERNEL); \
22+
struct type##_standard entries[]; \
23+
} *tbl; \
24+
struct type##_error *term; \
25+
size_t term_offset = (offsetof(typeof(*tbl), entries[nhooks]) + \
26+
__alignof__(*term) - 1) & ~(__alignof__(*term) - 1); \
27+
tbl = kzalloc(term_offset + sizeof(*term), GFP_KERNEL); \
1728
if (tbl == NULL) \
1829
return NULL; \
30+
term = (struct type##_error *)&(((char *)tbl)[term_offset]); \
1931
strncpy(tbl->repl.name, info->name, sizeof(tbl->repl.name)); \
20-
tbl->term = (struct type##_error)typ2##_ERROR_INIT; \
32+
*term = (struct type##_error)typ2##_ERROR_INIT; \
2133
tbl->repl.valid_hooks = hook_mask; \
2234
tbl->repl.num_entries = nhooks + 1; \
2335
tbl->repl.size = nhooks * sizeof(struct type##_standard) + \
24-
sizeof(struct type##_error); \
36+
sizeof(struct type##_error); \
2537
for (; hook_mask != 0; hook_mask >>= 1, ++hooknum) { \
2638
if (!(hook_mask & 1)) \
2739
continue; \

0 commit comments

Comments
 (0)