netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net-next] hinic: fix strncpy output truncated compile warnings
@ 2020-08-06  7:48 Luo bin
  2020-08-06 19:01 ` David Miller
  0 siblings, 1 reply; 4+ messages in thread
From: Luo bin @ 2020-08-06  7:48 UTC (permalink / raw)
  To: davem
  Cc: linux-kernel, netdev, luoxianjun, yin.yinshi, cloud.wangxiaoyun,
	chiqijun

fix the compile warnings of 'strncpy' output truncated before
terminating nul copying N bytes from a string of the same length

Signed-off-by: Luo bin <luobin9@huawei.com>
Reported-by: kernel test robot <lkp@intel.com>
---
 drivers/net/ethernet/huawei/hinic/hinic_devlink.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
index c6adc776f3c8..1dc948c07b94 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);
 	else
-		strncpy(level_str, "Unknown", strlen("Unknown"));
+		strncpy(level_str, "Unknown", sizeof(level_str));
 
 	if (level == FAULT_LEVEL_SERIOUS_FLR) {
 		err = devlink_fmsg_u32_pair_put(fmsg, "Function level err func_id",
@@ -388,9 +388,9 @@ static int fault_report_show(struct devlink_fmsg *fmsg,
 	int err;
 
 	if (event->type < FAULT_TYPE_MAX)
-		strncpy(type_str, fault_type[event->type], strlen(fault_type[event->type]));
+		strncpy(type_str, fault_type[event->type], strlen(fault_type[event->type]) + 1);
 	else
-		strncpy(type_str, "Unknown", strlen("Unknown"));
+		strncpy(type_str, "Unknown", sizeof(type_str));
 
 	err = devlink_fmsg_string_pair_put(fmsg, "Fault type", type_str);
 	if (err)
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next] hinic: fix strncpy output truncated compile warnings
  2020-08-06  7:48 [PATCH net-next] hinic: fix strncpy output truncated compile warnings Luo bin
@ 2020-08-06 19:01 ` David Miller
  2020-08-07  0:57   ` luobin (L)
  0 siblings, 1 reply; 4+ messages in thread
From: David Miller @ 2020-08-06 19:01 UTC (permalink / raw)
  To: luobin9
  Cc: linux-kernel, netdev, luoxianjun, yin.yinshi, cloud.wangxiaoyun,
	chiqijun

From: Luo bin <luobin9@huawei.com>
Date: Thu, 6 Aug 2020 15:48:30 +0800

> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
> index c6adc776f3c8..1dc948c07b94 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);
>  	else
> -		strncpy(level_str, "Unknown", strlen("Unknown"));
> +		strncpy(level_str, "Unknown", sizeof(level_str));
>  
>  	if (level == FAULT_LEVEL_SERIOUS_FLR) {

Please fix these cases consistently, either use the strlen()+1 pattern
or the "sizeof(destination)" one.

Probably sizeof(destination) is best.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next] hinic: fix strncpy output truncated compile warnings
  2020-08-06 19:01 ` David Miller
@ 2020-08-07  0:57   ` luobin (L)
  2020-08-07  2:02     ` luobin (L)
  0 siblings, 1 reply; 4+ messages in thread
From: luobin (L) @ 2020-08-07  0:57 UTC (permalink / raw)
  To: David Miller
  Cc: linux-kernel, netdev, luoxianjun, yin.yinshi, cloud.wangxiaoyun,
	chiqijun

On 2020/8/7 3:01, David Miller wrote:
> From: Luo bin <luobin9@huawei.com>
> Date: Thu, 6 Aug 2020 15:48:30 +0800
> 
>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
>> index c6adc776f3c8..1dc948c07b94 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);
>>  	else
>> -		strncpy(level_str, "Unknown", strlen("Unknown"));
>> +		strncpy(level_str, "Unknown", sizeof(level_str));
>>  
>>  	if (level == FAULT_LEVEL_SERIOUS_FLR) {
> 
> Please fix these cases consistently, either use the strlen()+1 pattern
> or the "sizeof(destination)" one.
> 
> Probably sizeof(destination) is best.
> .
> 
Will fix. Thanks. Level_str array is initialized to zero, so can't use the strlen()+1 pattern, I'll
use strlen()+1 consistently.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH net-next] hinic: fix strncpy output truncated compile warnings
  2020-08-07  0:57   ` luobin (L)
@ 2020-08-07  2:02     ` luobin (L)
  0 siblings, 0 replies; 4+ messages in thread
From: luobin (L) @ 2020-08-07  2:02 UTC (permalink / raw)
  To: David Miller
  Cc: linux-kernel, netdev, luoxianjun, yin.yinshi, cloud.wangxiaoyun,
	chiqijun

On 2020/8/7 8:57, luobin (L) wrote:
> On 2020/8/7 3:01, David Miller wrote:
>> From: Luo bin <luobin9@huawei.com>
>> Date: Thu, 6 Aug 2020 15:48:30 +0800
>>
>>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_devlink.c b/drivers/net/ethernet/huawei/hinic/hinic_devlink.c
>>> index c6adc776f3c8..1dc948c07b94 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);
>>>  	else
>>> -		strncpy(level_str, "Unknown", strlen("Unknown"));
>>> +		strncpy(level_str, "Unknown", sizeof(level_str));
>>>  
>>>  	if (level == FAULT_LEVEL_SERIOUS_FLR) {
>>
>> Please fix these cases consistently, either use the strlen()+1 pattern
>> or the "sizeof(destination)" one.
>>
>> Probably sizeof(destination) is best.
>> .
>>
> Will fix. Thanks. Level_str array is initialized to zero, so can't use the strlen()+1 pattern, I'll
> use strlen()+1 consistently.
> 
I have tried to use 'sizeof(level_str)' instead of 'strlen(fault_level[level]) + 1', but this will lead
to following compile warning:

In function ‘strncpy’,
    inlined from ‘chip_fault_show’ at drivers/net/ethernet/huawei/hinic/hinic_devlink.c:345:3:
./include/linux/string.h:297:30: warning: ‘__builtin_strncpy’ specified bound 17 equals destination size [-Wstringop-truncation]
  297 | #define __underlying_strncpy __builtin_strncpy

So I will use the strlen()+1 pattern consistently.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-07  2:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-06  7:48 [PATCH net-next] hinic: fix strncpy output truncated compile warnings Luo bin
2020-08-06 19:01 ` David Miller
2020-08-07  0:57   ` luobin (L)
2020-08-07  2:02     ` luobin (L)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).