Skip to content

Commit 53ab8e7

Browse files
GustavoARSilvaidryomov
authored andcommitted
libceph, rbd: 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] KSPP/linux#21 [3] commit 7649773 ("cxgb3/l2t: Fix undefined behaviour") Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Reviewed-by: Ilya Dryomov <idryomov@gmail.com> Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
1 parent dffdcd7 commit 53ab8e7

3 files changed

Lines changed: 3 additions & 3 deletions

File tree

‎drivers/block/rbd_types.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct rbd_image_header_ondisk {
9393
__le32 snap_count;
9494
__le32 reserved;
9595
__le64 snap_names_len;
96-
struct rbd_image_snap_ondisk snaps[0];
96+
struct rbd_image_snap_ondisk snaps[];
9797
} __attribute__((packed));
9898

9999

‎include/linux/ceph/mon_client.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct ceph_monmap {
1919
struct ceph_fsid fsid;
2020
u32 epoch;
2121
u32 num_mon;
22-
struct ceph_entity_inst mon_inst[0];
22+
struct ceph_entity_inst mon_inst[];
2323
};
2424

2525
struct ceph_mon_client;

‎include/linux/crush/crush.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct crush_rule_mask {
8787
struct crush_rule {
8888
__u32 len;
8989
struct crush_rule_mask mask;
90-
struct crush_rule_step steps[0];
90+
struct crush_rule_step steps[];
9191
};
9292

9393
#define crush_rule_size(len) (sizeof(struct crush_rule) + \

0 commit comments

Comments
 (0)