Re: [PATCH] mptfusion: use strlcpy() instead of strncpy()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jan 12, 2018 at 1:46 PM, Xiongfeng Wang
<wangxiongfeng2@xxxxxxxxxx> wrote:
> From: Xiongfeng Wang <xiongfeng.wang@xxxxxxxxxx>
>
> drivers/message/fusion/mptctl.c: In function '__mptctl_ioctl.isra.3':
> ./include/linux/string.h:245:9: warning: '__builtin_strncpy' specified
> bound 12 equals destination size [-Wstringop-truncation]
>
> The compiler requires that the destination size should be greater than
> the length we copy to make sure the dest string is nul-terminated. We
> can just use strlcpy() to avoid this warning.

Are you sure it's a best approach in this case?

> -       strncpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);
> -       karg->driverVersion[MPT_IOCTL_VERSION_LENGTH-1]='\0';
> +       strlcpy (karg->driverVersion, MPT_LINUX_PACKAGE_NAME, MPT_IOCTL_VERSION_LENGTH);

This one is false positive.

> -       strncpy (karg.name, ioc->name, MPT_MAX_NAME);
> -       karg.name[MPT_MAX_NAME-1]='\0';
> -       strncpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);
> -       karg.product[MPT_PRODUCT_LENGTH-1]='\0';
> +       strlcpy (karg.name, ioc->name, MPT_MAX_NAME);
> +       strlcpy (karg.product, ioc->prod_name, MPT_PRODUCT_LENGTH);

These two as well.

> -       strncpy(karg.serial_number, " ", 24);
> +       strlcpy(karg.serial_number, " ", 24);

This one is interesting indeed.
Though the fix would be rather something like

memset(&karg.serial_number, " ", 24); // leave 24 for best performance of memset
karg.serial_number[24-1] = '\0';

>                                 if (mpt_config(ioc, &cfg) == 0) {
>                                         ManufacturingPage0_t *pdata = (ManufacturingPage0_t *) pbuf;

>                                         if (strlen(pdata->BoardTracerNumber) > 1) {
> -                                               strncpy(karg.serial_number,                                                                         pdata->BoardTracerNumber, 24);
> -                                               karg.serial_number[24-1]='\0';
> +                                               strlcpy(karg.serial_number,
> +                                                       pdata->BoardTracerNumber, 24);
>                                         }

...and here you don't need to touch anything.

-- 
With Best Regards,
Andy Shevchenko



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [SCSI Target Devel]     [Linux SCSI Target Infrastructure]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Linux IIO]     [Samba]     [Device Mapper]

  Powered by Linux