Skip to contentSkip to conversation

Bluetooth: hci_event: Use struct_size() helper

PATCH In review Gustavo A. R. Silva 3 participants 3 messages Feb 8, 2019 – Feb 18, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Bluetooth: hci_event: 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(*ev) + ev->num_hndl * sizeof(struct hci_comp_pkts_info)

to :

struct_size(ev, handles, ev->num_hndl)

This code was detected with the help of Coccinelle.

---
 net/bluetooth/hci_event.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/bluetooth/hci_event.c +4 -4
index ac2826ce162b..609fd6871c5a 100644
@@ -3556,8 +3556,8 @@ static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		return;
 	}
 
-	if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
-	    ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
+	if (skb->len < sizeof(*ev) ||
+	    skb->len < struct_size(ev, handles, ev->num_hndl)) {
 		BT_DBG("%s bad parameters", hdev->name);
 		return;
 	}
@@ -3644,8 +3644,8 @@ static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		return;
 	}
 
-	if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
-	    ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
+	if (skb->len < sizeof(*ev) ||
+	    skb->len < struct_size(ev, handles, ev->num_hndl)) {
 		BT_DBG("%s bad parameters", hdev->name);
 		return;
 	}
Signature · 1 line
2.20.1
#1 lore raw
Quoted 6 lines · Gustavo A. R. Silva

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:

Quoted 7 lines

sizeof(*ev) + ev->num_hndl * sizeof(struct hci_comp_pkts_info)

to :

struct_size(ev, handles, ev->num_hndl)

This code was detected with the help of Coccinelle.

[]

net/bluetooth/hci_event.c

Quoted 1 line
net/bluetooth/hci_event.c +0 -0

[]

Quoted 12 lines
net/bluetooth/hci_event.c +2 -2
@@ -3556,8 +3556,8 @@ static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		return;
 	}
 
-	if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
-	    ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
+	if (skb->len < sizeof(*ev) ||
+	    skb->len < struct_size(ev, handles, ev->num_hndl)) {
 		BT_DBG("%s bad parameters", hdev->name);
 		return;
 	}

net/bluetooth/hci_event.c static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb) · line 3648

Quoted 7 lines
net/bluetooth/hci_event.c +2 -2
@@ -3644,8 +3644,8 @@ static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
 		return;
 	}
 
-	if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
-	    ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
+	if (skb->len < sizeof(*ev) ||
+	    skb->len < struct_size(ev, handles, ev->num_hndl)) {

Unless these are in a real fast path where skb->len < sizeof(*ev) is pretty likely, it seems a bit redundant as the multiplication is pretty cheap and num_hndl is unsigned (actually __u8)

This could/should be simply

	if (skb->len < struct_size(...))
maintainer
#2 lore raw
In reply to Gustavo A. R. Silva

Hi Gustavo,

Quoted 10 lines

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(*ev) + ev->num_hndl * sizeof(struct hci_comp_pkts_info)

to :

Quoted 8 lines

struct_size(ev, handles, ev->num_hndl)

This code was detected with the help of Coccinelle.

---
net/bluetooth/hci_event.c | 8 ++++----

1 file changed, 4 insertions(+), 4 deletions(-)

patch has been applied to bluetooth-next tree.

Regards

Marcel