From: Michel Normand Date: Thu, 14 May 2009 13:11:39 +0000 (+0200) Subject: avoid segfault on git-unshare X-Git-Tag: lxc_0_6_3~105 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1615dc39e21bb11a3ec812515815749f0e28e3a9;p=thirdparty%2Flxc.git avoid segfault on git-unshare avoid segfault when invalid parameter git-unshare -u 666666 and improve error reporting Signed-off-by: Michel Normand Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/lxc_unshare.c b/src/lxc/lxc_unshare.c index f96672d74..6a021cc8e 100644 --- a/src/lxc/lxc_unshare.c +++ b/src/lxc/lxc_unshare.c @@ -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; }