--- cyrus-imapd-2.4.19/lib/util.c.orig 2017-05-15 07:43:30.000000000 +0200 +++ cyrus-imapd-2.4.19/lib/util.c 2017-05-17 15:08:09.000000000 +0200 @@ -360,23 +360,36 @@ */ int cyrus_mkdir(const char *path, mode_t mode __attribute__((unused))) { - char *p = (char *) path; + char *p, *npath; int save_errno; struct stat sbuf; - while ((p = strchr(p+1, '/'))) { + /* work with a copy */ + npath = xstrdup(path); + + /* skip leading slashes */ + p = npath; + while (*p == '/') + p++; + + while ((p = strchr(p, '/'))) { *p = '\0'; - if (mkdir(path, 0755) == -1 && errno != EEXIST) { + if (mkdir(npath, 0755) == -1 && errno != EEXIST) { save_errno = errno; - if (stat(path, &sbuf) == -1) { + if (stat(npath, &sbuf) == -1) { errno = save_errno; - syslog(LOG_ERR, "IOERROR: creating directory %s: %m", path); + syslog(LOG_ERR, "IOERROR: creating directory %s: %m", npath); + free (npath); return -1; } } - *p = '/'; + /* restore slash and skip multiple following slashes */ + *p++ = '/'; + while (*p == '/') + p++; } + free (npath); return 0; }