Skip to content

Commit b18ae8d

Browse files
GustavoARSilvasnitm
authored andcommitted
dm: 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> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
1 parent bd5c403 commit b18ae8d

9 files changed

Lines changed: 9 additions & 9 deletions

File tree

‎drivers/md/dm-crypt.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ struct crypt_config {
214214
struct mutex bio_alloc_lock;
215215

216216
u8 *authenc_key; /* space for keys in authenc() format (if used) */
217-
u8 key[0];
217+
u8 key[];
218218
};
219219

220220
#define MIN_IOS 64

‎drivers/md/dm-integrity.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct journal_entry {
9292
} s;
9393
__u64 sector;
9494
} u;
95-
commit_id_t last_bytes[0];
95+
commit_id_t last_bytes[];
9696
/* __u8 tag[0]; */
9797
};
9898

‎drivers/md/dm-log-writes.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ struct pending_block {
127127
char *data;
128128
u32 datalen;
129129
struct list_head list;
130-
struct bio_vec vecs[0];
130+
struct bio_vec vecs[];
131131
};
132132

133133
struct per_bio_data {

‎drivers/md/dm-raid.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ struct raid_set {
254254
int mode;
255255
} journal_dev;
256256

257-
struct raid_dev dev[0];
257+
struct raid_dev dev[];
258258
};
259259

260260
static void rs_config_backup(struct raid_set *rs, struct rs_layout *l)

‎drivers/md/dm-raid1.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct mirror_set {
8383
struct work_struct trigger_event;
8484

8585
unsigned nr_mirrors;
86-
struct mirror mirror[0];
86+
struct mirror mirror[];
8787
};
8888

8989
DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(raid1_resync_throttle,

‎drivers/md/dm-stats.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ struct dm_stat {
5656
size_t percpu_alloc_size;
5757
size_t histogram_alloc_size;
5858
struct dm_stat_percpu *stat_percpu[NR_CPUS];
59-
struct dm_stat_shared stat_shared[0];
59+
struct dm_stat_shared stat_shared[];
6060
};
6161

6262
#define STAT_PRECISE_TIMESTAMPS 1

‎drivers/md/dm-stripe.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct stripe_c {
4141
/* Work struct used for triggering events*/
4242
struct work_struct trigger_event;
4343

44-
struct stripe stripe[0];
44+
struct stripe stripe[];
4545
};
4646

4747
/*

‎drivers/md/dm-switch.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ struct switch_ctx {
5353
/*
5454
* Array of dm devices to switch between.
5555
*/
56-
struct switch_path path_list[0];
56+
struct switch_path path_list[];
5757
};
5858

5959
static struct switch_ctx *alloc_switch_ctx(struct dm_target *ti, unsigned nr_paths,

‎drivers/md/persistent-data/dm-btree-internal.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct node_header {
3838

3939
struct btree_node {
4040
struct node_header header;
41-
__le64 keys[0];
41+
__le64 keys[];
4242
} __packed;
4343

4444

0 commit comments

Comments
 (0)