diff mbox series

pwm: Don't check pointer for being non-NULL after use

Message ID 20240329101648.544155-2-u.kleine-koenig@pengutronix.de
State Accepted
Headers show
Series pwm: Don't check pointer for being non-NULL after use | expand

Commit Message

Uwe Kleine-König March 29, 2024, 10:16 a.m. UTC
After assigning chip = pwm->chip; the compiler is free to assume that
pwm is non-NULL and so can optimize out the check for pwm against NULL.

While it's probably a programming error to pass a NULL pointer to
pwm_put() this shouldn't be dropped without careful consideration and
wasn't intended.

So assign chip only after the NULL check.

Reported-by: David Lechner <dlechner@baylibre.com>
Fixes: d60bc2995732 ("pwm: Add a struct device to struct pwm_chip")
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 drivers/pwm/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)


base-commit: d60bc2995732489766cbe650fc367e036ff6a0e0

Comments

David Lechner March 29, 2024, 1:50 p.m. UTC | #1
On Fri, Mar 29, 2024 at 5:17 AM Uwe Kleine-König
<u.kleine-koenig@pengutronix.de> wrote:
>
> After assigning chip = pwm->chip; the compiler is free to assume that
> pwm is non-NULL and so can optimize out the check for pwm against NULL.
>
> While it's probably a programming error to pass a NULL pointer to
> pwm_put() this shouldn't be dropped without careful consideration and
> wasn't intended.
>
> So assign chip only after the NULL check.
>
> Reported-by: David Lechner <dlechner@baylibre.com>
> Fixes: d60bc2995732 ("pwm: Add a struct device to struct pwm_chip")
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---

Reviewed-by: David Lechner <dlechner@baylibre.com>
Uwe Kleine-König March 29, 2024, 2:02 p.m. UTC | #2
Hello,

On Fri, Mar 29, 2024 at 11:16:48AM +0100, Uwe Kleine-König wrote:
> After assigning chip = pwm->chip; the compiler is free to assume that
> pwm is non-NULL and so can optimize out the check for pwm against NULL.
> 
> While it's probably a programming error to pass a NULL pointer to
> pwm_put() this shouldn't be dropped without careful consideration and
> wasn't intended.
> 
> So assign chip only after the NULL check.
> 
> Reported-by: David Lechner <dlechner@baylibre.com>
> Fixes: d60bc2995732 ("pwm: Add a struct device to struct pwm_chip")
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/ukleinek/linux.git pwm/for-next

with David's Reviewed-by tag.

Thanks
Uwe
diff mbox series

Patch

diff --git a/drivers/pwm/core.c b/drivers/pwm/core.c
index 7f41ab087b98..54a62879fffa 100644
--- a/drivers/pwm/core.c
+++ b/drivers/pwm/core.c
@@ -1866,11 +1866,13 @@  EXPORT_SYMBOL_GPL(pwm_get);
  */
 void pwm_put(struct pwm_device *pwm)
 {
-	struct pwm_chip *chip = pwm->chip;
+	struct pwm_chip *chip;
 
 	if (!pwm)
 		return;
 
+	chip = pwm->chip;
+
 	mutex_lock(&pwm_lock);
 
 	/*