From: Jan Safranek Date: Wed, 30 Nov 2011 14:42:50 +0000 (+0100) Subject: tools: Fixed parsing of SUID/SGID/sticky bits in -t and -a options. X-Git-Tag: v0.38~18 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3ecaa4679d34ebbe9951f6a1004d4b551d6121e6;p=thirdparty%2Flibcgroup.git tools: Fixed parsing of SUID/SGID/sticky bits in -t and -a options. Add support of 4-character octal numbers in parse_mode(). Signed-off-by: Jan Safranek --- diff --git a/src/tools/tools-common.c b/src/tools/tools-common.c index 8a739cee..c763ad65 100644 --- a/src/tools/tools-common.c +++ b/src/tools/tools-common.c @@ -239,33 +239,25 @@ int cgroup_string_list_add_directory(struct cgroup_string_list *list, /* allowed mode strings are octal version: "755" */ int parse_mode(char *string, mode_t *pmode, const char *program_name) { - mode_t mode = 0; - int pos = 0; /* position of the number iin string */ - int i; - int j = 64; - - while (pos < 3) { - if ('0' <= string[pos] && string[pos] < '8') { - i = (int)string[pos] - (int)'0'; - /* parse the permission triple*/ - mode = mode + i*j; - j = j / 8; - } else { - fprintf(stdout, "%s wrong mode format %s", - program_name, string); - return -1; - } - pos++; - } - - /* the string have contains three characters */ - if (string[pos] != '\0') { - fprintf(stdout, "%s wrong mode format %s", - program_name, string); - return -1; - } + char *end; + long mode; + size_t len; + + len = strlen(string); + if (len < 3 || len > 4) + goto err; + + errno = 0; + mode = strtol(string, &end, 8); + if (errno != 0 || *end != '\0') + goto err; *pmode = mode; return 0; + +err: + *pmode = 0; + fprintf(stdout, "%s wrong mode format %s", program_name, string); + return -1; } int parse_uid_gid(char *string, uid_t *uid, gid_t *gid,