Re: [PATCH net-next v1] hinic: fix strncpy output truncated compile warnings
From: David Miller
Date: Fri Aug 07 2020 - 23:42:53 EST
From: "luobin (L)" <luobin9@xxxxxxxxxx>
Date: Sat, 8 Aug 2020 11:36:42 +0800
> On 2020/8/7 17:32, David Laight wrote:
>>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
>>> b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
>>> index c6adc776f3c8..1ec88ebf81d6 100644
>>> --- a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
>>> +++ b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
>>> @@ -342,9 +342,9 @@ static int chip_fault_show(struct devlink_fmsg *fmsg,
>>>
>>> level = event->event.chip.err_level;
>>> if (level < FAULT_LEVEL_MAX)
>>> - strncpy(level_str, fault_level[level], strlen(fault_level[level]));
>>> + strncpy(level_str, fault_level[level], strlen(fault_level[level]) + 1);
>>
>> Have you even considered what that code is actually doing?
...
> I'm sorry that I haven't got what you mean and I haven't found any defects in that code. Can you explain more to me?
David is trying to express the same thing I was trying to explain to
you, you should use sizeof(level_str) as the third argument because
the code is trying to make sure that the destination buffer is not
overrun.
If you use the strlen() of the source buffer, the strncpy() can still
overflow the destination buffer.
Now do you understand?