mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] network/: strlcpy instead of strncpy
@ 2021-04-12 19:42 Guilherme Janczak
  2021-04-12 21:41 ` Guilherme Janczak
  0 siblings, 1 reply; 3+ messages in thread
From: Guilherme Janczak @ 2021-04-12 19:42 UTC (permalink / raw)
  To: musl

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;

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-12 22:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-12 19:42 [musl] [PATCH] network/: strlcpy instead of strncpy Guilherme Janczak
2021-04-12 21:41 ` Guilherme Janczak
2021-04-12 22:25   ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).