Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Mon, 12 Apr 2021 19:42:07 +0000
From: Guilherme Janczak <guilherme.janczak@...dex.com>
To: musl@...ts.openwall.com
Subject: [PATCH] network/: strlcpy instead of strncpy

Hello, I'm sending 2 patches that do the same thing to 2 functions.
The patches replace strncpy with strlcpy.
As you can see neither function ensures null termination after strncpy,
but there doesn't seem to be an actual bug in their usage.


diff --git a/src/network/if_indextoname.c b/src/network/if_indextoname.c
index 3b368bf0..603204cc 100644
--- a/src/network/if_indextoname.c
+++ b/src/network/if_indextoname.c
@@ -19,5 +19,6 @@ char *if_indextoname(unsigned index, char *name)
 		if (errno == ENODEV) errno = ENXIO;
 		return 0;
 	}
-	return strncpy(name, ifr.ifr_name, IF_NAMESIZE);
+	strlcpy(name, ifr.ifr_name, IF_NAMESIZE);
+	return name;
 }
diff --git a/src/network/if_nametoindex.c b/src/network/if_nametoindex.c
index 331413c6..75af31f0 100644
--- a/src/network/if_nametoindex.c
+++ b/src/network/if_nametoindex.c
@@ -11,7 +11,7 @@ unsigned if_nametoindex(const char *name)
 	int fd, r;
 
 	if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) return 0;
-	strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
+	strlcpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
 	r = ioctl(fd, SIOCGIFINDEX, &ifr);
 	__syscall(SYS_close, fd);
 	return r < 0 ? 0 : ifr.ifr_ifindex;

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.