Replace one-element array with flexible-array member
Subsystems (2)
cover Replace one-element array with flexible-array member
Hi!
This series aims to replace one-element arrays with flexible-array members in drivers/scsi/hptiop.h
This helps with the ongoing efforts to tighten the FORTIFY_SOURCE routines on memcpy() and help us make progress towards globally enabling -fstrict-flex-arrays=3 [1].
Gustavo A. R. Silva (2):
scsi: hptiop: Replace one-element array with flexible-array member
scsi: hptiop: Use struct_size() helper in code related to struct
hpt_iop_request_scsi_command
drivers/scsi/hptiop.c | 9 +++------ drivers/scsi/hptiop.h | 2 +- 2 files changed, 4 insertions(+), 7 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/2 scsi: hptiop: Replace one-element array with flexible-array member
One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element array with flexible-array member in struct hpt_iop_request_scsi_command and refactor the rest of the code, accordingly.
The following pieces of code suggest that the one element of array sg_list in struct hpt_iop_request_scsi_command is not taken into account when calculating the total size for both struct hpt_iop_request_scsi_command and the maximum number of elements sg_list will contain:
1047 req->header.size = cpu_to_le32(
1048 sizeof(struct hpt_iop_request_scsi_command)
1049 - sizeof(struct hpt_iopsg)
1050 + sg_count * sizeof(struct hpt_iopsg));
1400 req_size = sizeof(struct hpt_iop_request_scsi_command) 1401 + sizeof(struct hpt_iopsg) * (hba->max_sg_descriptors - 1);
So it's safe to replace the one-element array with a flexible-array member and update the code above, accordingly: now we don't need to subtract sizeof(struct hpt_iopsg) from sizeof(struct hpt_iop_request_scsi_command) because this is implicitly done by the flex-array transformation.
--- drivers/scsi/hptiop.c | 3 +-- drivers/scsi/hptiop.h | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/scsi/hptiop.c +1 -2
@@ -1046,7 +1046,6 @@ static int hptiop_queuecommand_lck(struct scsi_cmnd *scp) req->lun = scp->device->lun; req->header.size =cpu_to_le32 (sizeof (struct hpt_iop_request_scsi_command ) - -sizeof (struct hpt_iopsg ) + sg_count *sizeof (struct hpt_iopsg ));memcpy (req->cdb, scp->cmnd,sizeof (req->cdb));
@@ -1398,7 +1397,7 @@ static int hptiop_probe(struct pci_dev *pcidev, const struct pci_device_id *id) host->max_cmd_len =16 ; req_size =sizeof (struct hpt_iop_request_scsi_command ) - +sizeof (struct hpt_iopsg ) * (hba->max_sg_descriptors -1 ); + +sizeof (struct hpt_iopsg ) * hba->max_sg_descriptors;if ((req_size &0x1f ) !=0 ) req_size = (req_size +0x1f ) & ~0x1f ;
drivers/scsi/hptiop.h +1 -1
@@ -228,7 +228,7 @@ struct hpt_iop_request_scsi_command {u8 pad1;u8 cdb[16 ];__le32 dataxfer_length; -struct hpt_iopsg sg_list[1 ]; +struct hpt_iopsg sg_list[]; };struct hpt_iop_request_ioctl_command {
Signature · 1 line
2.34.1
Re: [PATCH 1/2][next] scsi: hptiop: Replace one-element array with flexible-array member
Quoted 10 lines · Gustavo A. R. Silva
One-element arrays are deprecated, and we are replacing them with flexible array members instead. So, replace one-element array with flexible-array member in struct
hpt_iop_request_scsi_commandand refactor the rest of the code, accordingly.The following pieces of code suggest that the one element of array
sg_listin structhpt_iop_request_scsi_commandis not taken into account when calculating the total size for both structhpt_iop_request_scsi_commandand the maximum number of elementssg_listwill contain:
Quoted 6 lines
1047
req->header.size= cpu_to_le32(
1048sizeof(struct hpt_iop_request_scsi_command)
1049 -sizeof(struct hpt_iopsg)
1050 +sg_count*sizeof(struct hpt_iopsg));1400
req_size=sizeof(struct hpt_iop_request_scsi_command)1401 +sizeof(struct hpt_iopsg)* (hba->max_sg_descriptors- 1);
Accidentally merge line above ("1401" should start a new line).
Quoted 4 lines
So it's safe to replace the one-element array with a flexible-array member and update the code above, accordingly: now we don't need to subtract
sizeof(struct hpt_iopsg)fromsizeof(struct hpt_iop_request_scsi_command)because this is implicitly done by the flex-array transformation.
The only binary output change I see is from the line numbers changing from the patch, as the argument to __might_sleep() is adjusted:
...
│ call d1 <hptiop_reset+0x74> │ R_X86_64_PLT32 __x86_indirect_thunk_rax-0x4 │ - mov $0x434,%esi │ + mov $0x433,%esi │ mov $0x0,%rdi │ R_X86_64_32S .rodata.str1.1 │ call e2 <hptiop_reset+0x85> │ R_X86_64_PLT32 __might_sleep-0x4
...
So this looks good!
Signature · 1 line
Kees Cook
PATCH 2/2 scsi: hptiop: Use struct_size() helper in code related to struct hpt_iop_request_scsi_command
In reply to Gustavo A. R. SilvaPrefer struct_size() over open-coded versions of idiom:
sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
where count is the max number of items the flexible array is supposed to contain.
--- drivers/scsi/hptiop.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/scsi/hptiop.c +3 -5
@@ -1044,9 +1044,7 @@ static int hptiop_queuecommand_lck(struct scsi_cmnd *scp) req->channel = scp->device->channel; req->target = scp->device->id; req->lun = scp->device->lun; - req->header.size =cpu_to_le32 ( -sizeof (struct hpt_iop_request_scsi_command ) - + sg_count *sizeof (struct hpt_iopsg )); + req->header.size =cpu_to_le32 (struct_size (req, sg_list, sg_count));memcpy (req->cdb, scp->cmnd,sizeof (req->cdb)); hba->ops->post_req (hba, _req);
@@ -1396,8 +1394,8 @@ static int hptiop_probe(struct pci_dev *pcidev, const struct pci_device_id *id) host->cmd_per_lun =le32_to_cpu (iop_config.max_requests); host->max_cmd_len =16 ; - req_size =sizeof (struct hpt_iop_request_scsi_command ) - +sizeof (struct hpt_iopsg ) * hba->max_sg_descriptors; + req_size =struct_size ((struct hpt_iop_request_scsi_command *)0 , + sg_list, hba->max_sg_descriptors);if ((req_size &0x1f ) !=0 ) req_size = (req_size +0x1f ) & ~0x1f ;
Signature · 1 line
2.34.1
Re: [PATCH 2/2][next] scsi: hptiop: Use struct_size() helper in code related to struct hpt_iop_request_scsi_command
Quoted 9 lines · Gustavo A. R. Silva
Prefer
struct_size()over open-coded versions of idiom:sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
where count is the max number of items the flexible array is supposed to contain.
Looks correct; thanks!
Signature · 1 line
Kees Cook
Martin K. Petersen
AppliedGustavo,
Quoted 2 lines
This series aims to replace one-element arrays with flexible-array members in
drivers/scsi/hptiop.h
Applied to 6.1/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering