Introduce xbps_sanitize_path() to fix #78 properly.

This removes multiple slashes of a path and returns you a buffer with
the sanitized string.
This commit is contained in:
Juan RP 2015-02-18 15:12:39 +01:00
parent 3c34c300d1
commit 1722635e08
3 changed files with 54 additions and 10 deletions

View File

@ -48,7 +48,7 @@
* *
* This header documents the full API for the XBPS Library. * This header documents the full API for the XBPS Library.
*/ */
#define XBPS_API_VERSION "20150203" #define XBPS_API_VERSION "20150218"
#ifndef XBPS_VERSION #ifndef XBPS_VERSION
#define XBPS_VERSION "UNSET" #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); 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 #ifdef __cplusplus

View File

@ -96,7 +96,7 @@ static char *
symlink_target(struct xbps_handle *xhp, const char *path) symlink_target(struct xbps_handle *xhp, const char *path)
{ {
struct stat sb; struct stat sb;
char *lnk, *res = NULL; char *p, *p1, *dname, *lnk, *res = NULL;
ssize_t r; ssize_t r;
if (lstat(path, &sb) == -1) if (lstat(path, &sb) == -1)
@ -112,8 +112,6 @@ symlink_target(struct xbps_handle *xhp, const char *path)
} }
lnk[sb.st_size] = '\0'; lnk[sb.st_size] = '\0';
if (strstr(lnk, "./") || lnk[0] != '/') { if (strstr(lnk, "./") || lnk[0] != '/') {
char *p, *p1, *dname;
/* relative */ /* relative */
p = strdup(path); p = strdup(path);
assert(p); assert(p);
@ -121,16 +119,26 @@ symlink_target(struct xbps_handle *xhp, const char *path)
assert(dname); assert(dname);
p = xbps_xasprintf("%s/%s", dname, lnk); p = xbps_xasprintf("%s/%s", dname, lnk);
assert(p); assert(p);
if (strstr(p, "./") && ((p1 = realpath(p, NULL)))) { p1 = xbps_sanitize_path(p);
res = strdup(p1 + strlen(xhp->rootdir)); assert(p1);
free(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) { 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); assert(res);
free(lnk); free(lnk);
free(p); free(p1);
} else { } else {
/* absolute */ /* absolute */
res = lnk; res = lnk;

View File

@ -1,5 +1,5 @@
/*- /*-
* Copyright (c) 2008-2014 Juan Romero Pardines. * Copyright (c) 2008-2015 Juan Romero Pardines.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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; 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;
}