Commit a2633dc2 authored by Kari Argillander's avatar Kari Argillander Committed by Uwe Kleine-König
Browse files

rust: pwm: Fix potential memory leak on init error



When initializing a PWM chip using pwmchip_alloc(), the allocated device
owns an initial reference that must be released on all error paths.

If __pinned_init() were to fail, the allocated pwm_chip would currently
leak because the error path returns without calling pwmchip_put().

Fixes: 7b3dce81 ("rust: pwm: Add Kconfig and basic data structures")
Signed-off-by: default avatarKari Argillander <kari.argillander@gmail.com>
Acked-by: default avatarMichal Wilczynski <m.wilczynski@samsung.com>
Link: https://patch.msgid.link/20260102-pwm-rust-v2-1-2702ce57d571@gmail.com


Signed-off-by: default avatarUwe Kleine-König <ukleinek@kernel.org>
parent b0dc6c6e
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -607,7 +607,11 @@ pub fn new<'a>(
        let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) };

        // SAFETY: We construct the `T` object in-place in the allocated private memory.
        unsafe { data.__pinned_init(drvdata_ptr.cast())? };
        unsafe { data.__pinned_init(drvdata_ptr.cast()) }.inspect_err(|_| {
            // SAFETY: It is safe to call `pwmchip_put()` with a valid pointer obtained
            // from `pwmchip_alloc()`. We will not use pointer after this.
            unsafe { bindings::pwmchip_put(c_chip_ptr) }
        })?;

        // SAFETY: `c_chip_ptr` points to a valid chip.
        unsafe {