From ac1765b0adbf461dcec2dac3c730bb6509904516 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 25 Aug 2023 17:45:50 +0000 Subject: [PATCH] file: Fix tests and handle invalid inputs better Signed-off-by: Michael Tremer --- src/libpakfire/file.c | 10 ++++++---- tests/libpakfire/file.c | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 2c985a7cf..cb3c5e99f 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -797,10 +797,8 @@ const char* pakfire_file_get_abspath(struct pakfire_file* file) { int pakfire_file_set_abspath(struct pakfire_file* file, const char* path) { // Check if path is set and absolute - if (!path || *path != '/') { - errno = EINVAL; - return 1; - } + if (!path || *path != '/') + return -EINVAL; // Store the abspath archive_entry_copy_sourcepath(file->entry, path); @@ -816,6 +814,10 @@ PAKFIRE_EXPORT int pakfire_file_set_path(struct pakfire_file* file, const char* char buffer[PATH_MAX]; int r = 0; + // Check input + if (!path) + return -EINVAL; + // Strip any leading dots from paths if (pakfire_string_startswith(path, "./")) path++; diff --git a/tests/libpakfire/file.c b/tests/libpakfire/file.c index d32f5dd4c..c3f7ba1bf 100644 --- a/tests/libpakfire/file.c +++ b/tests/libpakfire/file.c @@ -49,10 +49,10 @@ static int test_create_invalid(const struct test* t) { ASSERT_SUCCESS(pakfire_file_create(&file, t->pakfire)); // Set path - ASSERT_ERRNO(pakfire_file_set_path(file, NULL), EINVAL); + ASSERT(pakfire_file_set_path(file, NULL) == -EINVAL); // It should not be possible to set relative absolute paths - ASSERT_ERRNO(pakfire_file_set_abspath(file, "abc/abc"), EINVAL); + ASSERT(pakfire_file_set_abspath(file, "abc/abc") == -EINVAL); // Destroy it ASSERT_NULL(pakfire_file_unref(file)); -- 2.39.5