Commit 1615dc39 by Michel Normand Committed by Daniel Lezcano

avoid segfault on git-unshare

avoid segfault when invalid parameter git-unshare -u 666666 and improve error reporting Signed-off-by: 's avatarMichel Normand <normand@fr.ibm.com> Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent be43f17e
......@@ -51,22 +51,29 @@ void usage(char *cmd)
static uid_t lookup_user(const char *optarg)
{
int bufflen = sysconf(_SC_GETPW_R_SIZE_MAX);
char buff[bufflen];
char name[sysconf(_SC_LOGIN_NAME_MAX)];
uid_t uid = -1;
struct passwd pwent;
struct passwd *pent;
if (!optarg || (optarg[0] == '\0'))
return uid;
if (sscanf(optarg, "%u", &uid) < 1) {
struct passwd pwent; /* not a uid -- perhaps a username */
struct passwd *pent;
if (sscanf(optarg, "%u", &uid) < 1) {
/* not a uid -- perhaps a username */
if (sscanf(optarg, "%s", name) < 1)
return uid;
if (getpwnam_r(name, &pwent, NULL, 0, &pent) || !pent)
if (getpwnam_r(name, &pwent, buff, bufflen, &pent) || !pent) {
fprintf(stderr, "invalid username %s\n", name);
return uid;
}
uid = pent->pw_uid;
} else {
if (getpwuid_r(uid, NULL, NULL, 0, NULL)) {
if (getpwuid_r(uid, &pwent, buff, bufflen, &pent) || !pent) {
fprintf(stderr, "invalid uid %d\n", uid);
uid = -1;
return uid;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment