Input: analog - fix invalid snprintf() call

Overlapping input and output arguments to snprintf() are
undefined behavior in C99:

drivers/input/joystick/analog.c: In function 'analog_name':
drivers/input/joystick/analog.c:428:3: error: 'snprintf' argument 4 overlaps destination object 'analog' [-Werror=restrict]
  428 |   snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  429 |     analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/input/joystick/analog.c:420:40: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
  420 | static void analog_name(struct analog *analog)
      |                         ~~~~~~~~~~~~~~~^~~~~~

Change this function to use the simpler seq_buf interface instead.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/r/20210927101416.1569609-1-arnd@kernel.org
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
This commit is contained in:
Arnd Bergmann 2021-10-05 13:27:26 -07:00 коммит произвёл Dmitry Torokhov
Родитель 5c4c2c8e6f
Коммит 1f59342be6
1 изменённых файлов: 10 добавлений и 8 удалений

Просмотреть файл

@ -19,6 +19,7 @@
#include <linux/input.h> #include <linux/input.h>
#include <linux/gameport.h> #include <linux/gameport.h>
#include <linux/jiffies.h> #include <linux/jiffies.h>
#include <linux/seq_buf.h>
#include <linux/timex.h> #include <linux/timex.h>
#include <linux/timekeeping.h> #include <linux/timekeeping.h>
@ -419,23 +420,24 @@ static void analog_calibrate_timer(struct analog_port *port)
static void analog_name(struct analog *analog) static void analog_name(struct analog *analog)
{ {
snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button", struct seq_buf s;
seq_buf_init(&s, analog->name, sizeof(analog->name));
seq_buf_printf(&s, "Analog %d-axis %d-button",
hweight8(analog->mask & ANALOG_AXES_STD), hweight8(analog->mask & ANALOG_AXES_STD),
hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 + hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4); hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
if (analog->mask & ANALOG_HATS_ALL) if (analog->mask & ANALOG_HATS_ALL)
snprintf(analog->name, sizeof(analog->name), "%s %d-hat", seq_buf_printf(&s, " %d-hat",
analog->name, hweight16(analog->mask & ANALOG_HATS_ALL)); hweight16(analog->mask & ANALOG_HATS_ALL));
if (analog->mask & ANALOG_HAT_FCS) if (analog->mask & ANALOG_HAT_FCS)
strlcat(analog->name, " FCS", sizeof(analog->name)); seq_buf_printf(&s, " FCS");
if (analog->mask & ANALOG_ANY_CHF) if (analog->mask & ANALOG_ANY_CHF)
strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF", seq_buf_printf(&s, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
sizeof(analog->name));
strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick", seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
sizeof(analog->name));
} }
/* /*