Skip to content

Commit 5bec1fb

Browse files
GustavoARSilvaholtmann
authored andcommitted
Bluetooth: Use struct_size() helper
One of the more common cases of allocation size calculations is finding the size of a structure that has a zero-sized array at the end, along with memory for some number of elements for that array. For example: struct foo { int stuff; struct boo entry[]; }; size = sizeof(struct foo) + count * sizeof(struct boo); Instead of leaving these open-coded and prone to type mistakes, we can now use the new struct_size() helper: size = struct_size(instance, entry, count); 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 7f09d5a commit 5bec1fb

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

net/bluetooth/mgmt.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,8 +2301,7 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
23012301
MGMT_STATUS_INVALID_PARAMS);
23022302
}
23032303

2304-
expected_len = sizeof(*cp) + key_count *
2305-
sizeof(struct mgmt_link_key_info);
2304+
expected_len = struct_size(cp, keys, key_count);
23062305
if (expected_len != len) {
23072306
bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
23082307
expected_len, len);
@@ -5030,7 +5029,7 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
50305029
MGMT_STATUS_INVALID_PARAMS);
50315030
}
50325031

5033-
expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5032+
expected_len = struct_size(cp, irks, irk_count);
50345033
if (expected_len != len) {
50355034
bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
50365035
expected_len, len);
@@ -5112,8 +5111,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
51125111
MGMT_STATUS_INVALID_PARAMS);
51135112
}
51145113

5115-
expected_len = sizeof(*cp) + key_count *
5116-
sizeof(struct mgmt_ltk_info);
5114+
expected_len = struct_size(cp, keys, key_count);
51175115
if (expected_len != len) {
51185116
bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
51195117
expected_len, len);
@@ -5847,8 +5845,7 @@ static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
58475845
MGMT_STATUS_INVALID_PARAMS);
58485846
}
58495847

5850-
expected_len = sizeof(*cp) + param_count *
5851-
sizeof(struct mgmt_conn_param);
5848+
expected_len = struct_size(cp, params, param_count);
58525849
if (expected_len != len) {
58535850
bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
58545851
expected_len, len);

0 commit comments

Comments
 (0)