Re: [PATCH v3] HID: wacom: replace sprintf calls with safer alternatives in wacom_sys.c

From: Jason Gerecke

Date: Mon Nov 02 2026 - 14:45:33 EST


On Sun, Nov 1, 2026 at 12:18 AM Nehuen Lian Bova
<nehuenlian.kernel@xxxxxxxxx> wrote:
>
> Replace sprintf() calls with sysfs_emit() and snprintf() calls in
> wacom_sys.c, which are safer and avoid buffer overflows.
> Also include the header '<linux/sysfs.h>'
>
> Signed-off-by: Nehuen Lian Bova <nehuenlian.kernel@xxxxxxxxx>
> ---
> Changes in v2:
> - Use snprintf() instead of sysfs_emit() in __wacom_initialize_battery()
> as suggested by Sashiko bot.
>
> Changes in v3:
> - Pass sizeof(battery->bat_name) to snprintf() as suggested by
> Sashiko bot.
> drivers/hid/wacom_sys.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c
> index 628372ab818a..7d9bbcb13ae0 100644
> --- a/drivers/hid/wacom_sys.c
> +++ b/drivers/hid/wacom_sys.c
> @@ -6,6 +6,7 @@
> #include "wacom_wac.h"
> #include "wacom.h"
> #include <linux/input/mt.h>
> +#include <linux/sysfs.h>
>
> #define WAC_MSG_RETRIES 5
> #define WAC_CMD_RETRIES 10
> @@ -1838,7 +1839,7 @@ static int __wacom_initialize_battery(struct wacom *wacom,
> bat_desc->properties = wacom_battery_props;
> bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
> bat_desc->get_property = wacom_battery_get_property;
> - sprintf(battery->bat_name, "wacom_battery_%ld", n);
> + snprintf(battery->bat_name, sizeof(battery->bat_name), "wacom_battery_%ld", n);

If you're going through the effort to use snprintf the string, you
might as well also read its return value to check to check for
truncation (return value greater than or equal to size) and handle
such an error condition appropriately.

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....

> bat_desc->name = battery->bat_name;
> bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
> bat_desc->use_for_apm = 0;
> @@ -1928,7 +1929,7 @@ static ssize_t wacom_show_remote_mode(struct kobject *kobj,
> u8 mode;
>
> mode = wacom->led.groups[index].select;
> - return sprintf(buf, "%d\n", mode < 3 ? mode : -1);
> + return sysfs_emit(buf, "%d\n", mode < 3 ? mode : -1);



> }
>
> #define DEVICE_EKR_ATTR_GROUP(SET_ID) \
> --
> 2.43.0
>
>