Skip to contentSkip to conversation

kernel: relay: Improve exception handling in relay_create_buf()

PATCH In review Markus Elfring 2 participants 2 messages Dec 31, 2023 – Jan 2, 2024

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

PATCH 1/1 kernel: relay: Improve exception handling in relay_create_buf()

From: Markus Elfring <[email protected]>
Date: Sun, 31 Dec 2023 10:26:25 +0100

The kfree() function was called in one case by
the relay_create_buf() function during error handling even if the passed data structure member contained a null pointer. This issue was detected by using the Coccinelle software.

Thus use another label.

---
 kernel/relay.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/relay.c +3 -2
index a8e90e98bf2c..b8c06305213d 100644
@@ -161,14 +161,15 @@ static struct rchan_buf *relay_create_buf(struct rchan *chan)
 
 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
 	if (!buf->start)
-		goto free_buf;
+		goto free_padding;
 
 	buf->chan = chan;
 	kref_get(&buf->chan->kref);
 	return buf;
 
-free_buf:
+free_padding:
 	kfree(buf->padding);
+free_buf:
 	kfree(buf);
 	return NULL;
 }

--
2.43.0

maintainer
#1 lore raw
Quoted 22 lines · On Sun, 31 Dec 2023 10:38:10 +0100 Markus Elfring <[email protected]> wrote:

From: Markus Elfring <[email protected]>
Date: Sun, 31 Dec 2023 10:26:25 +0100

The kfree() function was called in one case by
the relay_create_buf() function during error handling even if the passed data structure member contained a null pointer. This issue was detected by using the Coccinelle software.

Thus use another label.

...

kernel/relay.c +1 -1
@@ -161,14 +161,15 @@ static struct rchan_buf *relay_create_buf(struct rchan *chan)
 
 	buf->start = relay_alloc_buf(buf, &chan->alloc_size);
 	if (!buf->start)
-		goto free_buf;
+		goto free_padding;
 
 	buf->chan = chan;

kernel/relay.c static struct rchan_buf *relay_create_buf(struct rchan *chan) · line 175

Quoted 10 lines
kernel/relay.c +2 -1
 	kref_get(&buf->chan->kref);
 	return buf;
 
-free_buf:
+free_padding:
 	kfree(buf->padding);
+free_buf:
 	kfree(buf);
 	return NULL;
 }

kfree(NULL) is an OK thing to do, and is a well-known pattern. Can we arrange for Coccinelle to understand this?