Re: [PATCH v4] HID: wacom: validate battery range before scaling

From: Jason Gerecke

Date: Mon Nov 09 2026 - 13:13:09 EST


On Fri, Nov 6, 2026 at 5:26 PM Aldo Ariel Panzardo <qwe.aldo@xxxxxxxxx> wrote:
>
> Battery events divide by the difference between the logical maximum
> and minimum without validating the range. A malformed HID descriptor
> with equal limits can therefore trigger a divide-by-zero exception.
>
> Move percentage conversion to a helper that rejects non-positive
> ranges and accounts for a nonzero logical minimum. Clamp the result
> to [0, 100] as an additional safeguard against out-of-range
> percentages.
>
> Fixes: 37d160193834 ("HID: wacom: generic: Scale battery capacity measurements to percentages")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Aldo Ariel Panzardo <qwe.aldo@xxxxxxxxx>

LGTM

Reviewed-by: Jason Gerecke <jason.gerecke@xxxxxxxxx>

Jason (she/they)
---
Now instead of four in the eights place /
you’ve got three, ‘Cause you added one /
(That is to say, eight) to the two, /
But you can’t take seven from three, /
So you look at the sixty-fours....

> ---
>
> Changes in v4:
> - Reword commit message: describe clamp as a defensive safeguard
> rather than claiming it handles a specific ADC scenario. The
> caller already filters out-of-range values. Code unchanged.
>
> Changes in v3:
> - Drop s64 widening and div64_s64 as requested by Jason Gerecke.
> Use __s32 range with a <= 0 guard instead. Keep the clamping.
>
> Changes in v2:
> - Clamp the computed percentage to [0, 100] instead of returning
> false, so that slightly out-of-range ADC values do not prevent
> battery registration or silently drop updates.
>
> drivers/hid/wacom_wac.c | 14 ++++++++++++--
> 1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hid/wacom_wac.c b/drivers/hid/wacom_wac.c
> index 361738b..36bab07 100644
> --- a/drivers/hid/wacom_wac.c
> +++ b/drivers/hid/wacom_wac.c
> @@ -1960,6 +1960,16 @@ static void wacom_wac_battery_usage_mapping(struct hid_device *hdev,
> return;
> }
>
> +static __s32 wacom_wac_battery_percentage(struct hid_field *field, __s32 value)
> +{
> + __s32 range = field->logical_maximum - field->logical_minimum;
> +
> + if (range <= 0)
> + return 0;
> +
> + return clamp_val((value - field->logical_minimum) * 100 / range, 0, 100);
> +}
> +
> static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *field,
> struct hid_usage *usage, __s32 value)
> {
> @@ -1973,7 +1983,7 @@ static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *f
> wacom_wac->hid_data.bat_status = POWER_SUPPLY_STATUS_UNKNOWN;
> }
> else {
> - value = value * 100 / (field->logical_maximum - field->logical_minimum);
> + value = wacom_wac_battery_percentage(field, value);
> wacom_wac->hid_data.battery_capacity = value;
> wacom_wac->hid_data.bat_connected = 1;
> wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
> @@ -1981,7 +1991,7 @@ static void wacom_wac_battery_event(struct hid_device *hdev, struct hid_field *f
> wacom_wac->features.quirks |= WACOM_QUIRK_BATTERY;
> break;
> case WACOM_HID_WD_BATTERY_LEVEL:
> - value = value * 100 / (field->logical_maximum - field->logical_minimum);
> + value = wacom_wac_battery_percentage(field, value);
> wacom_wac->hid_data.battery_capacity = value;
> wacom_wac->hid_data.bat_connected = 1;
> wacom_wac->hid_data.bat_status = WACOM_POWER_SUPPLY_STATUS_AUTO;
> --
> 2.53.0
>