Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

scsi: megaraid: fix format-overflow warning

gcc-7 complains that the firmware version strings might overflow
for some values:

drivers/scsi/megaraid.c: In function 'megaraid_probe_one':
drivers/scsi/megaraid.c:314:33: error: '%d' directive writing between 1 and 2 bytes into a region of size between 1 and 2 [-Werror=format-overflow=]
drivers/scsi/megaraid.c:314:33: note: directive argument in the range [0, 15]
drivers/scsi/megaraid.c:314:3: note: 'sprintf' output between 7 and 9 bytes into a destination of size 7
drivers/scsi/megaraid.c:320:35: error: '%d' directive writing between 1 and 2 bytes into a region of size between 1 and 2 [-Werror=format-overflow=]
drivers/scsi/megaraid.c:320:35: note: directive argument in the range [0, 15]
drivers/scsi/megaraid.c:320:3: note: 'sprintf' output between 7 and 9 bytes into a destination of size 7

This makes the code use a truncating snprintf() instead, which shuts
up that warning.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Arnd Bergmann and committed by
Martin K. Petersen
875826a7 a7ec87a9

+4 -2
+4 -2
drivers/scsi/megaraid.c
··· 311 311 right 8 bits making them zero. This 0 value was hardcoded to fix 312 312 sparse warnings. */ 313 313 if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) { 314 - sprintf (adapter->fw_version, "%c%d%d.%d%d", 314 + snprintf(adapter->fw_version, sizeof(adapter->fw_version), 315 + "%c%d%d.%d%d", 315 316 adapter->product_info.fw_version[2], 316 317 0, 317 318 adapter->product_info.fw_version[1] & 0x0f, 318 319 0, 319 320 adapter->product_info.fw_version[0] & 0x0f); 320 - sprintf (adapter->bios_version, "%c%d%d.%d%d", 321 + snprintf(adapter->bios_version, sizeof(adapter->fw_version), 322 + "%c%d%d.%d%d", 321 323 adapter->product_info.bios_version[2], 322 324 0, 323 325 adapter->product_info.bios_version[1] & 0x0f,