Skip to content

Commit 4a67e5d

Browse files
GustavoARSilvaholtmann
authored andcommitted
Bluetooth: mgmt: Use struct_size() helper
Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes, in particular in the context in which this code is being used. So, change the following form: sizeof(*rp) + (sizeof(rp->entry[0]) * count); to : struct_size(rp, entry, count) Notice that, in this case, variable rp_len is not necessary, hence it is removed. This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Marcel Holtmann <[email protected]>
1 parent c14f7e1 commit 4a67e5d

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

net/bluetooth/mgmt.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
474474
{
475475
struct mgmt_rp_read_ext_index_list *rp;
476476
struct hci_dev *d;
477-
size_t rp_len;
478477
u16 count;
479478
int err;
480479

@@ -488,8 +487,7 @@ static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
488487
count++;
489488
}
490489

491-
rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
492-
rp = kmalloc(rp_len, GFP_ATOMIC);
490+
rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC);
493491
if (!rp) {
494492
read_unlock(&hci_dev_list_lock);
495493
return -ENOMEM;
@@ -525,7 +523,6 @@ static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
525523
}
526524

527525
rp->num_controllers = cpu_to_le16(count);
528-
rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
529526

530527
read_unlock(&hci_dev_list_lock);
531528

@@ -538,7 +535,8 @@ static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
538535
hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
539536

540537
err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
541-
MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
538+
MGMT_OP_READ_EXT_INDEX_LIST, 0, rp,
539+
struct_size(rp, entry, count));
542540

543541
kfree(rp);
544542

0 commit comments

Comments
 (0)