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.
This commit is contained in:
1 parent
9d0cae9372
commit
743d241080
1 file changed
+13
-2
@@ -1,19 +1,30 @@
|
|||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.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) {
|
char *getlogin(void) {
|
||||||
return getenv("USER");
|
return getenv("USER");
|
||||||
}
|
}
|
||||||
|
|
||||||
int getlogin_r(char *buf, size_t bufsize) {
|
int getlogin_r(char *buf, size_t bufsize) {
|
||||||
strncpy(buf, getlogin(), bufsize);
|
return strlcpy(buf, getlogin(), bufsize);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct passwd *getpwuid(uid_t uid) {
|
struct passwd *getpwuid(uid_t uid) {
|
||||||
|
|||||||
Reference in new issue
Block a user