[v2,02/26] net/ntnic: handle string truncations when using strlcpy

Message ID 20250505071309.586015-3-okl-plv@napatech.com (mailing list archive)
State Awaiting Upstream
Delegated to: Stephen Hemminger
Headers
Series net/ntnic: fixes and improvements |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Oleksandr Kolomeiets May 5, 2025, 7:12 a.m. UTC
While strlcpy prevents out-of-bounds access,
it allows bugs to remain unnoticed.
If unexcepted truncation took place,
the respective warning message is emitted.

Signed-off-by: Oleksandr Kolomeiets <okl-plv@napatech.com>
---
 drivers/net/ntnic/ntnic_xstats/ntnic_xstats.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
  

Patch

diff --git a/drivers/net/ntnic/ntnic_xstats/ntnic_xstats.c b/drivers/net/ntnic/ntnic_xstats/ntnic_xstats.c
index cf3271d5de..a26cf5a07a 100644
--- a/drivers/net/ntnic/ntnic_xstats/ntnic_xstats.c
+++ b/drivers/net/ntnic/ntnic_xstats/ntnic_xstats.c
@@ -758,7 +758,10 @@  static int nthw_xstats_get_names(nt4ga_stat_t *p_nt4ga_stat,
 		return nb_names;
 
 	for (i = 0; i < size && i < nb_names; i++) {
-		strlcpy(xstats_names[i].name, names[i].name, sizeof(xstats_names[i].name));
+		size_t written =
+			strlcpy(xstats_names[i].name, names[i].name, sizeof(xstats_names[i].name));
+		if (written >= sizeof(xstats_names[i].name))
+			NT_LOG(WRN, NTNIC, "xstats name %s truncated", names[i].name);
 		count++;
 	}
 
@@ -795,9 +798,11 @@  static int nthw_xstats_get_names_by_id(nt4ga_stat_t *p_nt4ga_stat,
 
 	for (i = 0; i < size; i++) {
 		if (ids[i] < nb_names) {
-			strlcpy(xstats_names[i].name,
+			size_t written = strlcpy(xstats_names[i].name,
 				names[ids[i]].name,
 				RTE_ETH_XSTATS_NAME_SIZE);
+			if (written >= RTE_ETH_XSTATS_NAME_SIZE)
+				NT_LOG(WRN, NTNIC, "xstats name %s truncated", names[ids[i]].name);
 		}
 
 		count++;