Skip to contentSkip to conversation

Bluetooth: Remove unneeded val variable when setting SSP

PATCH In review Marcel Holtmann 4 participants 6 messages Oct 10, 2013

Subsystems (2)
lore
Share & tools
lore

Marcel Holtmann

author maintainer
#0 lore raw

PATCH 1/2 Bluetooth: Remove unneeded val variable when setting SSP

The variable val in the set_ssp() function of the management interface is not needed. Just use cp->val directly since its input values have already been validated.

---
 net/bluetooth/mgmt.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
net/bluetooth/mgmt.c +3 -5
index a9d7506..2fb4d35 100644
@@ -1290,7 +1290,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 {
 	struct mgmt_mode *cp = data;
 	struct pending_cmd *cmd;
-	u8 val, status;
+	u8 status;
 	int err;
 
 	BT_DBG("request for %s", hdev->name);
@@ -1309,8 +1309,6 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	val = !!cp->val;
-
 	if (!hdev_is_powered(hdev)) {
 		bool changed = false;
 
@@ -1335,7 +1333,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 		goto failed;
 	}
 
-	if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) {
+	if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
 		err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
 		goto failed;
 	}
@@ -1346,7 +1344,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 		goto failed;
 	}
 
-	err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(val), &val);
+	err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
 	if (err < 0) {
 		mgmt_pending_remove(cmd);
 		goto failed;
Signature · 1 line
1.8.3.1
#1 lore raw

Hi Marcel,

Quoted 8 lines · Marcel Holtmann

The variable val in the set_ssp() function of the management interface is not needed. Just use cp->val directly since its input values have already been validated.

---
 net/bluetooth/mgmt.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

Both patches have been applied to bluetooth-next. Thanks.

Johan

#2 lore raw
In reply to Marcel Holtmann

Hi Marcel,

net/bluetooth/mgmt.c static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) · line 1336

Quoted 6 lines · Marcel Holtmann

The variable val in the set_ssp() function of the management interface is not needed. Just use cp->val directly since its input values have already been validated.
[...]

net/bluetooth/mgmt.c +1 -1
-       if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) {
+       if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {

In this case, the "!!" trick is unnecessary as the only allowed values are 0x00 and 0x01 (I believe this has been removed in other similar cases).

Best Regards,

Signature · 3 lines
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil
author maintainer
#3 lore raw

Hi Anderson,

Quoted 10 lines

net/bluetooth/mgmt.c static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) · line 1336

Quoted 6 lines

The variable val in the set_ssp() function of the management interface is not needed. Just use cp->val directly since its input values have already been validated.
[...]

net/bluetooth/mgmt.c +1 -1
-       if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) {
+       if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {

In this case, the "!!" trick is unnecessary as the only allowed values are 0x00 and 0x01 (I believe this has been removed in other similar cases).

it has not been removed. That is why I changed it and made it similar to the other case.

Regards

Marcel

#4 lore raw
In reply to Marcel Holtmann

Hi Marcel,

Quoted 41 lines · Marcel Holtmann

The variable val in the set_ssp() function of the management interface is not needed. Just use cp->val directly since its input values have already been validated.

---
 net/bluetooth/mgmt.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
net/bluetooth/mgmt.c +2 -4
index a9d7506..2fb4d35 100644
@@ -1290,7 +1290,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 {
 	struct mgmt_mode *cp = data;
 	struct pending_cmd *cmd;
-	u8 val, status;
+	u8 status;
 	int err;
 
 	BT_DBG("request for %s", hdev->name);
@@ -1309,8 +1309,6 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	val = !!cp->val;
-
 	if (!hdev_is_powered(hdev)) {
 		bool changed = false;
 
@@ -1335,7 +1333,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 		goto failed;
 	}
 
-	if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) {
+	if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
 		err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
 		goto failed;
 	}

net/bluetooth/mgmt.c static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) · line 1347

Quoted 5 lines
net/bluetooth/mgmt.c +1 -1
@@ -1346,7 +1344,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 		goto failed;
 	}
 
-	err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(val), &val);
+	err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);

I think sizeof is read better then magic number

Best regards
Andrei Emeltchenko

author maintainer
#5 lore raw

Hi Andrei,

Quoted 44 lines
Quoted 44 lines

The variable val in the set_ssp() function of the management interface is not needed. Just use cp->val directly since its input values have already been validated.

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

1 file changed, 3 insertions(+), 5 deletions(-)
net/bluetooth/mgmt.c +2 -4
index a9d7506..2fb4d35 100644
@@ -1290,7 +1290,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 {
 	struct mgmt_mode *cp = data;
 	struct pending_cmd *cmd;
-	u8 val, status;
+	u8 status;
 	int err;
 
 	BT_DBG("request for %s", hdev->name);
@@ -1309,8 +1309,6 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 
 	hci_dev_lock(hdev);
 
-	val = !!cp->val;
-
 	if (!hdev_is_powered(hdev)) {
 		bool changed = false;
 
@@ -1335,7 +1333,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 		goto failed;
 	}
 
-	if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) {
+	if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
 		err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
 		goto failed;
 	}
@@ -1346,7 +1344,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
 		goto failed;
 	}
 
Quoted 4 lines

net/bluetooth/mgmt.c static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) · line 1347

Quoted 2 lines
net/bluetooth/mgmt.c +1 -1
-	err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(val), &val);
+	err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);

I think sizeof is read better then magic number

if you look at the whole code base, for cases of single u8, we use the "magic" number 1 a lot to keep the lines shorter.

Regards

Marcel