Skip to contentSkip to conversation

staging: rtl8192u: ieee80211: replace kmalloc/memset with kzalloc

PATCH In review Elena Afanasova 3 participants 3 messages Oct 25, 2020 – Oct 26, 2020 Staging

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 staging: rtl8192u: ieee80211: replace kmalloc/memset with kzalloc

Replace kmalloc + memset 0 by a call to kzalloc which zeroes memory too. Found with Coccinelle.

---
Changes in v3:
- use array_size()
Changes in v2:
- correct the subject line
- correct the commit message

 drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c +1 -4
index 63a561ab4a76..53ce97aae206 100644
@@ -227,13 +227,10 @@ static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
 {
 	struct ieee80211_txb *txb;
 	int i;
-	txb = kmalloc(
-		sizeof(struct ieee80211_txb) + (sizeof(u8 *) * nr_frags),
-		gfp_mask);
+	txb = kzalloc(sizeof(*txb) + array_size(sizeof(u8 *), nr_frags), gfp_mask);
 	if (!txb)
 		return NULL;
 
-	memset(txb, 0, sizeof(struct ieee80211_txb));
 	txb->nr_frags = nr_frags;
 	txb->frag_size = __cpu_to_le16(txb_size);
 
Signature · 1 line
2.25.1
#1 lore raw
Quoted 19 lines · Elena Afanasova

Replace kmalloc + memset 0 by a call to kzalloc which zeroes memory too. Found with Coccinelle.

---
Changes in v3:
- use array_size()
Changes in v2:
- correct the subject line
- correct the commit message

 drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c +0 -0
index 63a561ab4a76..53ce97aae206 100644

drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size, · line 230

Quoted 7 lines
drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c +1 -3
@@ -227,13 +227,10 @@ static struct ieee80211_txb *ieee80211_alloc_txb(int nr_frags, int txb_size,
 {
 	struct ieee80211_txb *txb;
 	int i;
-	txb = kmalloc(
-		sizeof(struct ieee80211_txb) + (sizeof(u8 *) * nr_frags),
-		gfp_mask);
+	txb = kzalloc(sizeof(*txb) + array_size(sizeof(u8 *), nr_frags), gfp_mask);

There is no need to exceed 80 characters here.

julia

Quoted 16 lines
drivers/staging/rtl8192u/ieee80211/ieee80211_tx.c +0 -2
 	if (!txb)
 		return NULL;
 
-	memset(txb, 0, sizeof(struct ieee80211_txb));
 	txb->nr_frags = nr_frags;
 	txb->frag_size = __cpu_to_le16(txb_size);
 
--

2.25.1

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/43e637becf35f98a952c38ee1a5b690798c62c75.camel%40gmail.com.

#2 lore raw
In reply to Elena Afanasova
Quoted 7 lines · Elena Afanasova

Replace kmalloc + memset 0 by a call to kzalloc which zeroes memory too. Found with Coccinelle.

---
Changes in v3:
- use array_size()

struct_size() is more correct than array_size(). If there were really a danger from array overflows then array_size() would not prevent it 100%.

There should probably be a static checker warning for when people do "expr + array_size()". If you're looking for something to write, that's an idea you could implement with Coccinelle or Smatch.

regards,
dan carpenter