device_property_read_u32() reads both the "default-brightness" and the
"max-brightness" properties directly into signed int fields. Therefore,
values larger than INT_MAX are interpreted as negative and pass the
upper-bound checks.

Read both properties into a temporary u32 and clamp the values before
assigning them to the signed backlight properties.

Fixes: 147b38a5ad06 ("backlight: aw99706: Add support for Awinic AW99706 
backlight")
Signed-off-by: Thorsten Blum <[email protected]>
---
Changes in v2:
- Also clamp the default brightness (Junjie)
- Clamp both values before assigning them to bl_props (Junjie)
- Update patch subject and description
- v1: https://lore.kernel.org/r/[email protected]/

 drivers/video/backlight/aw99706.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/drivers/video/backlight/aw99706.c 
b/drivers/video/backlight/aw99706.c
index 43f82c83f91a..1ea5d0d2146d 100644
--- a/drivers/video/backlight/aw99706.c
+++ b/drivers/video/backlight/aw99706.c
@@ -265,18 +265,13 @@ static void aw99706_dt_parse(struct aw99706_device *aw,
                aw->init_tbl[i].val = val << __ffs(prop->mask);
        }
 
-       bl_props->brightness = AW99706_MAX_BRT_LVL >> 1;
-       bl_props->max_brightness = AW99706_MAX_BRT_LVL;
-       device_property_read_u32(aw->dev, "default-brightness",
-                                &bl_props->brightness);
-       device_property_read_u32(aw->dev, "max-brightness",
-                                &bl_props->max_brightness);
-
-       if (bl_props->max_brightness > AW99706_MAX_BRT_LVL)
-               bl_props->max_brightness = AW99706_MAX_BRT_LVL;
-
-       if (bl_props->brightness > bl_props->max_brightness)
-               bl_props->brightness = bl_props->max_brightness;
+       dt_val = AW99706_MAX_BRT_LVL;
+       device_property_read_u32(aw->dev, "max-brightness", &dt_val);
+       bl_props->max_brightness = min(dt_val, AW99706_MAX_BRT_LVL);
+
+       dt_val = AW99706_MAX_BRT_LVL >> 1;
+       device_property_read_u32(aw->dev, "default-brightness", &dt_val);
+       bl_props->brightness = min_t(u32, dt_val, bl_props->max_brightness);
 }
 
 static int aw99706_hw_init(struct aw99706_device *aw)

Reply via email to