Re: [PATCH 14/21] leds: flash: Avoid divide-by-zero in led_clamp_align()
From: David Laight
Date: Thu Oct 22 2026 - 17:00:16 EST
On Wed, 21 Oct 2026 12:32:23 +0000
Lee Jones <lee@xxxxxxxxxx> wrote:
> If a flash LED driver leaves a setting's step size as zero, writing to
> the corresponding sysfs attribute invokes led_clamp_align() and triggers
> an integer division by zero when aligning the offset to s->step.
>
> Clamp s->val to [s->min, s->max] and return early when s->step is zero.
>
> Fixes: 7aea8389a77a ("leds: Add LED Flash class extension to the LED subsystem")
> Closes: https://sashiko.dev/#/bug/linux-9d06d7fe-310b-4aee-8b16-0fb49550c287
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
> ---
> drivers/leds/led-class-flash.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/drivers/leds/led-class-flash.c b/drivers/leds/led-class-flash.c
> index 181dfd49c6ad..16b3b6b9365b 100644
> --- a/drivers/leds/led-class-flash.c
> +++ b/drivers/leds/led-class-flash.c
> @@ -375,6 +375,11 @@ static void led_clamp_align(struct led_flash_setting *s)
> {
> u32 v, offset;
>
> + if (!s->step) {
> + s->val = clamp(s->val, s->min, s->max);
> + return;
> + }
Why not just:
u32 step = s->step ?: 1;
and then use step instead of s->step.
David
> +
> v = s->val + s->step / 2;
> v = clamp(v, s->min, s->max);
> offset = v - s->min;