Canonicalize paths before using them

This fixes a problem where on Windows the prefix would
not match if the prefix is generated with backslashes
and the rest of the variables use normal slashes
pull/187/head
Ignacio Casal Quinteiro 2018-09-17 15:20:28 +02:00
parent 9f17da92d2
commit 2c05971029
1 changed files with 44 additions and 2 deletions

View File

@ -188,6 +188,43 @@ determine_prefix(const pkgconf_pkg_t *pkg, char *buf, size_t buflen)
return buf; return buf;
} }
static void
remove_additional_separators(char *buf)
{
char *p = buf;
while (*p) {
if (*p == '/') {
char *q;
q = ++p;
while (*q && *q == '/')
q++;
if (p != q)
memmove (p, q, strlen (q) + 1);
} else {
p++;
}
}
}
static void
canonicalize_path(char *buf)
{
#ifdef _WIN32
char *p = buf;
while (*p) {
if (*p == '\\')
*p = '/';
p++;
}
#endif
remove_additional_separators(buf);
}
static bool static bool
is_path_prefix_equal(const char *path1, const char *path2, size_t path2_len) is_path_prefix_equal(const char *path1, const char *path2, size_t path2_len)
{ {
@ -201,8 +238,13 @@ is_path_prefix_equal(const char *path1, const char *path2, size_t path2_len)
static bool static bool
pkgconf_pkg_parser_value_set(pkgconf_pkg_t *pkg, const size_t lineno, const char *keyword, char *value) pkgconf_pkg_parser_value_set(pkgconf_pkg_t *pkg, const size_t lineno, const char *keyword, char *value)
{ {
char canonicalized_value[PKGCONF_ITEM_SIZE];
(void) lineno; (void) lineno;
pkgconf_strlcpy(canonicalized_value, value, sizeof canonicalized_value);
canonicalize_path(canonicalized_value);
/* Some pc files will use absolute paths for all of their directories /* Some pc files will use absolute paths for all of their directories
* which is broken when redefining the prefix. We try to outsmart the * which is broken when redefining the prefix. We try to outsmart the
* file and rewrite any directory that starts with the same prefix. * file and rewrite any directory that starts with the same prefix.
@ -213,7 +255,7 @@ pkgconf_pkg_parser_value_set(pkgconf_pkg_t *pkg, const size_t lineno, const char
char newvalue[PKGCONF_ITEM_SIZE]; char newvalue[PKGCONF_ITEM_SIZE];
pkgconf_strlcpy(newvalue, pkg->prefix->value, sizeof newvalue); pkgconf_strlcpy(newvalue, pkg->prefix->value, sizeof newvalue);
pkgconf_strlcat(newvalue, value + strlen(pkg->orig_prefix->value), sizeof newvalue); pkgconf_strlcat(newvalue, canonicalized_value + strlen(pkg->orig_prefix->value), sizeof newvalue);
pkgconf_tuple_add(pkg->owner, &pkg->vars, keyword, newvalue, false); pkgconf_tuple_add(pkg->owner, &pkg->vars, keyword, newvalue, false);
} }
else if (strcmp(keyword, pkg->owner->prefix_varname) || !(pkg->owner->flags & PKGCONF_PKG_PKGF_REDEFINE_PREFIX)) else if (strcmp(keyword, pkg->owner->prefix_varname) || !(pkg->owner->flags & PKGCONF_PKG_PKGF_REDEFINE_PREFIX))
@ -225,7 +267,7 @@ pkgconf_pkg_parser_value_set(pkgconf_pkg_t *pkg, const size_t lineno, const char
if (relvalue != NULL) if (relvalue != NULL)
{ {
pkg->orig_prefix = pkgconf_tuple_add(pkg->owner, &pkg->vars, "orig_prefix", value, true); pkg->orig_prefix = pkgconf_tuple_add(pkg->owner, &pkg->vars, "orig_prefix", canonicalized_value, true);
pkg->prefix = pkgconf_tuple_add(pkg->owner, &pkg->vars, keyword, relvalue, false); pkg->prefix = pkgconf_tuple_add(pkg->owner, &pkg->vars, keyword, relvalue, false);
} }
else else