modpost,fixdep: Replace zero-length array with flexible-array
authorGustavo A. R. Silva <gustavoars@kernel.org>
Thu, 7 May 2020 18:56:01 +0000 (13:56 -0500)
committerMasahiro Yamada <masahiroy@kernel.org>
Mon, 25 May 2020 15:03:16 +0000 (00:03 +0900)
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: Masahiro Yamada <masahiroy@kernel.org>
scripts/basic/fixdep.c
scripts/mod/modpost.c
scripts/mod/modpost.h

index 877ca2c88246303fca7d792cc6a17ef72bfcc254..d98540552941c1b895b94361a924e975091ab747 100644 (file)
@@ -160,7 +160,7 @@ struct item {
        struct item     *next;
        unsigned int    len;
        unsigned int    hash;
-       char            name[0];
+       char            name[];
 };
 
 #define HASHSZ 256
index 5c3c50c5ec524aaf0e5cbadca2b0d7e8e285b59b..4d4b979d76bec55efe31cbae6fb469916ff9b974 100644 (file)
@@ -166,7 +166,7 @@ struct symbol {
                                    *  (only for external modules) **/
        unsigned int is_static:1;  /* 1 if symbol is not global */
        enum export  export;       /* Type of export */
-       char name[0];
+       char name[];
 };
 
 static struct symbol *symbolhash[SYMBOL_HASH_SIZE];
index 60dca9b7106b9a26ebe30ac77cefa7c11ce714f6..39f6c29fb5685ceb232c6b3a022fe9cf88f1453a 100644 (file)
@@ -111,7 +111,7 @@ buf_write(struct buffer *buf, const char *s, int len);
 
 struct namespace_list {
        struct namespace_list *next;
-       char namespace[0];
+       char namespace[];
 };
 
 struct module {