Skip to contentSkip to conversation

hinic: fix strncpy output truncated compile warnings

PATCH In review Luo bin 2 participants 4 messages Aug 6, 2020 – Aug 7, 2020

Subsystems (3)
lore
Share & tools
lore

Luo bin

author
#0 lore raw

PATCH 1/1 hinic: fix strncpy output truncated compile warnings

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

---
 drivers/net/ethernet/huawei/hinic/hinic_devlink.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/huawei/hinic/hinic_devlink.c +4 -4
index c6adc776f3c8..1dc948c07b94 100644
@@ -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)
Signature · 1 line
2.17.1
maintainer
#1 lore raw

From: Luo bin <[email protected]>
Date: Thu, 6 Aug 2020 15:48:30 +0800

Quoted 5 lines
drivers/net/ethernet/huawei/hinic/hinic_devlink.c +0 -0
index c6adc776f3c8..1dc948c07b94 100644

drivers/net/ethernet/huawei/hinic/hinic_devlink.c static int chip_fault_show(struct devlink_fmsg *fmsg, · line 349

Quoted 10 lines
drivers/net/ethernet/huawei/hinic/hinic_devlink.c +2 -2
@@ -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.

author
#2 lore raw
Quoted 17 lines · David Miller

From: Luo bin <[email protected]>
Date: Thu, 6 Aug 2020 15:48:30 +0800

Quoted 14 lines
drivers/net/ethernet/huawei/hinic/hinic_devlink.c +2 -2
index c6adc776f3c8..1dc948c07b94 100644
@@ -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));
 
Quoted 8 lines

drivers/net/ethernet/huawei/hinic/hinic_devlink.c static int chip_fault_show(struct devlink_fmsg *fmsg, · line 349

Quoted 1 line
drivers/net/ethernet/huawei/hinic/hinic_devlink.c +0 -0
 	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.

author
#3 lore raw
Quoted 23 lines · luobin (L)
Quoted 22 lines · David Miller

From: Luo bin <[email protected]>
Date: Thu, 6 Aug 2020 15:48:30 +0800

Quoted 5 lines
drivers/net/ethernet/huawei/hinic/hinic_devlink.c +0 -0
index c6adc776f3c8..1dc948c07b94 100644

drivers/net/ethernet/huawei/hinic/hinic_devlink.c static int chip_fault_show(struct devlink_fmsg *fmsg, · line 349

Quoted 10 lines
drivers/net/ethernet/huawei/hinic/hinic_devlink.c +2 -2
@@ -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.

Quoted 6 lines
Quoted 3 lines

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.