Skip to contentSkip to conversation

nfc: st21nfca: Check for devm_kzalloc() failure

PATCH In review Fabio Estevam 2 participants 4 messages Mar 24, 2018 – Jun 4, 2018 NFC

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 nfc: st21nfca: Check for devm_kzalloc() failure

From: Fabio Estevam <[email protected]>

devm_kzalloc() may fail, so we should better check for error and propagate the error in the case of allocation failure.

This avoids a potential NULL pointer dereference later on.

---
 drivers/nfc/st21nfca/se.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nfc/st21nfca/se.c +2 -0
index 4bed9e84..fd967a3 100644
@@ -328,6 +328,8 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
 
 		transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
 						   skb->len - 2, GFP_KERNEL);
+		if (!transaction)
+			return -ENOMEM;
 
 		transaction->aid_len = skb->data[1];
 		memcpy(transaction->aid, &skb->data[2],
Signature · 1 line
2.7.4
#1 lore raw

PATCH 2/2 nfc: st21nfca: Remove unnecessary devm_kzalloc() cast

From: Fabio Estevam <[email protected]>

There is no need to use cast for the returned value from memory allocation functions, so remove the unnecessary cast.

Detected via Coccinelle script:
scripts/coccinelle/api/alloc/alloc_cast.cocci.

---
 drivers/nfc/st21nfca/se.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/nfc/st21nfca/se.c +1 -2
index fd967a3..5b63549 100644
@@ -326,8 +326,7 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
 		    skb->data[0] != NFC_EVT_TRANSACTION_AID_TAG)
 			return -EPROTO;
 
-		transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
-						   skb->len - 2, GFP_KERNEL);
+		transaction = devm_kzalloc(dev, skb->len - 2, GFP_KERNEL);
 		if (!transaction)
 			return -ENOMEM;
 
Signature · 1 line
2.7.4
#2 lore raw
In reply to Fabio Estevam

Hi Samuel,

Maybe this patch series got forgotten?

Quoted 28 lines · Fabio Estevam

From: Fabio Estevam <[email protected]>

devm_kzalloc() may fail, so we should better check for error and propagate the error in the case of allocation failure.

This avoids a potential NULL pointer dereference later on.

---
 drivers/nfc/st21nfca/se.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nfc/st21nfca/se.c +2 -1
index 4bed9e84..fd967a3 100644
@@ -328,6 +328,8 @@ int st21nfca_connectivity_event_received(struct nfc_hci_dev *hdev, u8 host,
 
                transaction = (struct nfc_evt_transaction *)devm_kzalloc(dev,
                                                   skb->len - 2, GFP_KERNEL);
+               if (!transaction)
+                       return -ENOMEM;
 
                transaction->aid_len = skb->data[1];
                memcpy(transaction->aid, &skb->data[2],
--

2.7.4

#3 lore raw

Hi Fabio,

Quoted 3 lines · Fabio Estevam

Hi Samuel,

Maybe this patch series got forgotten?

Both patches applied to nfc-next, sorry for the lag.

Cheers,
Samuel.