From 743d241080dba6eed3627c08bb8679d488476953 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Fri, 26 May 2023 10:39:43 +0200 Subject: [PATCH] Use a variant of strlcpy to avoid unnecessary padding The GNU/Linux implementation of getlogin_r does not pad with zeroes for the rest of the buffer. When the bufsize is insufficient, it does not touch the buffer at all, but this divergent behavior is fine for now. --- fakeid.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fakeid.c b/fakeid.c index 0c6e0c4..43e8596 100644 --- a/fakeid.c +++ b/fakeid.c @@ -1,19 +1,30 @@ #define _GNU_SOURCE #include +#include #include #include #include #include #include +int strlcpy(char *restrict dest, const char *restrict src, size_t bufsize) { + size_t i; + for (i = 0; i < bufsize; ++i) { + dest[i] = src[i]; + if (src[i] == '\0') { + return 0; + } + } + return ERANGE; +} + char *getlogin(void) { return getenv("USER"); } int getlogin_r(char *buf, size_t bufsize) { - strncpy(buf, getlogin(), bufsize); - return 0; + return strlcpy(buf, getlogin(), bufsize); } struct passwd *getpwuid(uid_t uid) {