Re: [PATCH] scsi: csiostor: replace snprintf with sysfs_emit

From: Damien Le Moal
Date: Tue Feb 08 2022 - 23:08:57 EST


On 2022/02/09 12:12, Joe Perches wrote:
> On Wed, 2022-02-09 at 11:36 +0900, Damien Le Moal wrote:
>> On 2/9/22 09:40, davidcomponentone@xxxxxxxxx wrote:
>>> From: Yang Guang <yang.guang5@xxxxxxxxxx>
>>>
>>> coccinelle report:
>>> ./drivers/scsi/csiostor/csio_scsi.c:1433:8-16:
>>> WARNING: use scnprintf or sprintf
>>> ./drivers/scsi/csiostor/csio_scsi.c:1369:9-17:
>>> WARNING: use scnprintf or sprintf
>>> ./drivers/scsi/csiostor/csio_scsi.c:1479:8-16:
>>> WARNING: use scnprintf or sprintf
>>>
>>> Use sysfs_emit instead of scnprintf or sprintf makes more sense.
> []
>>> diff --git a/drivers/scsi/csiostor/csio_scsi.c b/drivers/scsi/csiostor/csio_scsi.c
> []
>>> @@ -1366,9 +1366,9 @@ csio_show_hw_state(struct device *dev,
>>> struct csio_hw *hw = csio_lnode_to_hw(ln);
>>>
>>> if (csio_is_hw_ready(hw))
>>> - return snprintf(buf, PAGE_SIZE, "ready\n");
>>> + return sysfs_emit(buf, "ready\n");
>>> else
>>> - return snprintf(buf, PAGE_SIZE, "not ready\n");
>>> + return sysfs_emit(buf, "not ready\n");
>>
>> While at it, you could remove the useless "else" above.
>
> Or not. It's fine as is. It's just a style preference.

It is. I dislike the useless line of code in this case :)

>
> Another style option would be to use a ?: like any of
>
> return sysfs_emit(buf, "%sready\n", csio_is_hw_ready(hw) ? "" : "not ");
> or
> return sysfs_emit(buf, "%s\n", csio_is_hw_ready(hw) ? "ready" : "not ready");
> or
> return sysfs_emit(buf, csio_is_hw_ready(hw) ? "ready\n" : "not ready\n");

That is nice and can make that

return sysfs_emit(buf, "%sready\n", csio_is_hw_ready(hw) ? "" : "not ");

too :)

--
Damien Le Moal
Western Digital Research