Commit 22ff1a36 authored by Arnd Bergmann's avatar Arnd Bergmann Committed by Andy Shevchenko

platform/x86: alienware-wmi: fix format string overflow warning

gcc points out a possible format string overflow for a large value of 'zone':

drivers/platform/x86/alienware-wmi.c: In function 'alienware_wmi_init':
drivers/platform/x86/alienware-wmi.c:461:24: error: '%02X' directive writing between 2 and 8 bytes into a region of size 6 [-Werror=format-overflow=]
   sprintf(buffer, "zone%02X", i);
                        ^~~~
drivers/platform/x86/alienware-wmi.c:461:19: note: directive argument in the range [0, 2147483646]
   sprintf(buffer, "zone%02X", i);
                   ^~~~~~~~~~
drivers/platform/x86/alienware-wmi.c:461:3: note: 'sprintf' output between 7 and 13 bytes into a destination of size 10

This replaces the 'int' variable with an 'u8' to make sure
it always fits, renaming the variable to 'zone' for clarity.

Unfortunately, gcc-7.1.1 still warns about it with that change, which
seems to be unintended by the gcc developers. I have opened a bug
against gcc with a reduced test case. As a workaround, I also
change the format string to use "%02hhX", which shuts up the
warning in that version.

Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81483
Link: https://patchwork.ozlabs.org/patch/788415/Suggested-by: default avatarAndy Shevchenko <andy@infradead.org>
Signed-off-by: default avatarArnd Bergmann <arnd@arndb.de>
[andy: added empty lines after u8 zone; definitions]
Signed-off-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
parent cd0223c6
...@@ -255,12 +255,13 @@ static int parse_rgb(const char *buf, struct platform_zone *zone) ...@@ -255,12 +255,13 @@ static int parse_rgb(const char *buf, struct platform_zone *zone)
static struct platform_zone *match_zone(struct device_attribute *attr) static struct platform_zone *match_zone(struct device_attribute *attr)
{ {
int i; u8 zone;
for (i = 0; i < quirks->num_zones; i++) {
if ((struct device_attribute *)zone_data[i].attr == attr) { for (zone = 0; zone < quirks->num_zones; zone++) {
if ((struct device_attribute *)zone_data[zone].attr == attr) {
pr_debug("alienware-wmi: matched zone location: %d\n", pr_debug("alienware-wmi: matched zone location: %d\n",
zone_data[i].location); zone_data[zone].location);
return &zone_data[i]; return &zone_data[zone];
} }
} }
return NULL; return NULL;
...@@ -420,7 +421,7 @@ static DEVICE_ATTR(lighting_control_state, 0644, show_control_state, ...@@ -420,7 +421,7 @@ static DEVICE_ATTR(lighting_control_state, 0644, show_control_state,
static int alienware_zone_init(struct platform_device *dev) static int alienware_zone_init(struct platform_device *dev)
{ {
int i; u8 zone;
char buffer[10]; char buffer[10];
char *name; char *name;
...@@ -457,19 +458,19 @@ static int alienware_zone_init(struct platform_device *dev) ...@@ -457,19 +458,19 @@ static int alienware_zone_init(struct platform_device *dev)
if (!zone_data) if (!zone_data)
return -ENOMEM; return -ENOMEM;
for (i = 0; i < quirks->num_zones; i++) { for (zone = 0; zone < quirks->num_zones; zone++) {
sprintf(buffer, "zone%02X", i); sprintf(buffer, "zone%02hhX", zone);
name = kstrdup(buffer, GFP_KERNEL); name = kstrdup(buffer, GFP_KERNEL);
if (name == NULL) if (name == NULL)
return 1; return 1;
sysfs_attr_init(&zone_dev_attrs[i].attr); sysfs_attr_init(&zone_dev_attrs[zone].attr);
zone_dev_attrs[i].attr.name = name; zone_dev_attrs[zone].attr.name = name;
zone_dev_attrs[i].attr.mode = 0644; zone_dev_attrs[zone].attr.mode = 0644;
zone_dev_attrs[i].show = zone_show; zone_dev_attrs[zone].show = zone_show;
zone_dev_attrs[i].store = zone_set; zone_dev_attrs[zone].store = zone_set;
zone_data[i].location = i; zone_data[zone].location = zone;
zone_attrs[i] = &zone_dev_attrs[i].attr; zone_attrs[zone] = &zone_dev_attrs[zone].attr;
zone_data[i].attr = &zone_dev_attrs[i]; zone_data[zone].attr = &zone_dev_attrs[zone];
} }
zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr; zone_attrs[quirks->num_zones] = &dev_attr_lighting_control_state.attr;
zone_attribute_group.attrs = zone_attrs; zone_attribute_group.attrs = zone_attrs;
...@@ -481,12 +482,13 @@ static int alienware_zone_init(struct platform_device *dev) ...@@ -481,12 +482,13 @@ static int alienware_zone_init(struct platform_device *dev)
static void alienware_zone_exit(struct platform_device *dev) static void alienware_zone_exit(struct platform_device *dev)
{ {
u8 zone;
sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group); sysfs_remove_group(&dev->dev.kobj, &zone_attribute_group);
led_classdev_unregister(&global_led); led_classdev_unregister(&global_led);
if (zone_dev_attrs) { if (zone_dev_attrs) {
int i; for (zone = 0; zone < quirks->num_zones; zone++)
for (i = 0; i < quirks->num_zones; i++) kfree(zone_dev_attrs[zone].attr.name);
kfree(zone_dev_attrs[i].attr.name);
} }
kfree(zone_dev_attrs); kfree(zone_dev_attrs);
kfree(zone_data); kfree(zone_data);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment