2012-05-07 03:32:53 +00:00
|
|
|
/*
|
|
|
|
* dependency.c
|
|
|
|
* dependency parsing and management
|
|
|
|
*
|
2013-02-24 14:21:36 +00:00
|
|
|
* Copyright (c) 2011, 2012, 2013 pkgconf authors (see AUTHORS).
|
2012-05-07 03:32:53 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2012-07-20 19:29:58 +00:00
|
|
|
* 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.
|
2012-05-07 03:32:53 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-18 04:38:25 +00:00
|
|
|
#include <libpkgconf/stdinc.h>
|
2015-09-06 14:35:08 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
/*
|
2016-12-11 01:57:26 +00:00
|
|
|
* !doc
|
2012-05-07 03:32:53 +00:00
|
|
|
*
|
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.
|
2012-05-07 03:32:53 +00:00
|
|
|
*/
|
2016-12-11 01:57:26 +00:00
|
|
|
|
2012-05-07 03:32:53 +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
|
|
|
|
|
2017-09-09 00:53:52 +00:00
|
|
|
static const char *
|
|
|
|
dependency_to_str(const pkgconf_dependency_t *dep, char *buf, size_t buflen)
|
2017-02-25 22:04:55 +00:00
|
|
|
{
|
2017-09-09 00:53:52 +00:00
|
|
|
pkgconf_strlcpy(buf, dep->package, buflen);
|
2017-02-25 22:04:55 +00:00
|
|
|
if (dep->version != NULL)
|
|
|
|
{
|
2017-09-09 00:53:52 +00:00
|
|
|
pkgconf_strlcat(buf, " ", buflen);
|
|
|
|
pkgconf_strlcat(buf, pkgconf_pkg_get_comparator(dep), buflen);
|
|
|
|
pkgconf_strlcat(buf, " ", buflen);
|
|
|
|
pkgconf_strlcat(buf, dep->version, buflen);
|
2017-02-25 22:04:55 +00:00
|
|
|
}
|
|
|
|
|
2017-09-09 00:53:52 +00:00
|
|
|
return buf;
|
2017-02-25 22:04:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 00:03:18 +00:00
|
|
|
/* 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 *
|
2021-10-06 17:52:18 +00:00
|
|
|
add_or_replace_dependency_node(pkgconf_client_t *client, pkgconf_dependency_t *dep, pkgconf_list_t *list)
|
2018-03-19 00:03:18 +00:00
|
|
|
{
|
|
|
|
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);
|
2018-03-19 00:03:18 +00:00
|
|
|
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);
|
2018-03-19 00:03:18 +00:00
|
|
|
}
|
|
|
|
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);
|
2022-08-03 22:43:04 +00:00
|
|
|
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;
|
2018-03-19 00:03:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-06 15:38:30 +00:00
|
|
|
static inline pkgconf_dependency_t *
|
2021-10-06 17:52:18 +00:00
|
|
|
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)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
2015-09-06 15:38:30 +00:00
|
|
|
pkgconf_dependency_t *dep;
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2023-11-05 22:17:02 +00:00
|
|
|
dep = calloc(1, sizeof(pkgconf_dependency_t));
|
2016-12-10 22:19:40 +00:00
|
|
|
dep->package = pkgconf_strndup(package, package_sz);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2012-08-14 15:13:28 +00:00
|
|
|
if (version_sz != 0)
|
2016-12-10 22:19:40 +00:00
|
|
|
dep->version = pkgconf_strndup(version, version_sz);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
dep->compare = compare;
|
2018-03-18 23:01:59 +00:00
|
|
|
dep->flags = flags;
|
2021-10-06 17:48:13 +00:00
|
|
|
dep->owner = client;
|
|
|
|
dep->refcount = 0;
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2018-03-19 00:03:18 +00:00
|
|
|
return add_or_replace_dependency_node(client, dep, list);
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
2017-02-25 22:04:55 +00:00
|
|
|
* :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.
|
2018-03-18 23:01:59 +00:00
|
|
|
* :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 *
|
|
|
|
*/
|
2016-08-27 04:40:15 +00:00
|
|
|
pkgconf_dependency_t *
|
2021-10-06 17:52:18 +00:00
|
|
|
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)
|
2016-08-27 04:40:15 +00:00
|
|
|
{
|
2022-08-03 22:43:04 +00:00
|
|
|
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-08-27 04:40:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2013-03-01 17:36:21 +00:00
|
|
|
void
|
2015-09-06 15:38:30 +00:00
|
|
|
pkgconf_dependency_append(pkgconf_list_t *list, pkgconf_dependency_t *tail)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_insert_tail(&tail->iter, tail, list);
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:18 +00:00
|
|
|
/*
|
|
|
|
* !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++;
|
2022-08-04 18:52:26 +00:00
|
|
|
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;
|
|
|
|
|
2022-08-04 18:52:26 +00:00
|
|
|
--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)
|
|
|
|
*
|
2023-11-24 18:44:04 +00:00
|
|
|
* Release a dependency list and its child dependency nodes.
|
2016-12-11 01:57:26 +00:00
|
|
|
*
|
|
|
|
* :param pkgconf_list_t* list: The dependency list to release.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
2012-05-07 03:32:53 +00:00
|
|
|
void
|
2015-09-06 15:38:30 +00:00
|
|
|
pkgconf_dependency_free(pkgconf_list_t *list)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
2015-09-06 15:31:21 +00:00
|
|
|
pkgconf_node_t *node, *next;
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2015-09-06 15:31:21 +00:00
|
|
|
PKGCONF_FOREACH_LIST_ENTRY_SAFE(list->head, next, node)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
2015-09-06 15:38:30 +00:00
|
|
|
pkgconf_dependency_t *dep = node->data;
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2021-10-06 17:29:18 +00:00
|
|
|
pkgconf_node_delete(&dep->iter, list);
|
2021-10-06 17:48:13 +00:00
|
|
|
pkgconf_dependency_unref(dep->owner, dep);
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
2022-08-04 18:53:02 +00:00
|
|
|
|
|
|
|
pkgconf_list_zero(list);
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
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``.
|
|
|
|
*
|
2017-02-25 22:04:55 +00:00
|
|
|
* :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.
|
2018-03-18 23:01:59 +00:00
|
|
|
* :param uint flags: Any flags to attach to the dependency nodes.
|
2016-12-11 01:57:26 +00:00
|
|
|
* :return: nothing
|
|
|
|
*/
|
2013-03-01 17:36:21 +00:00
|
|
|
void
|
2021-10-06 17:52:18 +00:00
|
|
|
pkgconf_dependency_parse_str(pkgconf_client_t *client, pkgconf_list_t *deplist_head, const char *depends, unsigned int flags)
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
parse_state_t state = OUTSIDE_MODULE;
|
2015-09-06 16:34:09 +00:00
|
|
|
pkgconf_pkg_comparator_t compare = PKGCONF_CMP_ANY;
|
2017-10-16 17:56:19 +00:00
|
|
|
char cmpname[PKGCONF_ITEM_SIZE];
|
2015-09-06 16:29:56 +00:00
|
|
|
char buf[PKGCONF_BUFSIZE];
|
2012-08-19 19:43:15 +00:00
|
|
|
size_t package_sz = 0, version_sz = 0;
|
2012-05-07 03:32:53 +00:00
|
|
|
char *start = buf;
|
|
|
|
char *ptr = buf;
|
|
|
|
char *vstart = NULL;
|
2012-08-14 15:13:28 +00:00
|
|
|
char *package = NULL, *version = NULL;
|
2013-02-24 13:41:18 +00:00
|
|
|
char *cnameptr = cmpname;
|
2020-05-26 19:42:13 +00:00
|
|
|
char *cnameend = cmpname + PKGCONF_ITEM_SIZE - 1;
|
2013-02-24 13:41:18 +00:00
|
|
|
|
|
|
|
memset(cmpname, '\0', sizeof cmpname);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2016-12-10 22:19:40 +00:00
|
|
|
pkgconf_strlcpy(buf, depends, sizeof buf);
|
|
|
|
pkgconf_strlcat(buf, " ", sizeof buf);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case OUTSIDE_MODULE:
|
2015-09-06 16:39:55 +00:00
|
|
|
if (!PKGCONF_IS_MODULE_SEPARATOR(*ptr))
|
2012-05-07 03:32:53 +00:00
|
|
|
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))
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
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))
|
2012-05-07 03:32:53 +00:00
|
|
|
sptr++;
|
|
|
|
|
|
|
|
if (*sptr == '\0')
|
|
|
|
state = OUTSIDE_MODULE;
|
2015-09-06 16:39:55 +00:00
|
|
|
else if (PKGCONF_IS_MODULE_SEPARATOR(*sptr))
|
2012-05-07 03:32:53 +00:00
|
|
|
state = OUTSIDE_MODULE;
|
2015-09-06 16:39:55 +00:00
|
|
|
else if (PKGCONF_IS_OPERATOR_CHAR(*sptr))
|
2012-05-07 03:32:53 +00:00
|
|
|
state = BEFORE_OPERATOR;
|
|
|
|
else
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
}
|
2015-09-06 16:39:55 +00:00
|
|
|
else if (PKGCONF_IS_MODULE_SEPARATOR(*ptr))
|
2012-05-07 03:32:53 +00:00
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
else if (*(ptr + 1) == '\0')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state != INSIDE_MODULE_NAME && start != ptr)
|
|
|
|
{
|
|
|
|
char *iter = start;
|
|
|
|
|
2015-09-06 16:39:55 +00:00
|
|
|
while (PKGCONF_IS_MODULE_SEPARATOR(*iter))
|
2012-05-07 03:32:53 +00:00
|
|
|
iter++;
|
|
|
|
|
2012-08-14 15:13:28 +00:00
|
|
|
package = iter;
|
|
|
|
package_sz = ptr - iter;
|
2012-05-07 03:32:53 +00:00
|
|
|
start = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == OUTSIDE_MODULE)
|
|
|
|
{
|
2018-03-18 23:01:59 +00:00
|
|
|
pkgconf_dependency_addraw(client, deplist_head, package, package_sz, NULL, 0, compare, flags);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2015-09-06 16:34:09 +00:00
|
|
|
compare = PKGCONF_CMP_ANY;
|
2012-08-19 19:43:15 +00:00
|
|
|
package_sz = 0;
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BEFORE_OPERATOR:
|
2015-09-06 16:39:55 +00:00
|
|
|
if (PKGCONF_IS_OPERATOR_CHAR(*ptr))
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
state = INSIDE_OPERATOR;
|
2020-05-26 19:42:13 +00:00
|
|
|
if (cnameptr < cnameend)
|
|
|
|
*cnameptr++ = *ptr;
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_OPERATOR:
|
2024-07-03 02:58:06 +00:00
|
|
|
if (PKGCONF_IS_OPERATOR_CHAR(*ptr))
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
2024-07-03 02:58:06 +00:00
|
|
|
if (cnameptr < cnameend)
|
|
|
|
*cnameptr++ = *ptr;
|
|
|
|
break;
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 02:58:06 +00:00
|
|
|
state = AFTER_OPERATOR;
|
|
|
|
compare = pkgconf_pkg_comparator_lookup_by_name(cmpname);
|
|
|
|
// fallthrough
|
2012-05-07 03:32:53 +00:00
|
|
|
|
|
|
|
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))
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
|
|
|
vstart = ptr;
|
|
|
|
state = INSIDE_VERSION;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INSIDE_VERSION:
|
2015-09-06 16:39:55 +00:00
|
|
|
if (PKGCONF_IS_MODULE_SEPARATOR(*ptr) || *(ptr + 1) == '\0')
|
2012-05-07 03:32:53 +00:00
|
|
|
{
|
2012-08-14 15:13:28 +00:00
|
|
|
version = vstart;
|
|
|
|
version_sz = ptr - vstart;
|
2012-05-07 03:32:53 +00:00
|
|
|
state = OUTSIDE_MODULE;
|
|
|
|
|
2018-03-18 23:01:59 +00:00
|
|
|
pkgconf_dependency_addraw(client, deplist_head, package, package_sz, version, version_sz, compare, flags);
|
2012-05-07 03:32:53 +00:00
|
|
|
|
2015-09-06 16:34:09 +00:00
|
|
|
compare = PKGCONF_CMP_ANY;
|
2013-03-16 00:44:11 +00:00
|
|
|
cnameptr = cmpname;
|
|
|
|
memset(cmpname, 0, sizeof cmpname);
|
2012-08-19 19:43:15 +00:00
|
|
|
package_sz = 0;
|
2012-05-07 03:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == OUTSIDE_MODULE)
|
|
|
|
start = ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
2012-07-02 02:44:30 +00:00
|
|
|
|
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.
|
2018-03-18 23:01:59 +00:00
|
|
|
* :param uint flags: Any flags to attach to the dependency nodes.
|
2016-12-11 01:57:26 +00:00
|
|
|
* :return: nothing
|
|
|
|
*/
|
2013-03-01 17:36:21 +00:00
|
|
|
void
|
2021-10-06 17:52:18 +00:00
|
|
|
pkgconf_dependency_parse(pkgconf_client_t *client, pkgconf_pkg_t *pkg, pkgconf_list_t *deplist, const char *depends, unsigned int flags)
|
2012-07-02 02:44:30 +00:00
|
|
|
{
|
2022-07-26 17:08:48 +00:00
|
|
|
char *kvdepends = pkgconf_tuple_parse(client, &pkg->vars, depends, pkg->flags);
|
2012-07-02 02:44:30 +00:00
|
|
|
|
2018-03-18 23:01:59 +00:00
|
|
|
pkgconf_dependency_parse_str(client, deplist, kvdepends, flags);
|
2012-07-02 02:44:30 +00:00
|
|
|
free(kvdepends);
|
|
|
|
}
|
2021-10-06 19:13:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* !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;
|
|
|
|
|
2023-11-05 22:17:02 +00:00
|
|
|
new_dep = calloc(1, sizeof(pkgconf_dependency_t));
|
2021-10-06 19:13:34 +00:00
|
|
|
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);
|
|
|
|
|
2021-10-06 19:13:34 +00:00
|
|
|
return pkgconf_dependency_ref(client, new_dep);
|
2022-06-26 15:02:37 +00:00
|
|
|
}
|