Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scsi: aacraid: Replace one-element array with flexible-array member

There is a regular need in the kernel to provide a way to declare having a
dynamically sized set of trailing elements in a structure. Kernel code
should always use “flexible array members”[1] for these cases. The older
style of one-element or zero-length arrays should no longer be used[2].

Refactor the code according to the use of a flexible-array member in struct
aac_raw_io2 instead of one-element array, and use the struct_size() helper.

Also, this helps with the ongoing efforts to enable -Warray-bounds by
fixing the following warnings:

drivers/scsi/aacraid/aachba.c: In function ‘aac_build_sgraw2’:
drivers/scsi/aacraid/aachba.c:3970:18: warning: array subscript 1 is above array bounds of ‘struct sge_ieee1212[1]’ [-Warray-bounds]
3970 | if (rio2->sge[j].length % (i*PAGE_SIZE)) {
| ~~~~~~~~~^~~
drivers/scsi/aacraid/aachba.c:3974:27: warning: array subscript 1 is above array bounds of ‘struct sge_ieee1212[1]’ [-Warray-bounds]
3974 | nseg_new += (rio2->sge[j].length / (i*PAGE_SIZE));
| ~~~~~~~~~^~~
drivers/scsi/aacraid/aachba.c:4011:28: warning: array subscript 1 is above array bounds of ‘struct sge_ieee1212[1]’ [-Warray-bounds]
4011 | for (j = 0; j < rio2->sge[i].length / (pages * PAGE_SIZE); ++j) {
| ~~~~~~~~~^~~
drivers/scsi/aacraid/aachba.c:4012:24: warning: array subscript 1 is above array bounds of ‘struct sge_ieee1212[1]’ [-Warray-bounds]
4012 | addr_low = rio2->sge[i].addrLow + j * pages * PAGE_SIZE;
| ~~~~~~~~~^~~
drivers/scsi/aacraid/aachba.c:4014:33: warning: array subscript 1 is above array bounds of ‘struct sge_ieee1212[1]’ [-Warray-bounds]
4014 | sge[pos].addrHigh = rio2->sge[i].addrHigh;
| ~~~~~~~~~^~~
drivers/scsi/aacraid/aachba.c:4015:28: warning: array subscript 1 is above array bounds of ‘struct sge_ieee1212[1]’ [-Warray-bounds]
4015 | if (addr_low < rio2->sge[i].addrLow)
| ~~~~~~~~~^~~

[1] https://en.wikipedia.org/wiki/Flexible_array_member
[2] https://www.kernel.org/doc/html/v5.9/process/deprecated.html#zero-length-and-one-element-arrays

Link: https://github.com/KSPP/linux/issues/79
Link: https://github.com/KSPP/linux/issues/109
Link: https://lore.kernel.org/lkml/60414244.ur4%2FkI+fBF1ohKZs%25lkp@intel.com/
Link: https://lore.kernel.org/r/20210421185611.GA105224@embeddedor
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Build-tested-by: kernel test robot <lkp@intel.com>

authored by

Gustavo A. R. Silva and committed by
Martin K. Petersen
39107e85 aef80fd1

+6 -6
+5 -5
drivers/scsi/aacraid/aachba.c
··· 1235 1235 if (ret < 0) 1236 1236 return ret; 1237 1237 command = ContainerRawIo2; 1238 - fibsize = sizeof(struct aac_raw_io2) + 1239 - ((le32_to_cpu(readcmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212)); 1238 + fibsize = struct_size(readcmd2, sge, 1239 + le32_to_cpu(readcmd2->sgeCnt)); 1240 1240 } else { 1241 1241 struct aac_raw_io *readcmd; 1242 1242 readcmd = (struct aac_raw_io *) fib_data(fib); ··· 1366 1366 if (ret < 0) 1367 1367 return ret; 1368 1368 command = ContainerRawIo2; 1369 - fibsize = sizeof(struct aac_raw_io2) + 1370 - ((le32_to_cpu(writecmd2->sgeCnt)-1) * sizeof(struct sge_ieee1212)); 1369 + fibsize = struct_size(writecmd2, sge, 1370 + le32_to_cpu(writecmd2->sgeCnt)); 1371 1371 } else { 1372 1372 struct aac_raw_io *writecmd; 1373 1373 writecmd = (struct aac_raw_io *) fib_data(fib); ··· 3998 3998 if (aac_convert_sgl == 0) 3999 3999 return 0; 4000 4000 4001 - sge = kmalloc_array(nseg_new, sizeof(struct sge_ieee1212), GFP_ATOMIC); 4001 + sge = kmalloc_array(nseg_new, sizeof(*sge), GFP_ATOMIC); 4002 4002 if (sge == NULL) 4003 4003 return -ENOMEM; 4004 4004
+1 -1
drivers/scsi/aacraid/aacraid.h
··· 1929 1929 u8 bpComplete; /* reserved for F/W use */ 1930 1930 u8 sgeFirstIndex; /* reserved for F/W use */ 1931 1931 u8 unused[4]; 1932 - struct sge_ieee1212 sge[1]; 1932 + struct sge_ieee1212 sge[]; 1933 1933 }; 1934 1934 1935 1935 #define CT_FLUSH_CACHE 129