From: Ivana Hutarova Varekova Date: Fri, 22 Jun 2012 13:38:34 +0000 (+0200) Subject: cg_build_path adds needless "/" character X-Git-Tag: v0.41~110 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32a8597ce577e34d0dd8cda8d6f8fbaeebcb9987;p=thirdparty%2Flibcgroup.git cg_build_path adds needless "/" character cg_build_path adds needless "/" character in cases when the input path ends/starts with character "/" itself. This character is added now only in cases it is wanted and it was not set. Changelog - incorporate Jan's feedback - fix typo in patch description, use snprintf instead of sprintf - deal with the situation if directory name is empty as well - move the path concatenation functionality to a separate function Example: Old version: cg_build_path("", path, memory); returns /sys/fs/cgroup/memory// cg_build_path("/3", path, memory); returns /sys/fs/cgroup/memory//3/ cg_build_path("3/", path, memory); returns /sys/fs/cgroup/memory/3// New version: cg_build_path("", path, memory); returns /sys/fs/cgroup/memory/ cg_build_path("/3", path, memory); returns /sys/fs/cgroup/memory/3/ cg_build_path("3/", path, memory); returns /sys/fs/cgroup/memory/3/ Signed-off-by: Ivana Hutarova Varekova Acked-by: Dhaval Giani --- diff --git a/src/api.c b/src/api.c index 29de7770..f050df70 100644 --- a/src/api.c +++ b/src/api.c @@ -1099,6 +1099,20 @@ static inline pid_t cg_gettid(void) return syscall(__NR_gettid); } +static char *cg_concat_path(const char *pref, const char *suf, char *path) +{ + if ((suf[strlen(suf)-1] == '/') || + ((strlen(suf) == 0) && (pref[strlen(pref)-1] == '/'))) { + snprintf(path, FILENAME_MAX, "%s%s", pref, + suf+((suf[0] == '/') ? 1 : 0)); + } else { + snprintf(path, FILENAME_MAX, "%s%s/", pref, + suf+((suf[0] == '/') ? 1 : 0)); + } + path[FILENAME_MAX-1] = '\0'; + return path; +} + /* Call with cg_mount_table_lock taken */ /* path value have to have size at least FILENAME_MAX */ @@ -1125,9 +1139,7 @@ static char *cg_build_path_locked(const char *name, char *path, /* FIXME: missing OOM check here! */ - snprintf(path, FILENAME_MAX, "%s%s/", - tmp, name); - path[FILENAME_MAX-1] = '\0'; + cg_concat_path(tmp, name, path); free(tmp); } return path;