Skip to content

Commit 661ea25

Browse files
GustavoARSilvaLi Yang
authored andcommitted
soc: fsl: qe: Replace one-element array and use struct_size() helper
The current codebase makes use of one-element arrays in the following form: struct something { int length; u8 data[1]; }; struct something *instance; instance = kmalloc(sizeof(*instance) + size, GFP_KERNEL); instance->length = size; memcpy(instance->data, source, size); 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. So, replace the one-element array with a flexible-array member. Also, make use of the new struct_size() helper to properly calculate the size of struct qe_firmware. This issue was found with the help of Coccinelle and, audited and fixed _manually_. [1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html [2] KSPP#21 [3] commit 7649773 ("cxgb3/l2t: Fix undefined behaviour") Signed-off-by: Gustavo A. R. Silva <[email protected]> Reviewed-by: Qiang Zhao <[email protected]> Signed-off-by: Li Yang <[email protected]>
1 parent d3e8198 commit 661ea25

File tree

2 files changed

+3
-3
lines changed
  • drivers/soc/fsl/qe
  • include/soc/fsl/qe

2 files changed

+3
-3
lines changed

drivers/soc/fsl/qe/qe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ int qe_upload_firmware(const struct qe_firmware *firmware)
448448
unsigned int i;
449449
unsigned int j;
450450
u32 crc;
451-
size_t calc_size = sizeof(struct qe_firmware);
451+
size_t calc_size;
452452
size_t length;
453453
const struct qe_header *hdr;
454454

@@ -480,7 +480,7 @@ int qe_upload_firmware(const struct qe_firmware *firmware)
480480
}
481481

482482
/* Validate the length and check if there's a CRC */
483-
calc_size += (firmware->count - 1) * sizeof(struct qe_microcode);
483+
calc_size = struct_size(firmware, microcode, firmware->count);
484484

485485
for (i = 0; i < firmware->count; i++)
486486
/*

include/soc/fsl/qe/qe.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ struct qe_firmware {
307307
u8 revision; /* The microcode version revision */
308308
u8 padding; /* Reserved, for alignment */
309309
u8 reserved[4]; /* Reserved, for future expansion */
310-
} __attribute__ ((packed)) microcode[1];
310+
} __packed microcode[];
311311
/* All microcode binaries should be located here */
312312
/* CRC32 should be located here, after the microcode binaries */
313313
} __attribute__ ((packed));

0 commit comments

Comments
 (0)