From 442fae1b389c621530a2c379511ea64963d190a8 Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Thu, 3 Nov 2022 13:52:25 -0500 Subject: [PATCH] drivers: pinctrl: fix lookup when there are no states Right now it is possible that some devices define 0 pinctrl states in devicetree, because pinctrl-N entries may still be optional for backward compatibility. If the programmer makes a mistake and forgets them, application could experience runtime crashes because pinctrl_lookup_states assumes you have at least one state, so it does not perform any bounds checking. Change the while condition so that it is skipped if states count is zero. Signed-off-by: Gerard Marull-Paretas --- drivers/pinctrl/common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/pinctrl/common.c b/drivers/pinctrl/common.c index bbd0af1d7f547f..1271539ddb4d83 100644 --- a/drivers/pinctrl/common.c +++ b/drivers/pinctrl/common.c @@ -10,7 +10,7 @@ int pinctrl_lookup_state(const struct pinctrl_dev_config *config, uint8_t id, const struct pinctrl_state **state) { *state = &config->states[0]; - while (*state <= &config->states[config->state_cnt - 1U]) { + while (*state < &config->states[config->state_cnt]) { if (id == (*state)->id) { return 0; }