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.
main
Clément FRÉVILLE 2 years ago
parent 9d0cae9372
commit 743d241080

@ -1,19 +1,30 @@
#define _GNU_SOURCE
#include <dlfcn.h>
#include <errno.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
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) {

Loading…
Cancel
Save