mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Guilherme Janczak <guilherme.janczak@yandex.com>
To: musl@lists.openwall.com
Subject: [musl] [PATCH] network/: strlcpy instead of strncpy
Date: Mon, 12 Apr 2021 19:42:07 +0000	[thread overview]
Message-ID: <20210412194207.GF37941@jan-obsd-z87.my.domain> (raw)

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;

             reply	other threads:[~2021-04-12 19:42 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-12 19:42 Guilherme Janczak [this message]
2021-04-12 21:41 ` Guilherme Janczak
2021-04-12 22:25   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210412194207.GF37941@jan-obsd-z87.my.domain \
    --to=guilherme.janczak@yandex.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).