Skip to contentSkip to conversation

HID: wacom: replace sprintf calls with sysfs_emit calls in wacom_sys.c

PATCH New Nehuen Lian Bova 4 participants 11 messages Nov 1 – Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 HID: wacom: replace sprintf calls with sysfs_emit calls in wacom_sys.c

Replace 'sprintf' calls with safer 'sysfs_emit' calls in wacom_sys.c, also include the header '<linux/sysfs.h>'. No functional changes.

---
 drivers/hid/wacom_sys.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hid/wacom_sys.c +3 -2
index 628372ab818a..bb51ea4b34f8 100644
@@ -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);
+	sysfs_emit(battery->bat_name, "wacom_battery_%ld", n);
 	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)					\
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] HID: wacom: invalid sysfs_emit() call in __wacom_initialize_battery() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] HID: wacom: invalid sysfs_emit() call in __wacom_initialize_battery() --

commit 4c50e84abd55ee59728372c4dfa52c46c6e8c7c7
Author: Nehuen Lian Bova <[email protected]>

HID: wacom: replace sprintf calls with sysfs_emit calls in wacom_sys.c

This commit replaces sprintf() calls with sysfs_emit() in drivers/hid/wacom_sys.c and includes <linux/sysfs.h>.

Quoted 4 lines
drivers/hid/wacom_sys.c +0 -0
index 628372ab818a2..bb51ea4b34f89 100644

[ ... ]

drivers/hid/wacom_sys.c static int __wacom_initialize_battery(struct wacom *wacom, · line 1843

Quoted 7 lines
drivers/hid/wacom_sys.c +1 -1
@@ -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);
+	sysfs_emit(battery->bat_name, "wacom_battery_%ld", n);
 	bat_desc->name = battery->bat_name;

[Severity: High]
Does using sysfs_emit() here break battery registration?

sysfs_emit() is meant for sysfs show callbacks and requires a page-aligned buffer:

fs/sysfs/file.c:sysfs_emit() {
	if (WARN(!buf || offset_in_page(buf),
		 "invalid sysfs_emit: buf:%p\n", buf))
		return 0;
}

However, battery->bat_name is a 64-byte array embedded inside struct wacom_battery:

drivers/hid/wacom.h:

struct wacom_battery {
	struct wacom *wacom;
	struct power_supply_desc bat_desc;
	struct power_supply *battery;
	char bat_name[WACOM_NAME_MAX];
	...
};

When __wacom_initialize_battery() runs during device probe or hotplug, offset_in_page(battery->bat_name) will be non-zero, causing sysfs_emit() to trigger a WARN() and return 0 without writing the name, leaving bat_desc->name empty for devm_power_supply_register(). Furthermore, if it ever were page-aligned, sysfs_emit() assumes a PAGE_SIZE buffer rather than sizeof(battery->bat_name).

Should this use snprintf(battery->bat_name, sizeof(battery->bat_name), ...) instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

PATCH v2 1/1 HID: wacom: replace sprintf calls with safer alternatives

In reply to Nehuen Lian Bova

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

---
Changes in v2:
- Use snprintf() instead of sysfs_emit() in __wacom_initialize_battery() as suggested by Sashiko bot.

 drivers/hid/wacom_sys.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hid/wacom_sys.c +3 -2
index 628372ab818a..bb5bb9c15f93 100644
@@ -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, "wacom_battery_%ld", n);
 	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)					\
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH v2] HID: wacom: replace sprintf calls with safer alternatives

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] HID: wacom: missing size argument in __wacom_initialize_battery() snprintf() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] HID: wacom: missing size argument in __wacom_initialize_battery() snprintf() --

commit 3ae99c758bf47da8936a8a4c38001b6a0d39a084
Author: Nehuen Lian Bova <[email protected]>

HID: wacom: replace sprintf calls with safer alternatives

This commit replaces sprintf() calls in drivers/hid/wacom_sys.c with snprintf() in __wacom_initialize_battery() and sysfs_emit() in wacom_show_remote_mode(), and includes <linux/sysfs.h>.

Quoted 4 lines
drivers/hid/wacom_sys.c +0 -0
index 628372ab818a2..bb5bb9c15f935 100644

[ ... ]

drivers/hid/wacom_sys.c static int __wacom_initialize_battery(struct wacom *wacom, · line 1842

Quoted 6 lines
drivers/hid/wacom_sys.c +1 -1
@@ -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, "wacom_battery_%ld", n);

[Severity: High]
Is the buffer size argument missing in this snprintf() call in __wacom_initialize_battery()?

Because snprintf() expects the buffer size as its second parameter, passing "wacom_battery_%ld" as the second argument and n as the format string pointer causes a build failure due to incompatible pointer/integer types (or an invalid pointer dereference when initializing a battery on device probe or wireless connection).

Should this pass sizeof(battery->bat_name) as the second argument instead:

	snprintf(battery->bat_name, sizeof(battery->bat_name),
		 "wacom_battery_%ld", n);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

PATCH v3 1/1 HID: wacom: replace sprintf calls with safer alternatives in wacom_sys.c

In reply to Nehuen Lian Bova

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

---
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(-)
drivers/hid/wacom_sys.c +3 -2
index 628372ab818a..7d9bbcb13ae0 100644
@@ -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);
 	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)					\
Signature · 1 line
2.43.0
#5 lore raw

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

Quoted 30 lines · Nehuen Lian Bova

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

---
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(-)
drivers/hid/wacom_sys.c +1 -0
index 628372ab818a..7d9bbcb13ae0 100644
@@ -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

drivers/hid/wacom_sys.c static int __wacom_initialize_battery(struct wacom *wacom, · line 1842

Quoted 5 lines
drivers/hid/wacom_sys.c +1 -1
@@ -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....

Quoted 19 lines
drivers/hid/wacom_sys.c +1 -1
        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

#6 lore raw

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

Quoted 6 lines

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)

I didn't check the return value because I noticed in almost every situation snprintf() is used, the return value wasn't checked.

Anyway, would be a good idea to return -ENAMETOOLONG after checking if return value >= sizeof(bat_name)?
Or using scnprintf() instead of snprintf() could be a cleaner option? :)

--
Nehuen Lian Bova

#7 lore raw

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

Hi Jason,

Just wanted to follow up on my previous suggestion regarding the error handling when using snprintf() or implementing scnprintf() directly. Did you get a chance to consider it?
If you prefer a different approach or need more information from my side, please let me know.

Thanks,
Nehuen

#8 lore raw

Re: [PATCH v2] HID: wacom: replace sprintf calls with safer alternatives

In reply to Nehuen Lian Bova
kernel test robot noticed the following build errors:

Hi Nehuen,

kernel test robot noticed the following build errors:

[auto build test ERROR on hid/for-next]
[also build test ERROR on linus/master v7.3 next-20261030] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Nehuen-Lian-Bova/HID-wacom-replace-sprintf-calls-with-safer-alternatives/20261101-024830
base:   https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git for-next
patch link:    https://lore.kernel.org/r/20261101054830.291749-1-nehuenlian.kernel%40gmail.com
patch subject: [PATCH v2] HID: wacom: replace sprintf calls with safer alternatives
config: parisc-randconfig-1001-20261103 (https://download.01.org/0day-ci/archive/20261103/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 14.4.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

drivers/hid/wacom_sys.c: In function '__wacom_initialize_battery':

>> drivers/hid/wacom_sys.c:1842:37: error: passing argument 2 of 'snprintf' makes integer from pointer without a cast [-Wint-conversion]
    1842 |         snprintf(battery->bat_name, "wacom_battery_%ld", n);
         |                                     ^~~~~~~~~~~~~~~~~~~
         |                                     |
         |                                     char *
   In file included from include/linux/kernel.h:32,
                    from include/linux/random.h:7,
                    from include/linux/nodemask.h:94,
                    from include/linux/numa.h:6,
                    from include/linux/cpumask.h:15,
                    from include/linux/smp.h:13,
                    from include/linux/interrupt_rc.h:18,
                    from include/linux/spinlock.h:60,
                    from include/linux/mmzone.h:8,
                    from include/linux/gfp.h:7,
                    from include/linux/slab.h:17,
                    from include/linux/hid.h:19,
                    from drivers/hid/wacom_wac.h:7,
                    from drivers/hid/wacom_sys.c:6:
   include/linux/sprintf.h:13:47: note: expected 'size_t' {aka 'unsigned int'} but argument is of type 'char *'
      13 | __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...);
         |                                        ~~~~~~~^~~~
>> drivers/hid/wacom_sys.c:1842:58: error: passing argument 3 of 'snprintf' makes pointer from integer without a cast [-Wint-conversion]
    1842 |         snprintf(battery->bat_name, "wacom_battery_%ld", n);
         |                                                          ^
         |                                                          |
         |                                                          long unsigned int
   include/linux/sprintf.h:13:65: note: expected 'const char *' but argument is of type 'long unsigned int'
      13 | __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...);
         |                                                     ~~~~~~~~~~~~^~~

vim +/snprintf +1842 drivers/hid/wacom_sys.c

1820
1821 static int __wacom_initialize_battery(struct wacom *wacom,
1822 struct wacom_battery *battery)
1823 {
1824 static atomic_t battery_no = ATOMIC_INIT(0);
1825 struct device *dev = &wacom->hdev->dev;
1826 struct power_supply_config psy_cfg = { .drv_data = battery, };
1827 struct power_supply *ps_bat;
1828 struct power_supply_desc *bat_desc = &battery->bat_desc;
1829 unsigned long n;
1830 int error;
1831
1832 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1833 return -ENOMEM;
1834
1835 battery->wacom = wacom;
1836
1837 n = atomic_inc_return(&battery_no) - 1;
1838
1839 bat_desc->properties = wacom_battery_props;
1840 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1841 bat_desc->get_property = wacom_battery_get_property;

> 1842		snprintf(battery->bat_name, "wacom_battery_%ld", n);

1843 bat_desc->name = battery->bat_name;
1844 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1845 bat_desc->use_for_apm = 0;
1846
1847 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1848 if (IS_ERR(ps_bat)) {
1849 error = PTR_ERR(ps_bat);
1850 goto err;
1851 }
1852
1853 power_supply_powers(ps_bat, &wacom->hdev->dev);
1854
1855 battery->battery = ps_bat;
1856
1857 devres_close_group(dev, bat_desc);
1858 return 0;
1859
1860 err:
1861 devres_release_group(dev, bat_desc);
1862 return error;
1863 }
1864

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#9 lore raw

Re: [PATCH v2] HID: wacom: replace sprintf calls with safer alternatives

In reply to Nehuen Lian Bova
kernel test robot noticed the following build errors:

Hi Nehuen,

kernel test robot noticed the following build errors:

[auto build test ERROR on hid/for-next]
[also build test ERROR on linus/master v7.3 next-20261030] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Nehuen-Lian-Bova/HID-wacom-replace-sprintf-calls-with-safer-alternatives/20261101-024830
base:   https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git for-next
patch link:    https://lore.kernel.org/r/20261101054830.291749-1-nehuenlian.kernel%40gmail.com
patch subject: [PATCH v2] HID: wacom: replace sprintf calls with safer alternatives
config: i386-randconfig-1000-20261103 (https://download.01.org/0day-ci/archive/20261103/[email protected]/config)
compiler: clang version 22.1.8 (https://github.com/llvm/llvm-project ca7933e47d3a3451d81e72ac174dcb5aa28b59d1)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> drivers/hid/wacom_sys.c:1842:30: error: incompatible pointer to integer conversion passing 'char[18]' to parameter of type '__size_t' (aka 'unsigned int') [-Wint-conversion]
    1842 |         snprintf(battery->bat_name, "wacom_battery_%ld", n);
         |                                     ^~~~~~~~~~~~~~~~~~~
   include/linux/sprintf.h:13:47: note: passing argument to parameter 'size' here
      13 | __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...);
         |                                               ^
>> drivers/hid/wacom_sys.c:1842:51: error: incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'const char *' [-Wint-conversion]
    1842 |         snprintf(battery->bat_name, "wacom_battery_%ld", n);
         |                                                          ^
   include/linux/sprintf.h:13:65: note: passing argument to parameter 'fmt' here
      13 | __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...);
         |                                                                 ^

2 errors generated.

vim +1842 drivers/hid/wacom_sys.c

1820
1821 static int __wacom_initialize_battery(struct wacom *wacom,
1822 struct wacom_battery *battery)
1823 {
1824 static atomic_t battery_no = ATOMIC_INIT(0);
1825 struct device *dev = &wacom->hdev->dev;
1826 struct power_supply_config psy_cfg = { .drv_data = battery, };
1827 struct power_supply *ps_bat;
1828 struct power_supply_desc *bat_desc = &battery->bat_desc;
1829 unsigned long n;
1830 int error;
1831
1832 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1833 return -ENOMEM;
1834
1835 battery->wacom = wacom;
1836
1837 n = atomic_inc_return(&battery_no) - 1;
1838
1839 bat_desc->properties = wacom_battery_props;
1840 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1841 bat_desc->get_property = wacom_battery_get_property;

> 1842		snprintf(battery->bat_name, "wacom_battery_%ld", n);

1843 bat_desc->name = battery->bat_name;
1844 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1845 bat_desc->use_for_apm = 0;
1846
1847 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1848 if (IS_ERR(ps_bat)) {
1849 error = PTR_ERR(ps_bat);
1850 goto err;
1851 }
1852
1853 power_supply_powers(ps_bat, &wacom->hdev->dev);
1854
1855 battery->battery = ps_bat;
1856
1857 devres_close_group(dev, bat_desc);
1858 return 0;
1859
1860 err:
1861 devres_release_group(dev, bat_desc);
1862 return error;
1863 }
1864

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#10 lore raw

Re: [PATCH v2] HID: wacom: replace sprintf calls with safer alternatives

In reply to Nehuen Lian Bova
kernel test robot noticed the following build warnings:

Hi Nehuen,

kernel test robot noticed the following build warnings:

[auto build test WARNING on hid/for-next]
[also build test WARNING on linus/master v7.3 next-20261030] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Nehuen-Lian-Bova/HID-wacom-replace-sprintf-calls-with-safer-alternatives/20261101-024830
base:   https://git.kernel.org/pub/scm/linux/kernel/git/hid/hid.git for-next
patch link:    https://lore.kernel.org/r/20261101054830.291749-1-nehuenlian.kernel%40gmail.com
patch subject: [PATCH v2] HID: wacom: replace sprintf calls with safer alternatives
config: nios2-allmodconfig (https://download.01.org/0day-ci/archive/20261103/[email protected]/config)
compiler: nios2-linux-gcc (GCC) 11.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261103/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/hid/wacom_sys.c: In function '__wacom_initialize_battery':

>> drivers/hid/wacom_sys.c:1842:37: warning: passing argument 2 of 'snprintf' makes integer from pointer without a cast [-Wint-conversion]
    1842 |         snprintf(battery->bat_name, "wacom_battery_%ld", n);
         |                                     ^~~~~~~~~~~~~~~~~~~
         |                                     |
         |                                     char *
   In file included from include/linux/kernel.h:32,
                    from include/linux/random.h:7,
                    from include/linux/nodemask.h:94,
                    from include/linux/numa.h:6,
                    from include/linux/cpumask.h:15,
                    from include/linux/smp.h:13,
                    from include/linux/interrupt_rc.h:18,
                    from include/linux/spinlock.h:60,
                    from include/linux/mmzone.h:8,
                    from include/linux/gfp.h:7,
                    from include/linux/slab.h:17,
                    from include/linux/hid.h:19,
                    from drivers/hid/wacom_wac.h:7,
                    from drivers/hid/wacom_sys.c:6:
   include/linux/sprintf.h:13:47: note: expected 'size_t' {aka 'unsigned int'} but argument is of type 'char *'
      13 | __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...);
         |                                        ~~~~~~~^~~~
>> drivers/hid/wacom_sys.c:1842:58: warning: passing argument 3 of 'snprintf' makes pointer from integer without a cast [-Wint-conversion]
    1842 |         snprintf(battery->bat_name, "wacom_battery_%ld", n);
         |                                                          ^
         |                                                          |
         |                                                          long unsigned int
   In file included from include/linux/kernel.h:32,
                    from include/linux/random.h:7,
                    from include/linux/nodemask.h:94,
                    from include/linux/numa.h:6,
                    from include/linux/cpumask.h:15,
                    from include/linux/smp.h:13,
                    from include/linux/interrupt_rc.h:18,
                    from include/linux/spinlock.h:60,
                    from include/linux/mmzone.h:8,
                    from include/linux/gfp.h:7,
                    from include/linux/slab.h:17,
                    from include/linux/hid.h:19,
                    from drivers/hid/wacom_wac.h:7,
                    from drivers/hid/wacom_sys.c:6:
   include/linux/sprintf.h:13:65: note: expected 'const char *' but argument is of type 'long unsigned int'
      13 | __printf(3, 4) int snprintf(char *buf, size_t size, const char *fmt, ...);
         |                                                     ~~~~~~~~~~~~^~~

vim +/snprintf +1842 drivers/hid/wacom_sys.c

1820
1821 static int __wacom_initialize_battery(struct wacom *wacom,
1822 struct wacom_battery *battery)
1823 {
1824 static atomic_t battery_no = ATOMIC_INIT(0);
1825 struct device *dev = &wacom->hdev->dev;
1826 struct power_supply_config psy_cfg = { .drv_data = battery, };
1827 struct power_supply *ps_bat;
1828 struct power_supply_desc *bat_desc = &battery->bat_desc;
1829 unsigned long n;
1830 int error;
1831
1832 if (!devres_open_group(dev, bat_desc, GFP_KERNEL))
1833 return -ENOMEM;
1834
1835 battery->wacom = wacom;
1836
1837 n = atomic_inc_return(&battery_no) - 1;
1838
1839 bat_desc->properties = wacom_battery_props;
1840 bat_desc->num_properties = ARRAY_SIZE(wacom_battery_props);
1841 bat_desc->get_property = wacom_battery_get_property;

> 1842		snprintf(battery->bat_name, "wacom_battery_%ld", n);

1843 bat_desc->name = battery->bat_name;
1844 bat_desc->type = POWER_SUPPLY_TYPE_BATTERY;
1845 bat_desc->use_for_apm = 0;
1846
1847 ps_bat = devm_power_supply_register(dev, bat_desc, &psy_cfg);
1848 if (IS_ERR(ps_bat)) {
1849 error = PTR_ERR(ps_bat);
1850 goto err;
1851 }
1852
1853 power_supply_powers(ps_bat, &wacom->hdev->dev);
1854
1855 battery->battery = ps_bat;
1856
1857 devres_close_group(dev, bat_desc);
1858 return 0;
1859
1860 err:
1861 devres_release_group(dev, bat_desc);
1862 return error;
1863 }
1864

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki