]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
avoid segfault on git-unshare
authorMichel Normand <normand@fr.ibm.com>
Thu, 14 May 2009 13:11:39 +0000 (15:11 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 14 May 2009 13:11:39 +0000 (15:11 +0200)
avoid segfault when invalid parameter
git-unshare -u 666666

and improve error reporting

Signed-off-by: Michel Normand <normand@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/lxc_unshare.c

index f96672d74f8e50414901c4144bbd1d700bb9589b..6a021cc8e68082b27c9f36345ba090921c8e8e81 100644 (file)
@@ -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;
                }