Commit 5bec1fb8 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by Marcel Holtmann

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: default avatarGustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: default avatarMarcel Holtmann <marcel@holtmann.org>
parent 7f09d5a6
...@@ -2301,8 +2301,7 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, ...@@ -2301,8 +2301,7 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
MGMT_STATUS_INVALID_PARAMS); MGMT_STATUS_INVALID_PARAMS);
} }
expected_len = sizeof(*cp) + key_count * expected_len = struct_size(cp, keys, key_count);
sizeof(struct mgmt_link_key_info);
if (expected_len != len) { if (expected_len != len) {
bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
expected_len, len); expected_len, len);
...@@ -5030,7 +5029,7 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, ...@@ -5030,7 +5029,7 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
MGMT_STATUS_INVALID_PARAMS); MGMT_STATUS_INVALID_PARAMS);
} }
expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); expected_len = struct_size(cp, irks, irk_count);
if (expected_len != len) { if (expected_len != len) {
bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
expected_len, len); expected_len, len);
...@@ -5112,8 +5111,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, ...@@ -5112,8 +5111,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
MGMT_STATUS_INVALID_PARAMS); MGMT_STATUS_INVALID_PARAMS);
} }
expected_len = sizeof(*cp) + key_count * expected_len = struct_size(cp, keys, key_count);
sizeof(struct mgmt_ltk_info);
if (expected_len != len) { if (expected_len != len) {
bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
expected_len, len); expected_len, len);
...@@ -5847,8 +5845,7 @@ static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, ...@@ -5847,8 +5845,7 @@ static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
MGMT_STATUS_INVALID_PARAMS); MGMT_STATUS_INVALID_PARAMS);
} }
expected_len = sizeof(*cp) + param_count * expected_len = struct_size(cp, params, param_count);
sizeof(struct mgmt_conn_param);
if (expected_len != len) { if (expected_len != len) {
bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
expected_len, len); expected_len, len);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment