Skip to contentSkip to conversation

net/hinic: fix snprintf length

PATCH Superseded Kevin Traynor 2 participants 5 messages Mar 6, 2020 – Mar 9, 2020 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 net/hinic: fix snprintf length

Correct length and remove unecessary brackets.

Fixes d9ce1917941c ("net/hinic/base: add hardware operation")

---
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 drivers/net/hinic/base/hinic_pmd_hwdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/hinic/base/hinic_pmd_hwdev.c +1 -1
index 8b16897ad..b6c821a2a 100644
@@ -1391,5 +1391,5 @@ static void print_cable_info(struct hinic_link_info *info)
 
 	memcpy(tmp_vendor, info->vendor_name, sizeof(info->vendor_name));
-	snprintf(tmp_str, (sizeof(tmp_str) - 1),
+	snprintf(tmp_str, sizeof(tmp_str),
 		 "Vendor: %s, %s, %s, length: %um, max_speed: %uGbps",
 		 tmp_vendor, info->sfp_type ? "SFP" : "QSFP", port_type,
Signature · 1 line
2.21.1
#1 lore raw

PATCH 2/2 net/hinic: fix repeating log and length check

gcc 10.0.1 reports:

../drivers/net/hinic/base/hinic_pmd_hwdev.c: In function ‘print_cable_info’:
../drivers/net/hinic/base/hinic_pmd_hwdev.c:1398:3:
warning:
‘snprintf’ argument 4 may overlap destination object ‘tmp_str’ [-Wrestrict]

 1398 |   snprintf(tmp_str + strlen(tmp_str), (sizeof(tmp_str) - 1),
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1399 |     "%s, Temperature: %u", tmp_str,
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1400 |     info->cable_temp);
      |     ~~~~~~~~~~~~~~~~~

The warning is that tmp_str is in both src and dest.

Anyway, the current code is incorrect and because of the +strlen the existing string will be repeated twice and max length does not limit to the end of the string.

Fix by removing tmp_str from the src of snprintf and adding the correct max length.

Fixes d9ce1917941c ("net/hinic/base: add hardware operation")

---
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 drivers/net/hinic/base/hinic_pmd_hwdev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/hinic/base/hinic_pmd_hwdev.c +3 -3
index b6c821a2a..fd0292f84 100644
@@ -1396,7 +1396,7 @@ static void print_cable_info(struct hinic_link_info *info)
 		 info->cable_length, info->cable_max_speed);
 	if (info->port_type != LINK_PORT_COPPER)
-		snprintf(tmp_str + strlen(tmp_str), (sizeof(tmp_str) - 1),
-			 "%s, Temperature: %u", tmp_str,
-			 info->cable_temp);
+		snprintf(tmp_str + strlen(tmp_str),
+			 sizeof(tmp_str) - strlen(tmp_str),
+			 ", Temperature: %u", info->cable_temp);
 
 	PMD_DRV_LOG(INFO, "Cable information: %s", tmp_str);
Signature · 1 line
2.21.1
#2 lore raw

PATCH v2 1/2 net/hinic: fix snprintf length

In reply to Kevin Traynor

Correct length and remove unnecessary brackets.

Fixes d9ce1917941c ("net/hinic/base: add hardware operation")

---
v2: fix typo in commit msg

---
 drivers/net/hinic/base/hinic_pmd_hwdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/hinic/base/hinic_pmd_hwdev.c +1 -1
index 8b16897ad..b6c821a2a 100644
@@ -1391,5 +1391,5 @@ static void print_cable_info(struct hinic_link_info *info)
 
 	memcpy(tmp_vendor, info->vendor_name, sizeof(info->vendor_name));
-	snprintf(tmp_str, (sizeof(tmp_str) - 1),
+	snprintf(tmp_str, sizeof(tmp_str),
 		 "Vendor: %s, %s, %s, length: %um, max_speed: %uGbps",
 		 tmp_vendor, info->sfp_type ? "SFP" : "QSFP", port_type,
Signature · 1 line
2.21.1
#3 lore raw

PATCH v2 2/2 net/hinic: fix repeating log and length check

gcc 10.0.1 reports:

../drivers/net/hinic/base/hinic_pmd_hwdev.c: In function ‘print_cable_info’:
../drivers/net/hinic/base/hinic_pmd_hwdev.c:1398:3:
warning:
‘snprintf’ argument 4 may overlap destination object ‘tmp_str’ [-Wrestrict]

 1398 |   snprintf(tmp_str + strlen(tmp_str), (sizeof(tmp_str) - 1),
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1399 |     "%s, Temperature: %u", tmp_str,
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1400 |     info->cable_temp);
      |     ~~~~~~~~~~~~~~~~~

The warning is that tmp_str is in both src and dest.

Anyway, the current code is incorrect and because of the +strlen the existing string will be repeated twice and max length does not limit to the end of the string.

Fix by removing tmp_str from the src of snprintf and adding the correct max length.

Fixes d9ce1917941c ("net/hinic/base: add hardware operation")

---
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]

---
 drivers/net/hinic/base/hinic_pmd_hwdev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/net/hinic/base/hinic_pmd_hwdev.c +3 -3
index b6c821a2a..fd0292f84 100644
@@ -1396,7 +1396,7 @@ static void print_cable_info(struct hinic_link_info *info)
 		 info->cable_length, info->cable_max_speed);
 	if (info->port_type != LINK_PORT_COPPER)
-		snprintf(tmp_str + strlen(tmp_str), (sizeof(tmp_str) - 1),
-			 "%s, Temperature: %u", tmp_str,
-			 info->cable_temp);
+		snprintf(tmp_str + strlen(tmp_str),
+			 sizeof(tmp_str) - strlen(tmp_str),
+			 ", Temperature: %u", info->cable_temp);
 
 	PMD_DRV_LOG(INFO, "Cable information: %s", tmp_str);
Signature · 1 line
2.21.1