Skip to content

Commit

Permalink
NFC: st21nfca: Fix potential memory leak
Browse files Browse the repository at this point in the history
If all bits of 'dev_mask' are already set, there is a memory leak because
'info' should be freed before returning.

While fixing it, 'return -ENOMEM' directly if the first kzalloc fails.
This makes the code more readable.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
  • Loading branch information
Christophe JAILLET authored and Samuel Ortiz committed Apr 1, 2017
1 parent ca42fb9 commit 8f79ded
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions drivers/nfc/st21nfca/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,8 @@ int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
unsigned long quirks = 0;

info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
if (!info) {
r = -ENOMEM;
goto err_alloc_hdev;
}
if (!info)
return -ENOMEM;

info->phy_ops = phy_ops;
info->phy_id = phy_id;
Expand All @@ -978,8 +976,10 @@ int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
* persistent info to discriminate 2 identical chips
*/
dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
if (dev_num >= ST21NFCA_NUM_DEVICES)
return -ENODEV;
if (dev_num >= ST21NFCA_NUM_DEVICES) {
r = -ENODEV;
goto err_alloc_hdev;
}

set_bit(dev_num, dev_mask);

Expand Down

0 comments on commit 8f79ded

Please sign in to comment.