From 1722635e088e385757a7ef04494b4c23456986d8 Mon Sep 17 00:00:00 2001 From: Juan RP Date: Wed, 18 Feb 2015 15:12:39 +0100 Subject: [PATCH 3/3] Introduce xbps_sanitize_path() to fix #78 properly. This removes multiple slashes of a path and returns you a buffer with the sanitized string. --- include/xbps.h.in | 11 ++++++++++- lib/package_remove.c | 24 ++++++++++++++++-------- lib/util.c | 29 ++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/include/xbps.h.in b/include/xbps.h.in index aebe315..712d952 100644 --- include/xbps.h.in +++ include/xbps.h.in @@ -48,7 +48,7 @@ * * This header documents the full API for the XBPS Library. */ -#define XBPS_API_VERSION "20141209" +#define XBPS_API_VERSION "20150218" #ifndef XBPS_VERSION #define XBPS_VERSION "UNSET" @@ -1933,6 +1933,15 @@ int xbps_cmpver(const char *pkg1, const char *pkg2); */ char *xbps_pubkey2fp(struct xbps_handle *xhp, xbps_data_t pubkey); +/** + * Returns a buffer with a sanitized path from \a src. + * This removes multiple slashes. + * + * @return The sanitized path in a buffer. + * The returned buffer must be free(3)d when it's no longer necessary. + */ +char *xbps_sanitize_path(const char *src); + /*@}*/ #ifdef __cplusplus diff --git a/lib/package_remove.c b/lib/package_remove.c index 623a805..0b586e8 100644 --- lib/package_remove.c +++ lib/package_remove.c @@ -96,7 +96,7 @@ static char * symlink_target(struct xbps_handle *xhp, const char *path) { struct stat sb; - char *lnk, *res = NULL; + char *p, *p1, *dname, *lnk, *res = NULL; ssize_t r; if (lstat(path, &sb) == -1) @@ -112,8 +112,6 @@ symlink_target(struct xbps_handle *xhp, const char *path) } lnk[sb.st_size] = '\0'; if (strstr(lnk, "./") || lnk[0] != '/') { - char *p, *p1, *dname; - /* relative */ p = strdup(path); assert(p); @@ -121,16 +119,26 @@ symlink_target(struct xbps_handle *xhp, const char *path) assert(dname); p = xbps_xasprintf("%s/%s", dname, lnk); assert(p); - if (strstr(p, "./") && ((p1 = realpath(p, NULL)))) { - res = strdup(p1 + strlen(xhp->rootdir)); - free(p1); + p1 = xbps_sanitize_path(p); + assert(p1); + free(p); + if ((strstr(p1, "./")) && (p = realpath(p1, NULL))) { + if (strcmp(xhp->rootdir, "/") == 0) + res = strdup(p); + else + res = strdup(p + strlen(xhp->rootdir)); + + free(p); } if (res == NULL) { - res = strdup(p + strlen(xhp->rootdir)+1); + if (strcmp(xhp->rootdir, "/") == 0) + res = strdup(p1); + else + res = strdup(p1 + strlen(xhp->rootdir)); } assert(res); free(lnk); - free(p); + free(p1); } else { /* absolute */ res = lnk; diff --git a/lib/util.c b/lib/util.c index 0361a7c..72b8e7d 100644 --- lib/util.c +++ lib/util.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2008-2014 Juan Romero Pardines. + * Copyright (c) 2008-2015 Juan Romero Pardines. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -425,3 +425,30 @@ xbps_pkg_reverts(xbps_dictionary_t pkg, const char *pkgver) return false; } + +char * +xbps_sanitize_path(const char *src) +{ + const char *s = src; + char *d, *dest; + size_t len; + + assert(src); + len = strlen(src); + assert(len != 0); + + dest = malloc(len); + assert(dest); + d = dest; + + while ((*d = *s)) { + if (*s == '/' && *(s+1) == '/') { + s++; + continue; + } + d++, s++; + } + *d = '\0'; + + return dest; +} -- 2.3.0