Linux Input/HID development
 help / color / mirror / Atom feed
From: Rafael Passos <rafael@rcpassos.me>
To: david@readahead.eu, jikos@kernel.org, bentiss@kernel.org
Cc: Shuah Khan <skhan@linuxfoundation.org>,
	Brigham Campbell <me@brighamcampbell.com>,
	Jori Koolstra <jkoolstra@xs4all.nl>,
	Rafael Passos <rafael@rcpassos.me>,
	linux-input@vger.kernel.org
Subject: [PATCH 3/3] HID: wiimote: use scoped cleanup in led creation
Date: Fri, 10 Jul 2026 12:32:11 -0300	[thread overview]
Message-ID: <20260710153456.2093889-4-rafael@rcpassos.me> (raw)
In-Reply-To: <20260710153456.2093889-1-rafael@rcpassos.me>

Cleanup code in led probe function, using the modern scoped cleanup.
This prevents mistakes in future changes to this function.

Signed-off-by: Rafael Passos <rafael@rcpassos.me>
---
 drivers/hid/hid-wiimote-modules.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/drivers/hid/hid-wiimote-modules.c b/drivers/hid/hid-wiimote-modules.c
index f42f9aa7b2d4..7c305099c82d 100644
--- a/drivers/hid/hid-wiimote-modules.c
+++ b/drivers/hid/hid-wiimote-modules.c
@@ -340,11 +340,11 @@ static int wiimod_led_probe(const struct wiimod_ops *ops,
 {
 	struct device *dev = &wdata->hdev->dev;
 	size_t namesz = strlen(dev_name(dev)) + 9;
-	struct led_classdev *led;
 	char *name;
 	int ret;
 
-	led = kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
+	struct led_classdev *led __free(kfree) =
+		kzalloc(sizeof(struct led_classdev) + namesz, GFP_KERNEL);
 	if (!led)
 		return -ENOMEM;
 
@@ -358,8 +358,13 @@ static int wiimod_led_probe(const struct wiimod_ops *ops,
 
 	wdata->leds[ops->arg] = led;
 	ret = led_classdev_register(dev, led);
-	if (ret)
-		goto err_free;
+	if (ret) {
+		wdata->leds[ops->arg] = NULL;
+		return ret;
+	}
+
+	/* Ownership transferred to wdata – prevent cleanup from freeing */
+	led = NULL;
 
 	/* enable LED1 to stop initial LED-blinking */
 	if (ops->arg == 0) {
@@ -368,11 +373,6 @@ static int wiimod_led_probe(const struct wiimod_ops *ops,
 	}
 
 	return 0;
-
-err_free:
-	wdata->leds[ops->arg] = NULL;
-	kfree(led);
-	return ret;
 }
 
 static void wiimod_led_remove(const struct wiimod_ops *ops,
-- 
2.53.0


  parent reply	other threads:[~2026-07-10 15:35 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-10 15:32 [PATCH 0/3] HID: wiimote: new LED behavior on connect, and scoped guards Rafael Passos
2026-07-10 15:32 ` [PATCH 1/3] HID: wiimote: turn on the LEDs indicating the controller id Rafael Passos
2026-07-10 15:46   ` sashiko-bot
2026-07-10 15:32 ` [PATCH 2/3] HID: wiimote: replace spinlock pairs with scoped_guard Rafael Passos
2026-07-10 15:47   ` sashiko-bot
2026-07-10 15:32 ` Rafael Passos [this message]
2026-07-10 15:44   ` [PATCH 3/3] HID: wiimote: use scoped cleanup in led creation sashiko-bot
2026-07-10 19:35 ` [PATCH 0/3] HID: wiimote: new LED behavior on connect, and scoped guards Rafael Passos
2026-07-13  9:28   ` David Rheinsberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260710153456.2093889-4-rafael@rcpassos.me \
    --to=rafael@rcpassos.me \
    --cc=bentiss@kernel.org \
    --cc=david@readahead.eu \
    --cc=jikos@kernel.org \
    --cc=jkoolstra@xs4all.nl \
    --cc=linux-input@vger.kernel.org \
    --cc=me@brighamcampbell.com \
    --cc=skhan@linuxfoundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox