libxbps: ABI/API break due to xbps_pkg{,pattern}_name changes.

The funcs xbps_pkg_name() and xbps_pkgpattern_name() were
using malloc(3) to return the result, until now.

They now have been changed to not allocate the result
via malloc, the caller is responsible to provide a buffer
at least of XBPS_NAME_SIZE (64).

If for whatever reason the pkgname can't be guessed,
returns false. This should avoid lots of small allocs
around libxbps.

New functions have the following prototype:

bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
bool xbps_pkgpattern_name(char *dst, size_t len, const char *pkg)

as suggested by @duncaen.
This commit is contained in:
Juan RP
2020-02-08 19:31:29 +01:00
parent 1cda3017c3
commit 6010a24de6
35 changed files with 377 additions and 435 deletions

View File

@ -111,7 +111,7 @@ xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update)
{
xbps_dictionary_t pkgd = NULL, obsd = NULL;
xbps_array_t obsoletes = NULL;
char *pkgname, metafile[PATH_MAX];
char pkgname[XBPS_NAME_SIZE], metafile[PATH_MAX];
int rv = 0;
pkg_state_t state = 0;
uid_t euid;
@ -119,8 +119,9 @@ xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update)
assert(xhp);
assert(pkgver);
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
abort();
}
euid = geteuid();
@ -181,7 +182,6 @@ xbps_remove_pkg(struct xbps_handle *xhp, const char *pkgver, bool update)
* overwritten later in unpack phase.
*/
if (update) {
free(pkgname);
return 0;
}
@ -259,8 +259,6 @@ purge:
xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_DONE, 0, pkgver, NULL);
xbps_dictionary_remove(xhp->pkgdb, pkgname);
out:
if (pkgname != NULL)
free(pkgname);
if (rv != 0) {
xbps_set_cb_state(xhp, XBPS_STATE_REMOVE_FAIL, rv, pkgver,
"%s: failed to remove package: %s", pkgver, strerror(rv));