pkgconf/libpkgconf/dependency.c

488 lines
14 KiB
C
Raw Normal View History

/*
* dependency.c
* dependency parsing and management
*
* Copyright (c) 2011, 2012, 2013 pkgconf authors (see AUTHORS).
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <libpkgconf/stdinc.h>
2015-09-06 14:35:08 +00:00
#include <libpkgconf/libpkgconf.h>
/*
2016-12-11 01:57:26 +00:00
* !doc
*
2016-12-11 01:57:26 +00:00
* libpkgconf `dependency` module
* ==============================
*
* The `dependency` module provides support for building `dependency lists` (the basic component of the overall `dependency graph`) and
* `dependency nodes` which store dependency information.
*/
2016-12-11 01:57:26 +00:00
typedef enum {
OUTSIDE_MODULE = 0,
INSIDE_MODULE_NAME = 1,
BEFORE_OPERATOR = 2,
INSIDE_OPERATOR = 3,
AFTER_OPERATOR = 4,
INSIDE_VERSION = 5
} parse_state_t;
#define DEBUG_PARSE 0
static const char *
dependency_to_str(const pkgconf_dependency_t *dep, char *buf, size_t buflen)
{
pkgconf_strlcpy(buf, dep->package, buflen);
if (dep->version != NULL)
{
pkgconf_strlcat(buf, " ", buflen);
pkgconf_strlcat(buf, pkgconf_pkg_get_comparator(dep), buflen);
pkgconf_strlcat(buf, " ", buflen);
pkgconf_strlcat(buf, dep->version, buflen);
}
return buf;
}
/* find a colliding dependency that is coloured differently */
static inline pkgconf_dependency_t *
find_colliding_dependency(const pkgconf_dependency_t *dep, const pkgconf_list_t *list)
{
const pkgconf_node_t *n;
PKGCONF_FOREACH_LIST_ENTRY(list->head, n)
{
pkgconf_dependency_t *dep2 = n->data;
if (strcmp(dep->package, dep2->package))
continue;
if (dep->flags != dep2->flags)
return dep2;
}
return NULL;
}
static inline pkgconf_dependency_t *
add_or_replace_dependency_node(pkgconf_client_t *client, pkgconf_dependency_t *dep, pkgconf_list_t *list)
{
char depbuf[PKGCONF_ITEM_SIZE];
pkgconf_dependency_t *dep2 = find_colliding_dependency(dep, list);
/* there is already a node in the graph which describes this dependency */
if (dep2 != NULL)
{
char depbuf2[PKGCONF_ITEM_SIZE];
PKGCONF_TRACE(client, "dependency collision: [%s/%x] -- [%s/%x]",
dependency_to_str(dep, depbuf, sizeof depbuf), dep->flags,
dependency_to_str(dep2, depbuf2, sizeof depbuf2), dep2->flags);
/* prefer the uncoloured node, either dep or dep2 */
if (dep->flags && dep2->flags == 0)
{
PKGCONF_TRACE(client, "dropping dependency [%s]@%p because of collision", depbuf, dep);
2021-10-06 17:48:13 +00:00
pkgconf_dependency_unref(dep->owner, dep);
return NULL;
}
else if (dep2->flags && dep->flags == 0)
{
PKGCONF_TRACE(client, "dropping dependency [%s]@%p because of collision", depbuf2, dep2);
pkgconf_node_delete(&dep2->iter, list);
2021-10-06 17:48:13 +00:00
pkgconf_dependency_unref(dep2->owner, dep2);
}
else
/* If both dependencies have equal strength, we keep both, because of situations like:
* Requires: foo > 1, foo < 3
*
* If the situation is that both dependencies are literally equal, it is still harmless because
* fragment deduplication will handle the excessive fragments.
*/
PKGCONF_TRACE(client, "keeping both dependencies (harmless)");
}
PKGCONF_TRACE(client, "added dependency [%s] to list @%p; flags=%x", dependency_to_str(dep, depbuf, sizeof depbuf), list, dep->flags);
pkgconf_node_insert_tail(&dep->iter, pkgconf_dependency_ref(dep->owner, dep), list);
/* This dependency is intentionally unowned.
*
* Internally we have no use for the returned type, and usually just
* discard it. However, there is a publig pkgconf_dependency_add
* function, which references this return value before returning it,
* giving ownership at that point.
*/
return dep;
}
static inline pkgconf_dependency_t *
pkgconf_dependency_addraw(pkgconf_client_t *client, pkgconf_list_t *list, const char *package, size_t package_sz, const char *version, size_t version_sz, pkgconf_pkg_comparator_t compare, unsigned int flags)
{
pkgconf_dependency_t *dep;
dep = calloc(sizeof(pkgconf_dependency_t), 1);
dep->package = pkgconf_strndup(package, package_sz);
if (version_sz != 0)
dep->version = pkgconf_strndup(version, version_sz);
dep->compare = compare;
dep->flags = flags;
2021-10-06 17:48:13 +00:00
dep->owner = client;
dep->refcount = 0;
return add_or_replace_dependency_node(client, dep, list);
}
2016-12-11 01:57:26 +00:00
/*
* !doc
*
* .. c:function:: pkgconf_dependency_t *pkgconf_dependency_add(pkgconf_list_t *list, const char *package, const char *version, pkgconf_pkg_comparator_t compare)
*
* Adds a parsed dependency to a dependency list as a dependency node.
*
* :param pkgconf_client_t* client: The client object that owns the package this dependency list belongs to.
2016-12-11 01:57:26 +00:00
* :param pkgconf_list_t* list: The dependency list to add a dependency node to.
* :param char* package: The package `atom` to set on the dependency node.
* :param char* version: The package `version` to set on the dependency node.
* :param pkgconf_pkg_comparator_t compare: The comparison operator to set on the dependency node.
* :param uint flags: Any flags to attach to the dependency node.
2016-12-11 01:57:26 +00:00
* :return: A dependency node.
* :rtype: pkgconf_dependency_t *
*/
pkgconf_dependency_t *
pkgconf_dependency_add(pkgconf_client_t *client, pkgconf_list_t *list, const char *package, const char *version, pkgconf_pkg_comparator_t compare, unsigned int flags)
{
pkgconf_dependency_t *dep;
dep = pkgconf_dependency_addraw(client, list, package, strlen(package), version,
version != NULL ? strlen(version) : 0, compare, flags);
return pkgconf_dependency_ref(dep->owner, dep);
}
2016-12-11 01:57:26 +00:00
/*
* !doc
*
* .. c:function:: void pkgconf_dependency_append(pkgconf_list_t *list, pkgconf_dependency_t *tail)
*
* Adds a dependency node to a pre-existing dependency list.
*
* :param pkgconf_list_t* list: The dependency list to add a dependency node to.
* :param pkgconf_dependency_t* tail: The dependency node to add to the tail of the dependency list.
* :return: nothing
*/
void
pkgconf_dependency_append(pkgconf_list_t *list, pkgconf_dependency_t *tail)
{
pkgconf_node_insert_tail(&tail->iter, tail, list);
}
/*
* !doc
*
* .. c:function:: void pkgconf_dependency_free_one(pkgconf_dependency_t *dep)
*
* Frees a dependency node.
*
* :param pkgconf_dependency_t* dep: The dependency node to free.
* :return: nothing
*/
void
pkgconf_dependency_free_one(pkgconf_dependency_t *dep)
{
if (dep->match != NULL)
pkgconf_pkg_unref(dep->match->owner, dep->match);
if (dep->package != NULL)
free(dep->package);
if (dep->version != NULL)
free(dep->version);
free(dep);
}
2021-10-06 17:48:13 +00:00
/*
* !doc
*
* .. c:function:: pkgconf_dependency_t *pkgconf_dependency_ref(pkgconf_client_t *owner, pkgconf_dependency_t *dep)
*
* Increases a dependency node's refcount.
*
* :param pkgconf_client_t* owner: The client object which owns the memory of this dependency node.
* :param pkgconf_dependency_t* dep: The dependency to increase the refcount of.
* :return: the dependency node on success, else NULL
*/
pkgconf_dependency_t *
pkgconf_dependency_ref(pkgconf_client_t *client, pkgconf_dependency_t *dep)
{
if (client != dep->owner)
return NULL;
dep->refcount++;
PKGCONF_TRACE(client, "%s refcount@%p: %d", dep->package, dep, dep->refcount);
2021-10-06 17:48:13 +00:00
return dep;
}
/*
* !doc
*
* .. c:function:: void pkgconf_dependency_unref(pkgconf_client_t *owner, pkgconf_dependency_t *dep)
*
* Decreases a dependency node's refcount and frees it if necessary.
*
* :param pkgconf_client_t* owner: The client object which owns the memory of this dependency node.
* :param pkgconf_dependency_t* dep: The dependency to decrease the refcount of.
* :return: nothing
*/
void
pkgconf_dependency_unref(pkgconf_client_t *client, pkgconf_dependency_t *dep)
{
if (client != dep->owner)
return;
--dep->refcount;
PKGCONF_TRACE(client, "%s refcount@%p: %d", dep->package, dep, dep->refcount);
if (dep->refcount <= 0)
2021-10-06 17:48:13 +00:00
pkgconf_dependency_free_one(dep);
}
2016-12-11 01:57:26 +00:00
/*
* !doc
*
* .. c:function:: void pkgconf_dependency_free(pkgconf_list_t *list)
*
* Release a dependency list and it's child dependency nodes.
*
* :param pkgconf_list_t* list: The dependency list to release.
* :return: nothing
*/
void
pkgconf_dependency_free(pkgconf_list_t *list)
{
pkgconf_node_t *node, *next;
PKGCONF_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
{
pkgconf_dependency_t *dep = node->data;
pkgconf_node_delete(&dep->iter, list);
2021-10-06 17:48:13 +00:00
pkgconf_dependency_unref(dep->owner, dep);
}
2022-08-04 18:53:02 +00:00
pkgconf_list_zero(list);
}
2016-12-11 01:57:26 +00:00
/*
* !doc
*
* .. c:function:: void pkgconf_dependency_parse_str(pkgconf_list_t *deplist_head, const char *depends)
*
* Parse a dependency declaration into a dependency list.
* Commas are counted as whitespace to allow for constructs such as ``@SUBSTVAR@, zlib`` being processed
* into ``, zlib``.
*
* :param pkgconf_client_t* client: The client object that owns the package this dependency list belongs to.
2016-12-11 01:57:26 +00:00
* :param pkgconf_list_t* deplist_head: The dependency list to populate with dependency nodes.
* :param char* depends: The dependency data to parse.
* :param uint flags: Any flags to attach to the dependency nodes.
2016-12-11 01:57:26 +00:00
* :return: nothing
*/
void
pkgconf_dependency_parse_str(pkgconf_client_t *client, pkgconf_list_t *deplist_head, const char *depends, unsigned int flags)
{
parse_state_t state = OUTSIDE_MODULE;
pkgconf_pkg_comparator_t compare = PKGCONF_CMP_ANY;
char cmpname[PKGCONF_ITEM_SIZE];
char buf[PKGCONF_BUFSIZE];
size_t package_sz = 0, version_sz = 0;
char *start = buf;
char *ptr = buf;
char *vstart = NULL;
char *package = NULL, *version = NULL;
char *cnameptr = cmpname;
char *cnameend = cmpname + PKGCONF_ITEM_SIZE - 1;
memset(cmpname, '\0', sizeof cmpname);
pkgconf_strlcpy(buf, depends, sizeof buf);
pkgconf_strlcat(buf, " ", sizeof buf);
while (*ptr)
{
switch (state)
{
case OUTSIDE_MODULE:
if (!PKGCONF_IS_MODULE_SEPARATOR(*ptr))
state = INSIDE_MODULE_NAME;
break;
case INSIDE_MODULE_NAME:
Avoid undefined behaviour with the ctype(3) functions. fix https://github.com/pkgconf/pkgconf/issues/291 As defined in the C standard: In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined. This is because they're designed to work with the int values returned by getc or fgetc; they need extra work to handle a char value. If EOF is -1 (as it almost always is), with 8-bit bytes, the allowed inputs to the ctype(3) functions are: {-1, 0, 1, 2, 3, ..., 255}. However, on platforms where char is signed, such as x86 with the usual ABI, code like char *ptr = ...; ... isspace(*ptr) ... may pass in values in the range: {-128, -127, -126, ..., -2, -1, 0, 1, ..., 127}. This has two problems: 1. Inputs in the set {-128, -127, -126, ..., -2} are forbidden. 2. The non-EOF byte 0xff is conflated with the value EOF = -1, so even though the input is not forbidden, it may give the wrong answer. Casting char to unsigned int first before passing the result to ctype(3) doesn't help: inputs like -128 are unchanged by this cast, because (on a two's-complement machine with 32-bit int and unsigned int), converting the signed char with integer value -128 to unsigned int gives integer value 2^32 - 128 = 0xffffff80, which is out of range, and which is converted in int back to -128, which is also out of range. It is necessary to cast char inputs to unsigned char first; you can then cast to unsigned int if you like but there's no need because the functions will always convert the argument to int by definition. So the above fragment needs to be: char *ptr = ...; ... isspace((unsigned char)*ptr) ... This patch changes unsigned int casts to unsigned char casts, and adds unsigned char casts where they are missing.
2023-03-17 19:32:58 +00:00
if (isspace((unsigned char)*ptr))
{
const char *sptr = ptr;
Avoid undefined behaviour with the ctype(3) functions. fix https://github.com/pkgconf/pkgconf/issues/291 As defined in the C standard: In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined. This is because they're designed to work with the int values returned by getc or fgetc; they need extra work to handle a char value. If EOF is -1 (as it almost always is), with 8-bit bytes, the allowed inputs to the ctype(3) functions are: {-1, 0, 1, 2, 3, ..., 255}. However, on platforms where char is signed, such as x86 with the usual ABI, code like char *ptr = ...; ... isspace(*ptr) ... may pass in values in the range: {-128, -127, -126, ..., -2, -1, 0, 1, ..., 127}. This has two problems: 1. Inputs in the set {-128, -127, -126, ..., -2} are forbidden. 2. The non-EOF byte 0xff is conflated with the value EOF = -1, so even though the input is not forbidden, it may give the wrong answer. Casting char to unsigned int first before passing the result to ctype(3) doesn't help: inputs like -128 are unchanged by this cast, because (on a two's-complement machine with 32-bit int and unsigned int), converting the signed char with integer value -128 to unsigned int gives integer value 2^32 - 128 = 0xffffff80, which is out of range, and which is converted in int back to -128, which is also out of range. It is necessary to cast char inputs to unsigned char first; you can then cast to unsigned int if you like but there's no need because the functions will always convert the argument to int by definition. So the above fragment needs to be: char *ptr = ...; ... isspace((unsigned char)*ptr) ... This patch changes unsigned int casts to unsigned char casts, and adds unsigned char casts where they are missing.
2023-03-17 19:32:58 +00:00
while (*sptr && isspace((unsigned char)*sptr))
sptr++;
if (*sptr == '\0')
state = OUTSIDE_MODULE;
else if (PKGCONF_IS_MODULE_SEPARATOR(*sptr))
state = OUTSIDE_MODULE;
else if (PKGCONF_IS_OPERATOR_CHAR(*sptr))
state = BEFORE_OPERATOR;
else
state = OUTSIDE_MODULE;
}
else if (PKGCONF_IS_MODULE_SEPARATOR(*ptr))
state = OUTSIDE_MODULE;
else if (*(ptr + 1) == '\0')
{
ptr++;
state = OUTSIDE_MODULE;
}
if (state != INSIDE_MODULE_NAME && start != ptr)
{
char *iter = start;
while (PKGCONF_IS_MODULE_SEPARATOR(*iter))
iter++;
package = iter;
package_sz = ptr - iter;
start = ptr;
}
if (state == OUTSIDE_MODULE)
{
pkgconf_dependency_addraw(client, deplist_head, package, package_sz, NULL, 0, compare, flags);
compare = PKGCONF_CMP_ANY;
package_sz = 0;
}
break;
case BEFORE_OPERATOR:
if (PKGCONF_IS_OPERATOR_CHAR(*ptr))
{
state = INSIDE_OPERATOR;
if (cnameptr < cnameend)
*cnameptr++ = *ptr;
}
break;
case INSIDE_OPERATOR:
if (!PKGCONF_IS_OPERATOR_CHAR(*ptr))
{
state = AFTER_OPERATOR;
compare = pkgconf_pkg_comparator_lookup_by_name(cmpname);
}
else if (cnameptr < cnameend)
*cnameptr++ = *ptr;
break;
case AFTER_OPERATOR:
Avoid undefined behaviour with the ctype(3) functions. fix https://github.com/pkgconf/pkgconf/issues/291 As defined in the C standard: In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF. If the argument has any other value, the behavior is undefined. This is because they're designed to work with the int values returned by getc or fgetc; they need extra work to handle a char value. If EOF is -1 (as it almost always is), with 8-bit bytes, the allowed inputs to the ctype(3) functions are: {-1, 0, 1, 2, 3, ..., 255}. However, on platforms where char is signed, such as x86 with the usual ABI, code like char *ptr = ...; ... isspace(*ptr) ... may pass in values in the range: {-128, -127, -126, ..., -2, -1, 0, 1, ..., 127}. This has two problems: 1. Inputs in the set {-128, -127, -126, ..., -2} are forbidden. 2. The non-EOF byte 0xff is conflated with the value EOF = -1, so even though the input is not forbidden, it may give the wrong answer. Casting char to unsigned int first before passing the result to ctype(3) doesn't help: inputs like -128 are unchanged by this cast, because (on a two's-complement machine with 32-bit int and unsigned int), converting the signed char with integer value -128 to unsigned int gives integer value 2^32 - 128 = 0xffffff80, which is out of range, and which is converted in int back to -128, which is also out of range. It is necessary to cast char inputs to unsigned char first; you can then cast to unsigned int if you like but there's no need because the functions will always convert the argument to int by definition. So the above fragment needs to be: char *ptr = ...; ... isspace((unsigned char)*ptr) ... This patch changes unsigned int casts to unsigned char casts, and adds unsigned char casts where they are missing.
2023-03-17 19:32:58 +00:00
if (!isspace((unsigned char)*ptr))
{
vstart = ptr;
state = INSIDE_VERSION;
}
break;
case INSIDE_VERSION:
if (PKGCONF_IS_MODULE_SEPARATOR(*ptr) || *(ptr + 1) == '\0')
{
version = vstart;
version_sz = ptr - vstart;
state = OUTSIDE_MODULE;
pkgconf_dependency_addraw(client, deplist_head, package, package_sz, version, version_sz, compare, flags);
compare = PKGCONF_CMP_ANY;
cnameptr = cmpname;
memset(cmpname, 0, sizeof cmpname);
package_sz = 0;
}
if (state == OUTSIDE_MODULE)
start = ptr;
break;
}
ptr++;
}
}
2016-12-11 01:57:26 +00:00
/*
* !doc
*
* .. c:function:: void pkgconf_dependency_parse(const pkgconf_client_t *client, pkgconf_pkg_t *pkg, pkgconf_list_t *deplist, const char *depends)
*
* Preprocess dependency data and then process that dependency declaration into a dependency list.
* Commas are counted as whitespace to allow for constructs such as ``@SUBSTVAR@, zlib`` being processed
* into ``, zlib``.
*
* :param pkgconf_client_t* client: The client object that owns the package this dependency list belongs to.
* :param pkgconf_pkg_t* pkg: The package object that owns this dependency list.
* :param pkgconf_list_t* deplist: The dependency list to populate with dependency nodes.
* :param char* depends: The dependency data to parse.
* :param uint flags: Any flags to attach to the dependency nodes.
2016-12-11 01:57:26 +00:00
* :return: nothing
*/
void
pkgconf_dependency_parse(pkgconf_client_t *client, pkgconf_pkg_t *pkg, pkgconf_list_t *deplist, const char *depends, unsigned int flags)
{
char *kvdepends = pkgconf_tuple_parse(client, &pkg->vars, depends, pkg->flags);
pkgconf_dependency_parse_str(client, deplist, kvdepends, flags);
free(kvdepends);
}
/*
* !doc
*
* .. c:function:: pkgconf_dependency_t *pkgconf_dependency_copy(pkgconf_client_t *client, const pkgconf_dependency_t *dep)
*
* Copies a dependency node to a new one.
*
* :param pkgconf_client_t* client: The client object that will own this dependency.
* :param pkgconf_dependency_t* dep: The dependency node to copy.
* :return: a pointer to a new dependency node, else NULL
*/
pkgconf_dependency_t *
pkgconf_dependency_copy(pkgconf_client_t *client, const pkgconf_dependency_t *dep)
{
pkgconf_dependency_t *new_dep;
new_dep = calloc(sizeof(pkgconf_dependency_t), 1);
new_dep->package = strdup(dep->package);
if (dep->version != NULL)
new_dep->version = strdup(dep->version);
new_dep->compare = dep->compare;
new_dep->flags = dep->flags;
new_dep->owner = client;
new_dep->refcount = 0;
2022-06-26 15:02:37 +00:00
if (dep->match != NULL)
new_dep->match = pkgconf_pkg_ref(client, dep->match);
return pkgconf_dependency_ref(client, new_dep);
2022-06-26 15:02:37 +00:00
}