about summary refs log tree commit diff
path: root/src/legacy/cuserid.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2021-02-13 13:54:00 -0500
committerRich Felker <dalias@aerifal.cx>2021-02-13 13:59:09 -0500
commita75283d777ed1827ed247dbb465818a0ce371c8f (patch)
treeea89365e0cc573d89240fd9a4012d6bcc345f53d /src/legacy/cuserid.c
parentef137da6428c342baabd3bcf9b5e91f75acefa64 (diff)
downloadmusl-a75283d777ed1827ed247dbb465818a0ce371c8f.tar.gz
musl-a75283d777ed1827ed247dbb465818a0ce371c8f.tar.xz
musl-a75283d777ed1827ed247dbb465818a0ce371c8f.zip
cuserid: don't return truncated results
checking the length also drops the need to pull in snprintf.
Diffstat (limited to 'src/legacy/cuserid.c')
-rw-r--r--src/legacy/cuserid.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/legacy/cuserid.c b/src/legacy/cuserid.c
index fd7832e4..3ff115a1 100644
--- a/src/legacy/cuserid.c
+++ b/src/legacy/cuserid.c
@@ -2,6 +2,7 @@
 #include <pwd.h>
 #include <stdio.h>
 #include <unistd.h>
+#include <string.h>
 
 char *cuserid(char *buf)
 {
@@ -10,7 +11,10 @@ char *cuserid(char *buf)
 	long pwb[256];
 	if (getpwuid_r(geteuid(), &pw, (void *)pwb, sizeof pwb, &ppw))
 		return 0;
+	size_t len = strnlen(pw.pw_name, L_cuserid);
+	if (len == L_cuserid)
+		return 0;
 	if (!buf) buf = usridbuf;
-	snprintf(buf, L_cuserid, "%s", pw.pw_name);
+	memcpy(buf, pw.pw_name, len+1);
 	return buf;
 }