]> git.ipfire.org Git - thirdparty/ipset.git/commitdiff
netfilter: Replace zero-length array with flexible-array member
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Thu, 20 Feb 2020 13:59:14 +0000 (07:59 -0600)
committerJozsef Kadlecsik <kadlec@netfilter.org>
Sun, 20 Sep 2020 10:26:15 +0000 (12:26 +0200)
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]

Lastly, fix checkpatch.pl warning
WARNING: __aligned(size) is preferred over __attribute__((aligned(size)))
in net/bridge/netfilter/ebtables.c

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 <gustavo@embeddedor.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Jozsef Kadlecsik <kadlec@netfilter.org>
kernel/include/linux/netfilter/ipset/ip_set.h
kernel/net/netfilter/ipset/ip_set_bitmap_ip.c
kernel/net/netfilter/ipset/ip_set_bitmap_ipmac.c
kernel/net/netfilter/ipset/ip_set_bitmap_port.c
kernel/net/netfilter/ipset/ip_set_hash_gen.h

index 2cc7f4662b3b6e248a59a3f1483a70d4c5d9de63..ed9f82e7b959d6ff1f3d51926d1f17871887a798 100644 (file)
@@ -99,7 +99,7 @@ struct ip_set_counter {
 
 struct ip_set_comment_rcu {
        struct rcu_head rcu;
-       char str[0];
+       char str[];
 };
 
 struct ip_set_comment {
index 4323ab093c9444150b6a96d4a8da0f0d69ab89ca..c488663985e53ebd847bd406fd66fce167ca41e3 100644 (file)
@@ -48,7 +48,7 @@ struct bitmap_ip {
 #ifdef HAVE_TIMER_SETUP
        struct ip_set *set;     /* attached to this ip_set */
 #endif
-       unsigned char extensions[0]     /* data extensions */
+       unsigned char extensions[     /* data extensions */
                __aligned(__alignof__(u64));
 };
 
index 760bbf500106ca994fafcde9dfd0443bb129c382..3d25d29ce39ff025d4dfc3371da2388f9287fc04 100644 (file)
@@ -52,7 +52,7 @@ struct bitmap_ipmac {
 #ifdef HAVE_TIMER_SETUP
        struct ip_set *set;     /* attached to this ip_set */
 #endif
-       unsigned char extensions[0]     /* MAC + data extensions */
+       unsigned char extensions[     /* MAC + data extensions */
                __aligned(__alignof__(u64));
 };
 
index cf9f26242df2045f20804531290a2e1825220e92..2a570d89ff0559efc1f68672b70b18022fe0731a 100644 (file)
@@ -40,7 +40,7 @@ struct bitmap_port {
 #ifdef HAVE_TIMER_SETUP
        struct ip_set *set;     /* attached to this ip_set */
 #endif
-       unsigned char extensions[0]     /* data extensions */
+       unsigned char extensions[     /* data extensions */
                __aligned(__alignof__(u64));
 };
 
index 44ea2c170b702ace00ba2c374b8616559f952696..cbc3e9877c141139e81dddd9b41efb3beb397335 100644 (file)
@@ -77,7 +77,7 @@ struct hbucket {
        DECLARE_BITMAP(used, AHASH_MAX_TUNED);
        u8 size;                /* size of the array */
        u8 pos;                 /* position of the first free entry */
-       unsigned char value[0]  /* the array of the values */
+       unsigned char value[  /* the array of the values */
                __aligned(__alignof__(u64));
 };
 
@@ -110,7 +110,7 @@ struct htable {
        u8 htable_bits;         /* size of hash table == 2^htable_bits */
        u32 maxelem;            /* Maxelem per region */
        struct ip_set_region *hregion;  /* Region locks and ext sizes */
-       struct hbucket __rcu *bucket[0]; /* hashtable buckets */
+       struct hbucket __rcu *bucket[]; /* hashtable buckets */
 };
 
 #define hbucket(h, i)          ((h)->bucket[i])