]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
HID: input: use __free(kfree) to clean up temporary buffers
authorDmitry Torokhov <dmitry.torokhov@gmail.com>
Sun, 1 Mar 2026 05:05:11 +0000 (21:05 -0800)
committerBenjamin Tissoires <bentiss@kernel.org>
Thu, 19 Mar 2026 14:45:15 +0000 (15:45 +0100)
The __free() cleanup automatically releases given resource when leaving
the scope, so use it to make the code less cluttered with error
handling.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Jiri Kosina <jkosina@suse.com>
drivers/hid/hid-input.c

index 9475b7e9da43ccf9be44ac0004957b0be1623b34..39513837285342037875d9cddb7e9c2aca94b77a 100644 (file)
@@ -429,23 +429,18 @@ static int hidinput_scale_battery_capacity(struct hid_device *dev,
 
 static int hidinput_query_battery_capacity(struct hid_device *dev)
 {
-       u8 *buf;
        int ret;
 
-       buf = kmalloc(4, GFP_KERNEL);
+       u8 *buf __free(kfree) = kmalloc(4, GFP_KERNEL);
        if (!buf)
                return -ENOMEM;
 
        ret = hid_hw_raw_request(dev, dev->battery_report_id, buf, 4,
                                 dev->battery_report_type, HID_REQ_GET_REPORT);
-       if (ret < 2) {
-               kfree(buf);
+       if (ret < 2)
                return -ENODATA;
-       }
 
-       ret = hidinput_scale_battery_capacity(dev, buf[1]);
-       kfree(buf);
-       return ret;
+       return hidinput_scale_battery_capacity(dev, buf[1]);
 }
 
 static int hidinput_get_battery_property(struct power_supply *psy,
@@ -1836,7 +1831,6 @@ static void hidinput_led_worker(struct work_struct *work)
        struct hid_report *report;
        int ret;
        u32 len;
-       __u8 *buf;
 
        field = hidinput_get_led_field(hid);
        if (!field)
@@ -1863,7 +1857,7 @@ static void hidinput_led_worker(struct work_struct *work)
 
        /* fall back to generic raw-output-report */
        len = hid_report_len(report);
-       buf = hid_alloc_report_buf(report, GFP_KERNEL);
+       u8 *buf __free(kfree) = hid_alloc_report_buf(report, GFP_KERNEL);
        if (!buf)
                return;
 
@@ -1873,7 +1867,6 @@ static void hidinput_led_worker(struct work_struct *work)
        if (ret == -ENOSYS)
                hid_hw_raw_request(hid, report->id, buf, len, HID_OUTPUT_REPORT,
                                HID_REQ_SET_REPORT);
-       kfree(buf);
 }
 
 static int hidinput_input_event(struct input_dev *dev, unsigned int type,