diff mbox series

scsi: core: Safe warning about bad dev info string

Message ID 20240111162419.12406-1-pmladek@suse.com
State New
Headers show
Series scsi: core: Safe warning about bad dev info string | expand

Commit Message

Petr Mladek Jan. 11, 2024, 4:24 p.m. UTC
Both "model" and "strflags" are passed to "%s" even when one or both
are NULL.

It is safe because vsprintf() would detect the NULL pointer and print
"(null)". But it is a kernel-specific feature and compiler warns
about it:

<warning>
   In file included from include/linux/kernel.h:19,
                    from arch/x86/include/asm/percpu.h:27,
                    from arch/x86/include/asm/current.h:6,
                    from include/linux/sched.h:12,
                    from include/linux/blkdev.h:5,
                    from drivers/scsi/scsi_devinfo.c:3:
   drivers/scsi/scsi_devinfo.c: In function 'scsi_dev_info_list_add_str':
>> include/linux/printk.h:434:44: warning: '%s' directive argument is null [-Wformat-overflow=]
     434 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
         |                                            ^
   include/linux/printk.h:430:3: note: in definition of macro 'printk_index_wrap'
     430 |   _p_func(_fmt, ##__VA_ARGS__);    \
         |   ^~~~~~~
   drivers/scsi/scsi_devinfo.c:551:4: note: in expansion of macro 'printk'
     551 |    printk(KERN_ERR "%s: bad dev info string '%s' '%s'"
         |    ^~~~~~
   drivers/scsi/scsi_devinfo.c:552:14: note: format string is defined here
     552 |           " '%s'\n", __func__, vendor, model,
         |              ^~
</warning>

Do not rely on the kernel specific behavior and print the message a safe way.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202401112002.AOjwMNM0-lkp@intel.com/
Signed-off-by: Petr Mladek <pmladek@suse.com>
---
Note: The patch is only compile tested.

 drivers/scsi/scsi_devinfo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Chris Down Jan. 16, 2024, 7:36 p.m. UTC | #1
Petr Mladek writes:
>Both "model" and "strflags" are passed to "%s" even when one or both
>are NULL.
>
>It is safe because vsprintf() would detect the NULL pointer and print
>"(null)". But it is a kernel-specific feature and compiler warns
>about it:
>
><warning>
>   In file included from include/linux/kernel.h:19,
>                    from arch/x86/include/asm/percpu.h:27,
>                    from arch/x86/include/asm/current.h:6,
>                    from include/linux/sched.h:12,
>                    from include/linux/blkdev.h:5,
>                    from drivers/scsi/scsi_devinfo.c:3:
>   drivers/scsi/scsi_devinfo.c: In function 'scsi_dev_info_list_add_str':
>>> include/linux/printk.h:434:44: warning: '%s' directive argument is null [-Wformat-overflow=]
>     434 | #define printk(fmt, ...) printk_index_wrap(_printk, fmt, ##__VA_ARGS__)
>         |                                            ^
>   include/linux/printk.h:430:3: note: in definition of macro 'printk_index_wrap'
>     430 |   _p_func(_fmt, ##__VA_ARGS__);    \
>         |   ^~~~~~~
>   drivers/scsi/scsi_devinfo.c:551:4: note: in expansion of macro 'printk'
>     551 |    printk(KERN_ERR "%s: bad dev info string '%s' '%s'"
>         |    ^~~~~~
>   drivers/scsi/scsi_devinfo.c:552:14: note: format string is defined here
>     552 |           " '%s'\n", __func__, vendor, model,
>         |              ^~
></warning>
>
>Do not rely on the kernel specific behavior and print the message a safe way.

Acked-by: Chris Down <chris@chrisdown.name>

While I agree with the other thread that in reality this is ok, it's worth 
reducing the addition to LKP noise for now and worrying about that later.

Thanks!

>
>Reported-by: kernel test robot <lkp@intel.com>
>Closes: https://lore.kernel.org/oe-kbuild-all/202401112002.AOjwMNM0-lkp@intel.com/
>Signed-off-by: Petr Mladek <pmladek@suse.com>
>---
>Note: The patch is only compile tested.
>
> drivers/scsi/scsi_devinfo.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
>diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c
>index 3fcaf10a9dfe..ba7237e83863 100644
>--- a/drivers/scsi/scsi_devinfo.c
>+++ b/drivers/scsi/scsi_devinfo.c
>@@ -551,9 +551,9 @@ static int scsi_dev_info_list_add_str(char *dev_list)
> 		if (model)
> 			strflags = strsep(&next, next_check);
> 		if (!model || !strflags) {
>-			printk(KERN_ERR "%s: bad dev info string '%s' '%s'"
>-			       " '%s'\n", __func__, vendor, model,
>-			       strflags);
>+			pr_err("%s: bad dev info string '%s' '%s' '%s'\n",
>+			       __func__, vendor, model ? model : "",
>+			       strflags ? strflags : "");
> 			res = -EINVAL;
> 		} else
> 			res = scsi_dev_info_list_add(0 /* compatible */, vendor,
>-- 
>2.43.0
>
Martin K. Petersen Jan. 30, 2024, 2:27 a.m. UTC | #2
On Thu, 11 Jan 2024 17:24:19 +0100, Petr Mladek wrote:

> Both "model" and "strflags" are passed to "%s" even when one or both
> are NULL.
> 
> It is safe because vsprintf() would detect the NULL pointer and print
> "(null)". But it is a kernel-specific feature and compiler warns
> about it:
> 
> [...]

Applied to 6.9/scsi-queue, thanks!

[1/1] scsi: core: Safe warning about bad dev info string
      https://git.kernel.org/mkp/scsi/c/796cae1a79b1
diff mbox series

Patch

diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c
index 3fcaf10a9dfe..ba7237e83863 100644
--- a/drivers/scsi/scsi_devinfo.c
+++ b/drivers/scsi/scsi_devinfo.c
@@ -551,9 +551,9 @@  static int scsi_dev_info_list_add_str(char *dev_list)
 		if (model)
 			strflags = strsep(&next, next_check);
 		if (!model || !strflags) {
-			printk(KERN_ERR "%s: bad dev info string '%s' '%s'"
-			       " '%s'\n", __func__, vendor, model,
-			       strflags);
+			pr_err("%s: bad dev info string '%s' '%s' '%s'\n",
+			       __func__, vendor, model ? model : "",
+			       strflags ? strflags : "");
 			res = -EINVAL;
 		} else
 			res = scsi_dev_info_list_add(0 /* compatible */, vendor,