Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sparc64: Replace zero-length array with flexible-array

The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:

struct foo {
int stuff;
struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.

Also, notice that, dynamic memory allocations won't be affected by
this change:

"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]

sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.

This issue was found with the help of Coccinelle.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Gustavo A. R. Silva and committed by
David S. Miller
60da7d0b 57829ea4

+5 -5
+1 -1
arch/sparc/kernel/cpumap.c
··· 50 50 51 51 /* Offsets into nodes[] for each level of the tree */ 52 52 struct cpuinfo_level level[CPUINFO_LVL_MAX]; 53 - struct cpuinfo_node nodes[0]; 53 + struct cpuinfo_node nodes[]; 54 54 }; 55 55 56 56
+4 -4
arch/sparc/kernel/ds.c
··· 87 87 __u64 handle; 88 88 __u16 major; 89 89 __u16 minor; 90 - char svc_id[0]; 90 + char svc_id[]; 91 91 }; 92 92 93 93 struct ds_reg_ack { ··· 701 701 702 702 struct ds_var_set_msg { 703 703 struct ds_var_hdr hdr; 704 - char name_and_value[0]; 704 + char name_and_value[]; 705 705 }; 706 706 707 707 struct ds_var_delete_msg { 708 708 struct ds_var_hdr hdr; 709 - char name[0]; 709 + char name[]; 710 710 }; 711 711 712 712 struct ds_var_resp { ··· 989 989 struct ds_info *dp; 990 990 int req_len; 991 991 int __pad; 992 - u64 req[0]; 992 + u64 req[]; 993 993 }; 994 994 995 995 static void process_ds_work(void)