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

@@ -830,7 +830,7 @@ main(int argc, char **argv)
const char *arch, *config_files, *mutable_files, *version, *changelog;
const char *buildopts, *shlib_provides, *shlib_requires, *alternatives;
const char *compression, *tags = NULL, *srcrevs = NULL;
char *pkgname, *binpkg, *tname, *p, cwd[PATH_MAX-1];
char pkgname[XBPS_NAME_SIZE], *binpkg, *tname, *p, cwd[PATH_MAX-1];
bool quiet = false, preserve = false;
int c, pkg_fd;
mode_t myumask;
@@ -947,8 +947,7 @@ main(int argc, char **argv)
/*
* Sanity check for required options.
*/
pkgname = xbps_pkg_name(pkgver);
if (pkgname == NULL)
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
die("invalid pkgver! got `%s' expected `foo-1.0_1'", pkgver);
version = xbps_pkg_version(pkgver);
if (version == NULL)

View File

@@ -36,7 +36,7 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbdata UNUSED)
{
xbps_dictionary_t pkgd;
const char *instver, *newver;
char *pkgname;
char pkgname[XBPS_NAME_SIZE];
int rv = 0;
bool slog = false;
@@ -99,8 +99,9 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbdata UNUSED)
/* empty */
break;
case XBPS_STATE_UPDATE:
pkgname = xbps_pkg_name(xscd->arg);
assert(pkgname);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), xscd->arg)) {
abort();
}
newver = xbps_pkg_version(xscd->arg);
pkgd = xbps_pkgdb_get_pkg(xscd->xhp, pkgname);
xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &instver);
@@ -110,7 +111,6 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbdata UNUSED)
"(rootdir: %s)\n", instver, newver,
xscd->xhp->rootdir);
}
free(pkgname);
break;
/* success */
case XBPS_STATE_REMOVE_FILE:

View File

@@ -93,18 +93,18 @@ show_package_list(struct transaction *trans, const char *match, int cols)
if (match && strcmp(tract, "update") == 0) {
xbps_dictionary_t ipkgd;
const char *ipkgver, *iversion, *version;
char *pkgname;
char pkgname[XBPS_NAME_SIZE];
/* get installed pkgver */
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
abort();
}
ipkgd = xbps_pkgdb_get_pkg(trans->xhp, pkgname);
assert(ipkgd);
xbps_dictionary_get_cstring_nocopy(ipkgd, "pkgver", &ipkgver);
version = xbps_pkg_version(pkgver);
iversion = xbps_pkg_version(ipkgver);
buf = xbps_xasprintf("%s (%s -> %s)", pkgname, iversion, version);
free(pkgname);
}
if ((match && (strcmp(match, tract) == 0)) || (!match && dload)) {
if (buf) {

View File

@@ -82,15 +82,15 @@ find_longest_pkgname(struct transaction *trans)
{
xbps_object_t obj;
const char *pkgver;
char *pkgname;
char pkgname[XBPS_NAME_SIZE];
unsigned int len = 0, max = 0;
while ((obj = xbps_object_iterator_next(trans->iter)) != NULL) {
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
abort();
}
len = strlen(pkgname);
free(pkgname);
if (max == 0 || len > max)
max = len;
}
@@ -125,7 +125,7 @@ print_trans_colmode(struct transaction *trans, int cols)
while ((obj = xbps_object_iterator_next(trans->iter)) != NULL) {
xbps_dictionary_t ipkgd;
const char *pkgver, *ipkgver, *ver, *iver, *tract;
char *pkgname;
char pkgname[XBPS_NAME_SIZE];
bool dload = false;
ver = iver = NULL;
@@ -135,8 +135,9 @@ print_trans_colmode(struct transaction *trans, int cols)
xbps_dictionary_get_uint64(obj, "filename-size", &dlsize);
xbps_dictionary_get_bool(obj, "download", &dload);
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
abort();
}
if (trans->xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
tract = "download";
@@ -206,7 +207,6 @@ print_trans_colmode(struct transaction *trans, int cols)
/* print download size */
printf("%s ", size);
printf("\n");
free(pkgname);
}
xbps_object_iterator_reset(trans->iter);
return true;

View File

@@ -43,19 +43,19 @@ pkgdb_cb(struct xbps_handle *xhp UNUSED,
bool *done UNUSED)
{
const char *pkgver = NULL;
char *pkgname;
char pkgname[XBPS_NAME_SIZE];
int rv, *errors = (int *)arg;
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
if (xhp->flags & XBPS_FLAG_VERBOSE)
printf("Checking %s ...\n", pkgver);
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
abort();
}
if ((rv = check_pkg_integrity(xhp, obj, pkgname)) != 0)
*errors += 1;
free(pkgname);
return 0;
}

View File

@@ -252,7 +252,8 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
*/
for (int i = args; i < argmax; i++) {
const char *arch = NULL, *pkg = argv[i];
char *sha256 = NULL, *pkgver = NULL, *pkgname = NULL;
char *sha256 = NULL, *pkgver = NULL;
char pkgname[XBPS_NAME_SIZE];
assert(pkg);
/*
@@ -272,8 +273,9 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
free(pkgver);
continue;
}
pkgname = xbps_pkg_name(pkgver);
assert(pkgname);
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver)) {
abort();
}
/*
* Check if this package exists already in the index, but first
* checking the version. If current package version is greater
@@ -288,7 +290,6 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
rv = errno;
xbps_object_release(binpkgd);
free(pkgver);
free(pkgname);
goto out;
}
} else if (!force) {
@@ -320,7 +321,6 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
free(opkgver);
free(oarch);
free(pkgver);
free(pkgname);
continue;
}
free(opkgver);
@@ -334,7 +334,6 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
if ((sha256 = xbps_file_hash(pkg)) == NULL) {
xbps_object_release(binpkgd);
free(pkgver);
free(pkgname);
rv = EINVAL;
goto out;
}
@@ -342,7 +341,6 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
xbps_object_release(binpkgd);
free(sha256);
free(pkgver);
free(pkgname);
rv = EINVAL;
goto out;
}
@@ -350,14 +348,12 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
if (stat(pkg, &st) == -1) {
xbps_object_release(binpkgd);
free(pkgver);
free(pkgname);
rv = EINVAL;
goto out;
}
if (!xbps_dictionary_set_uint64(binpkgd, "filename-size", (uint64_t)st.st_size)) {
xbps_object_release(binpkgd);
free(pkgver);
free(pkgname);
rv = EINVAL;
goto out;
}
@@ -371,13 +367,11 @@ index_add(struct xbps_handle *xhp, int args, int argmax, char **argv, bool force
*/
if (!xbps_dictionary_set(idxstage, pkgname, binpkgd)) {
xbps_object_release(binpkgd);
free(pkgname);
free(pkgver);
rv = EINVAL;
goto out;
}
xbps_object_release(binpkgd);
free(pkgname);
free(pkgver);
}
/*

View File

@@ -54,7 +54,7 @@ idx_cleaner_cb(struct xbps_handle *xhp,
{
struct CleanerCbInfo *info = arg;
const char *arch = NULL, *pkgver = NULL, *sha256 = NULL;
char *filen, *pkgname;
char *filen, pkgname[XBPS_NAME_SIZE];
xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
@@ -67,11 +67,9 @@ idx_cleaner_cb(struct xbps_handle *xhp,
* File cannot be read, might be permissions,
* broken or simply unexistent; either way, remove it.
*/
pkgname = xbps_pkg_name(pkgver);
if (pkgname == NULL)
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
goto out;
xbps_dictionary_remove(dest, pkgname);
free(pkgname);
printf("index: removed pkg %s\n", pkgver);
} else if (info->hashcheck) {
/*
@@ -80,11 +78,9 @@ idx_cleaner_cb(struct xbps_handle *xhp,
xbps_dictionary_get_cstring_nocopy(obj,
"filename-sha256", &sha256);
if (xbps_file_hash_check(filen, sha256) != 0) {
pkgname = xbps_pkg_name(pkgver);
if (pkgname == NULL)
if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver))
goto out;
xbps_dictionary_remove(dest, pkgname);
free(pkgname);
printf("index: removed pkg %s\n", pkgver);
}
}

View File

@@ -102,7 +102,7 @@ main(int argc, char **argv)
struct xbps_handle xh;
struct xferstat xfer;
const char *version, *rootdir = NULL, *confdir = NULL;
char *pkgname, *filename;
char pkgname[XBPS_NAME_SIZE], *filename;
int flags = 0, c, rv = 0;
const struct option longopts[] = {
{ NULL, 0, NULL, 0 }
@@ -197,14 +197,12 @@ main(int argc, char **argv)
if (argc != 2)
usage();
pkgname = xbps_pkg_name(argv[1]);
if (pkgname == NULL) {
if (!xbps_pkg_name(pkgname, sizeof(pkgname), argv[1])) {
fprintf(stderr,
"Invalid string, expected <string>-<version>_<revision>\n");
exit(EXIT_FAILURE);
}
printf("%s\n", pkgname);
free(pkgname);
} else if (strcmp(argv[0], "getpkgrevision") == 0) {
/* Returns the revision of a pkg string */
if (argc != 2)
@@ -220,12 +218,10 @@ main(int argc, char **argv)
if (argc != 2)
usage();
pkgname = xbps_pkgpattern_name(argv[1]);
if (pkgname == NULL)
if (!xbps_pkgpattern_name(pkgname, sizeof(pkgname), argv[1]))
exit(EXIT_FAILURE);
printf("%s\n", pkgname);
free(pkgname);
} else if (strcmp(argv[0], "getpkgdepversion") == 0) {
/* returns the version of a package pattern dependency */
if (argc != 2)