Skip to contentSkip to conversation

usb: Replace snprintf with scnprintf in gether_get_ifname

PATCH New [email protected] 1 participant 1 message Jun 20, 2019 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: Replace snprintf with scnprintf in gether_get_ifname

From: Daniel M German <[email protected]>

snprintf returns the actual length of the buffer created; however, this is not the case if snprintf truncates its parameter. See https://lwn.net/Articles/69419/ for a detailed explanation. The current code correctly handles this case at the expense of extra code in the return statement.

scnprintf does returns the actual length of the buffer created making the ?: operator unnecessary in the return statement.

This change does not alter the functionality of the code.

---
 drivers/usb/gadget/function/u_ether.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/usb/gadget/function/u_ether.c +2 -2
index 737bd77a575d..329b4d2861ee 100644
@@ -1004,9 +1004,9 @@ int gether_get_ifname(struct net_device *net, char *name, int len)
 	int ret;
 
 	rtnl_lock();
-	ret = snprintf(name, len, "%s\n", netdev_name(net));
+	ret = scnprintf(name, len, "%s\n", netdev_name(net));
 	rtnl_unlock();
-	return ret < len ? ret : len;
+	return ret;
 }
 EXPORT_SYMBOL_GPL(gether_get_ifname);
 
Signature · 1 line
2.20.1