From 34611a66436ce259625ba3a9ef50b1d287e112ea Mon Sep 17 00:00:00 2001 From: Finlay Maroney Date: Fri, 10 Mar 2023 19:33:20 -0500 Subject: [PATCH] Avoid double slash when explicitly passing --root or --rootdir Passing a path with a trailing slash to --root or --rootdir can lead to a double slash at the start of paths. e.g. $ pacman --root / -v 2>1 | grep " //" Log File : //var/log/pacman.log In MSYS2, paths starting with // will hit the network and fail. Avoid this be explicitly stripping the trailing / from paths passed to these flags. Signed-off-by: Allan McRae --- src/pacman/conf.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pacman/conf.c b/src/pacman/conf.c index f9edf75b..b7599b9e 100644 --- a/src/pacman/conf.c +++ b/src/pacman/conf.c @@ -1136,15 +1136,22 @@ int setdefaults(config_t *c) #define SETDEFAULT(opt, val) if(!opt) { opt = val; if(!opt) { return -1; } } if(c->rootdir) { + char* rootdir = strdup(c->rootdir); + int rootdir_len = strlen(rootdir); + /* This removes trailing slashes from the root directory */ + if(rootdir[rootdir_len-1] == '/'){ + rootdir[rootdir_len-1] = '\0'; + } char path[PATH_MAX]; if(!c->dbpath) { - snprintf(path, PATH_MAX, "%s/%s", c->rootdir, &DBPATH[1]); + snprintf(path, PATH_MAX, "%s/%s", rootdir, &DBPATH[1]); SETDEFAULT(c->dbpath, strdup(path)); } if(!c->logfile) { - snprintf(path, PATH_MAX, "%s/%s", c->rootdir, &LOGFILE[1]); + snprintf(path, PATH_MAX, "%s/%s", rootdir, &LOGFILE[1]); SETDEFAULT(c->logfile, strdup(path)); } + free(rootdir); } else { SETDEFAULT(c->rootdir, strdup(ROOTDIR)); SETDEFAULT(c->dbpath, strdup(DBPATH));