drivers: led_pwm: fix overflow in set_brightness

The set_brightness function of the led_pwm driver uses a default PWM
period (defined in the pwms DT property) to compute a pulse passed to
the pwm_set_pulse_dt function. If this default period is greater than
2^32/100 nanoseconds (about 43 milliseconds) then the calculation may
overflow.

This patch prevents this overflow by running the pulse computation under
a cast with a larger type (uint64_t).

Reported-by: Scott Worley <scott.worley@microchip.com>
Signed-off-by: Simon Guinot <simon.guinot@seagate.com>
This commit is contained in:
Simon Guinot 2023-07-16 12:43:52 +02:00 committed by Fabio Baltieri
commit a4645908ee

View file

@ -65,7 +65,7 @@ static int led_pwm_set_brightness(const struct device *dev,
dt_led = &config->led[led];
return pwm_set_pulse_dt(&config->led[led],
dt_led->period * value / 100);
(uint32_t) ((uint64_t) dt_led->period * value / 100));
}
static int led_pwm_on(const struct device *dev, uint32_t led)