diff --git a/lib/package_config_files.c b/lib/package_config_files.c
index e5a69e81..14727fc4 100644
--- a/lib/package_config_files.c
+++ b/lib/package_config_files.c
@@ -50,7 +50,8 @@ xbps_entry_is_a_conf_file(xbps_dictionary_t filesd,
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
d = xbps_array_get(array, i);
- xbps_dictionary_get_cstring_nocopy(d, "file", &cffile);
+ if (!xbps_dictionary_get_cstring_nocopy(d, "file", &cffile))
+ continue;
if (strcmp(cffile, entry_pname) == 0)
return true;
}
@@ -115,8 +116,9 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
iter2 = xbps_array_iter_from_dict(pkg_filesd, "conf_files");
if (iter2 != NULL) {
while ((obj2 = xbps_object_iterator_next(iter2))) {
- xbps_dictionary_get_cstring_nocopy(obj2,
- "file", &cffile);
+ if (!xbps_dictionary_get_cstring_nocopy(obj2,
+ "file", &cffile))
+ continue;
snprintf(buf, sizeof(buf), ".%s", cffile);
if (strcmp(entry_pname, buf) == 0) {
xbps_dictionary_get_cstring_nocopy(obj2, "sha256", &sha256_orig);
@@ -139,7 +141,8 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
* Compare original, installed and new hash for current file.
*/
while ((obj = xbps_object_iterator_next(iter))) {
- xbps_dictionary_get_cstring_nocopy(obj, "file", &cffile);
+ if (!xbps_dictionary_get_cstring_nocopy(obj, "file", &cffile))
+ continue;
snprintf(buf, sizeof(buf), ".%s", cffile);
if (strcmp(entry_pname, buf)) {
continue;
@@ -158,7 +161,8 @@ xbps_entry_install_conf_file(struct xbps_handle *xhp,
break;
}
}
- xbps_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new);
+ if (!xbps_dictionary_get_cstring_nocopy(obj, "sha256", &sha256_new))
+ continue;
/*
* Orig = X, Curr = X, New = X
*
diff --git a/lib/transaction_files.c b/lib/transaction_files.c
index cc4f0993..52bc6519 100644
--- a/lib/transaction_files.c
+++ b/lib/transaction_files.c
@@ -585,7 +585,10 @@ collect_files(struct xbps_handle *xhp, xbps_dictionary_t d,
if ((a = xbps_dictionary_get(d, "files"))) {
for (i = 0; i < xbps_array_count(a); i++) {
filed = xbps_array_get(a, i);
- xbps_dictionary_get_cstring_nocopy(filed, "file", &file);
+ if (!xbps_dictionary_get_cstring_nocopy(filed, "file", &file)) {
+ rv = EINVAL;
+ goto out;
+ }
if (removefile)
xbps_dictionary_get_cstring_nocopy(filed, "sha256", &sha256);
size = 0;
@@ -603,7 +606,10 @@ collect_files(struct xbps_handle *xhp, xbps_dictionary_t d,
if ((a = xbps_dictionary_get(d, "conf_files"))) {
for (i = 0; i < xbps_array_count(a); i++) {
filed = xbps_array_get(a, i);
- xbps_dictionary_get_cstring_nocopy(filed, "file", &file);
+ if (!xbps_dictionary_get_cstring_nocopy(filed, "file", &file)) {
+ rv = EINVAL;
+ goto out;
+ }
size = 0;
xbps_dictionary_get_uint64(filed, "size", &size);
if (removefile)
@@ -627,9 +633,11 @@ collect_files(struct xbps_handle *xhp, xbps_dictionary_t d,
for (i = 0; i < xbps_array_count(a); i++) {
const char *target = NULL;
filed = xbps_array_get(a, i);
- xbps_dictionary_get_cstring_nocopy(filed, "file", &file);
- xbps_dictionary_get_cstring_nocopy(filed, "target", &target);
- assert(target);
+ if (!xbps_dictionary_get_cstring_nocopy(filed, "file", &file) ||
+ !xbps_dictionary_get_cstring_nocopy(filed, "target", &target)) {
+ rv = EINVAL;
+ goto out;
+ }
rv = collect_file(xhp, file, 0, pkgname, pkgver, idx, NULL,
TYPE_LINK, update, removepkg, preserve, removefile, target);
if (rv == EEXIST) {
@@ -643,7 +651,10 @@ collect_files(struct xbps_handle *xhp, xbps_dictionary_t d,
if ((a = xbps_dictionary_get(d, "dirs"))) {
for (i = 0; i < xbps_array_count(a); i++) {
filed = xbps_array_get(a, i);
- xbps_dictionary_get_cstring_nocopy(filed, "file", &file);
+ if (!xbps_dictionary_get_cstring_nocopy(filed, "file", &file)) {
+ rv = EINVAL;
+ goto out;
+ }
rv = collect_file(xhp, file, 0, pkgname, pkgver, idx, NULL,
TYPE_DIR, update, removepkg, preserve, removefile, NULL);
if (rv == EEXIST) {
diff --git a/lib/util_hash.c b/lib/util_hash.c
index 3ed38c52..648fe379 100644
--- a/lib/util_hash.c
+++ b/lib/util_hash.c
@@ -231,8 +231,9 @@ file_hash_dictionary(xbps_dictionary_t d, const char *key, const char *file)
return NULL;
}
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
- xbps_dictionary_get_cstring_nocopy(obj,
- "file", &curfile);
+ if (!xbps_dictionary_get_cstring_nocopy(obj,
+ "file", &curfile))
+ continue;
if (strcmp(file, curfile) == 0) {
/* file matched */
xbps_dictionary_get_cstring_nocopy(obj,
diff --git a/tests/xbps/libxbps/shell/install_test.sh b/tests/xbps/libxbps/shell/install_test.sh
index 930bcba5..11ecc8c9 100644
--- a/tests/xbps/libxbps/shell/install_test.sh
+++ b/tests/xbps/libxbps/shell/install_test.sh
@@ -23,6 +23,29 @@ install_empty_body() {
atf_check_equal $rv 0
}
+atf_test_case install_malformed_files_plist
+
+install_malformed_files_plist_head() {
+ atf_set "descr" "Tests for pkg installations: files.plist entry without a file key"
+}
+
+install_malformed_files_plist_body() {
+ mkdir -p repo pkg_A/usr/bin
+ echo A > pkg_A/usr/bin/foo
+ cd repo
+ xbps-create --compression none -A noarch -n A-1.0_1 -s "A pkg" ../pkg_A
+ atf_check_equal $? 0
+ # rename the file keys in files.plist; the replacement has the same
+ # length, so the uncompressed archive stays valid.
+ sed -e 's,file,nope,' -i A-1.0_1.noarch.xbps
+ atf_check_equal $? 0
+ xbps-rindex -d -a $PWD/*.xbps
+ atf_check_equal $? 0
+ cd ..
+ xbps-install -C empty.conf -r root --repository=$PWD/repo -yd A
+ atf_check_equal $? 22
+}
+
atf_test_case install_with_deps
install_with_deps_head() {
@@ -774,6 +797,7 @@ update_issue_218_body() {
atf_init_test_cases() {
atf_add_test_case install_empty
+ atf_add_test_case install_malformed_files_plist
atf_add_test_case install_with_deps
atf_add_test_case install_with_vpkg_deps
atf_add_test_case install_if_not_installed_on_update