From: Michael Tremer Date: Sat, 10 Apr 2021 14:29:17 +0000 (+0000) Subject: Make pakfire_path_join write to stack X-Git-Tag: 0.9.28~1285^2~393 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=384d009ac877cfefb98dea46c0a6d91639bbda1a;p=pakfire.git Make pakfire_path_join write to stack Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index 47b954fc2..f7ddbaca0 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -752,6 +752,8 @@ out: } PAKFIRE_EXPORT char* pakfire_archive_extraction_path(PakfireArchive archive, const char* target) { + char prefix[PATH_MAX]; + PakfireRepo repo = pakfire_repo_create(archive->pakfire, "dummy"); // Read package metadata @@ -771,14 +773,17 @@ PAKFIRE_EXPORT char* pakfire_archive_extraction_path(PakfireArchive archive, con char* nevra = pakfire_package_get_nevra(pkg); // Append package name and version to path - char* prefix = pakfire_path_join(target, nevra); + int r = pakfire_path_join(prefix, sizeof(prefix), target, nevra); // Cleanup pakfire_package_unref(pkg); pakfire_repo_unref(repo); free(nevra); - return prefix; + if (r < 0) + return NULL; + + return strdup(prefix); } struct pakfire_archive_extractor { @@ -792,7 +797,7 @@ static int pakfire_archive_extract_entry(PakfireArchive archive, struct pakfire_archive_extractor* extractor = (struct pakfire_archive_extractor*)data; PakfireFile file = NULL; - char* buffer; + char buffer[PATH_MAX]; int r = 1; // Create a new file object if there is a filelist @@ -819,22 +824,20 @@ static int pakfire_archive_extract_entry(PakfireArchive archive, // Prepend the prefix if (extractor->prefix) { - buffer = pakfire_path_join(extractor->prefix, path); - if (!buffer) + r = pakfire_path_join(buffer, sizeof(buffer) - 1, extractor->prefix, path); + if (r < 0) goto ERROR; archive_entry_set_pathname(entry, buffer); - free(buffer); // Update hardlink destination const char* link = archive_entry_hardlink(entry); if (link) { - buffer = pakfire_path_join(extractor->prefix, link); - if (!buffer) + r = pakfire_path_join(buffer, sizeof(buffer) - 1, extractor->prefix, link); + if (r < 0) goto ERROR; archive_entry_set_hardlink(entry, buffer); - free(buffer); } } diff --git a/src/libpakfire/execute.c b/src/libpakfire/execute.c index 75609143b..e5cba0908 100644 --- a/src/libpakfire/execute.c +++ b/src/libpakfire/execute.c @@ -485,10 +485,14 @@ PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* script, c char* envp[], int flags, pakfire_execute_logging_callback logging_callback, void* data) { const char* root = pakfire_get_path(pakfire); - // Write the scriptlet to disk - char* path = pakfire_path_join(root, "tmp/.pakfire-script.XXXXXX"); + char path[PATH_MAX]; int r; + // Write the scriptlet to disk + r = pakfire_path_join(path, sizeof(path) - 1, root, "tmp/.pakfire-script.XXXXXX"); + if (r < 0) + goto out; + // Open a temporary file int fd = mkstemp(path); if (fd < 0) { @@ -542,10 +546,8 @@ PAKFIRE_EXPORT int pakfire_execute_script(Pakfire pakfire, const char* script, c out: // Remove script from disk - unlink(path); - - // Cleanup - free(path); + if (*path) + unlink(path); return r; } diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index e30760a58..7de9630d8 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -35,7 +35,6 @@ char* pakfire_string_replace(const char* s, const char* pattern, const char* rep char* pakfire_format_size(double size); char* pakfire_format_date(time_t t); -char* pakfire_path_join(const char* first, const char* second); const char* pakfire_path_relpath(const char* root, const char* path); int pakfire_path_isdir(const char* path); @@ -67,6 +66,9 @@ time_t pakfire_path_age(const char* path); char* pakfire_hexlify(const char* digest, const size_t length); +int pakfire_path_join(char* dest, size_t length, + const char* first, const char* second); + int pakfire_mkdir(const char* path, mode_t mode); FILE* pakfire_mktemp(char* path); char* pakfire_mkdtemp(char* path); diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index d04de89ee..945908423 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -63,9 +64,13 @@ gpgme_ctx_t pakfire_get_gpgctx(Pakfire pakfire) { // Set output to be ASCII armoured gpgme_set_armor(ctx, 1); + char home[PATH_MAX]; + // Use GPG const char* path = pakfire_get_path(pakfire); - char* home = pakfire_path_join(path, "etc/pakfire/gnupg"); + int r = pakfire_path_join(home, sizeof(home) - 1, path, "etc/pakfire/gnupg"); + if (r < 0) + goto FAIL; // Check if gpg directories exist if (pakfire_access(pakfire, home, NULL, R_OK) != 0) { @@ -80,7 +85,6 @@ gpgme_ctx_t pakfire_get_gpgctx(Pakfire pakfire) { // Setup engine error = gpgme_ctx_set_engine_info(ctx, GPGME_PROTOCOL_OpenPGP, NULL, home); - free(home); if (gpg_err_code(error) != GPG_ERR_NO_ERROR) goto FAIL; diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 6f53cf813..905928282 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -411,7 +411,6 @@ global: pakfire_dirname; pakfire_generate_uuid; pakfire_path_isdir; - pakfire_path_join; pakfire_path_relpath; pakfire_read_file_into_buffer; pakfire_split_string; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index ccd7747ae..ba69a0661 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -144,6 +144,7 @@ static int __mount(Pakfire pakfire, const char* source, const char* target, } static int pakfire_mount(Pakfire pakfire) { + char target[PATH_MAX]; int r; for (const struct pakfire_mountpoint* mp = mountpoints; mp->source; mp++) { @@ -153,7 +154,9 @@ static int pakfire_mount(Pakfire pakfire) { DEBUG(pakfire, "Mounting /%s\n", mp->target); - char* target = pakfire_path_join(pakfire->path, mp->target); + r = pakfire_path_join(target, sizeof(target) - 1, pakfire->path, mp->target); + if (r < 0) + return r; RETRY: // Perform mount() @@ -164,7 +167,6 @@ RETRY: r = pakfire_mkdir(target, S_IRUSR|S_IWUSR|S_IXUSR); if (r) { ERROR(pakfire, "Could not create %s\n", target); - free(target); return r; } @@ -172,11 +174,8 @@ RETRY: } ERROR(pakfire, "Could not mount /%s: %s\n", mp->target, strerror(errno)); - free(target); return r; } - - free(target); } return 0; @@ -596,11 +595,17 @@ PAKFIRE_EXPORT const char* pakfire_get_path(Pakfire pakfire) { } PAKFIRE_EXPORT char* pakfire_make_path(Pakfire pakfire, const char* path) { + char buffer[PATH_MAX]; + // Make sure that path never starts with / while (path && *path == '/') path++; - return pakfire_path_join(pakfire->path, path); + int r = pakfire_path_join(buffer, sizeof(buffer) - 1, pakfire->path, path); + if (r < 0) + return NULL; + + return strdup(buffer); } PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags) { diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 3b437ac02..8bfe96243 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -227,21 +228,15 @@ char* pakfire_format_date(time_t t) { return pakfire_strftime("%Y-%m-%d", t); } -PAKFIRE_EXPORT char* pakfire_path_join(const char* first, const char* second) { - char* buffer; - - if (!first) - return strdup(second); - +PAKFIRE_EXPORT int pakfire_path_join(char* dest, size_t length, + const char* first, const char* second) { if (!second) - return strdup(first); - - if (*second == '/') - return strdup(second); + return snprintf(dest, length, "%s", first); - asprintf(&buffer, "%s/%s", first, second); + if (!first || *second == '/') + return snprintf(dest, length, "%s", second); - return buffer; + return snprintf(dest, length, "%s/%s", first, second); } PAKFIRE_EXPORT const char* pakfire_path_relpath(const char* root, const char* path) { @@ -309,9 +304,13 @@ PAKFIRE_EXPORT char* pakfire_dirname(const char* path) { } PAKFIRE_EXPORT int pakfire_access(Pakfire pakfire, const char* dir, const char* file, int mode) { - char* path = pakfire_path_join(dir, file); + char path[PATH_MAX]; - int r = access(path, mode); + int r = pakfire_path_join(path, sizeof(path) - 1, dir, file); + if (r < 0) + return r; + + r = access(path, mode); if (r) { if (mode & R_OK) @@ -327,8 +326,6 @@ PAKFIRE_EXPORT int pakfire_access(Pakfire pakfire, const char* dir, const char* DEBUG(pakfire, "%s does not exist\n", path); } - free(path); - return r; } diff --git a/tests/libpakfire/archive.c b/tests/libpakfire/archive.c index a17ba0aff..6e84c0b5d 100644 --- a/tests/libpakfire/archive.c +++ b/tests/libpakfire/archive.c @@ -28,11 +28,11 @@ #include "../testsuite.h" -static const char* TEST_PKG1_PATH = "data/beep-1.3-2.ip3.x86_64.pfm"; -static const char* TEST_PKG1_FILE = "usr/bin/beep"; +#define TEST_PKG1_PATH "data/beep-1.3-2.ip3.x86_64.pfm" +#define TEST_PKG1_FILE "usr/bin/beep" static int test_open(const struct test* t) { - char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH); + const char* path = TEST_SRC_PATH TEST_PKG1_PATH; LOG("Trying to open %s\n", path); // Open the archive @@ -44,21 +44,17 @@ static int test_open(const struct test* t) { ASSERT(verify == PAKFIRE_ARCHIVE_VERIFY_OK); pakfire_archive_unref(archive); - free(path); return EXIT_SUCCESS; } static int test_filelist(const struct test* t) { - char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH); + const char* path = TEST_SRC_PATH TEST_PKG1_PATH; // Open the archive PakfireArchive archive; ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); - // Free path - free(path); - // Fetch the filelist PakfireFilelist list = pakfire_archive_get_filelist(archive); ASSERT(list); @@ -72,11 +68,10 @@ static int test_filelist(const struct test* t) { } static int test_extract(const struct test* t) { - char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH); + const char* path = TEST_SRC_PATH TEST_PKG1_PATH; PakfireArchive archive; ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); - free(path); // Extract the archive payload int r = pakfire_archive_extract(archive, NULL, PAKFIRE_ARCHIVE_USE_PAYLOAD); @@ -92,11 +87,10 @@ static int test_extract(const struct test* t) { } static int test_import(const struct test* t) { - char* path = pakfire_path_join(TEST_SRC_PATH, TEST_PKG1_PATH); + const char* path = TEST_SRC_PATH TEST_PKG1_PATH; PakfireArchive archive; ASSERT_SUCCESS(pakfire_archive_open(&archive, t->pakfire, path)); - free(path); PakfireRepo repo = pakfire_repo_create(t->pakfire, "tmp"); ASSERT(repo); diff --git a/tests/libpakfire/compress.c b/tests/libpakfire/compress.c index a318bd583..a6c153f12 100644 --- a/tests/libpakfire/compress.c +++ b/tests/libpakfire/compress.c @@ -18,7 +18,9 @@ # # #############################################################################*/ +#include #include +#include #include #include @@ -29,10 +31,11 @@ const char TEST_DATA[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"; static int read_test(const struct test* t, FILE* (function)(FILE* f, const char* mode), const char* file) { + char path[PATH_MAX]; char buffer[1024]; - char* path = pakfire_path_join(TEST_SRC_PATH, file); - ASSERT(path); + int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, file); + ASSERT(r >= 0); FILE* f = fopen(path, "r"); ASSERT(f); @@ -50,7 +53,6 @@ static int read_test(const struct test* t, ASSERT(memcmp(buffer, TEST_DATA, sizeof(TEST_DATA) - 1) == 0); fclose(f); - free(path); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/db.c b/tests/libpakfire/db.c index 96f81cbf4..2bd1756bf 100644 --- a/tests/libpakfire/db.c +++ b/tests/libpakfire/db.c @@ -75,7 +75,7 @@ static int test_add_package(const struct test* t) { ssize_t packages = pakfire_db_packages(db); ASSERT(packages == 0); - char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep-1.3-2.ip3.x86_64.pfm"); + const char* path = TEST_SRC_PATH "/data/beep-1.3-2.ip3.x86_64.pfm"; // Open archive PakfireArchive archive; @@ -107,7 +107,6 @@ static int test_add_package(const struct test* t) { pakfire_db_unref(db); pakfire_package_unref(pkg); pakfire_repo_unref(repo); - free(path); return 0; } diff --git a/tests/libpakfire/makefile.c b/tests/libpakfire/makefile.c index 95da58ef8..18d458270 100644 --- a/tests/libpakfire/makefile.c +++ b/tests/libpakfire/makefile.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include @@ -36,8 +37,7 @@ static const char* makefiles[] = { }; static int load_macros(PakfireParser parser) { - char* macros = pakfire_path_join(TEST_SRC_PATH, "../macros/*.macro"); - ASSERT(macros); + const char* macros = TEST_SRC_PATH "../macros/*.macro"; glob_t buffer; int r = glob(macros, 0, NULL, &buffer); @@ -48,16 +48,16 @@ static int load_macros(PakfireParser parser) { ASSERT(r == 0); } - free(macros); - return 0; } static int test_parse(const struct test* t) { const char** makefile = makefiles; + char path[PATH_MAX]; while (*makefile) { - char* path = pakfire_path_join(TEST_SRC_PATH, *makefile); + int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *makefile); + ASSERT(r >= 0); // Open file FILE* f = fopen(path, "r"); @@ -65,11 +65,10 @@ static int test_parse(const struct test* t) { PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); - int r = pakfire_parser_read(parser, f, NULL); + r = pakfire_parser_read(parser, f, NULL); ASSERT(r == 0); pakfire_parser_unref(parser); - free(path); fclose(f); // Next file @@ -109,8 +108,7 @@ static int test_packages(const struct test* t) { return r; // Read beep.nm - char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep.nm"); - ASSERT(path); + const char* path = TEST_SRC_PATH "data/beep.nm"; r = pakfire_parser_read_file(parser, path, NULL); ASSERT(r == 0); @@ -134,7 +132,6 @@ static int test_packages(const struct test* t) { pakfire_parser_unref(parser); pakfire_package_unref(pkg); pakfire_repo_unref(repo); - free(path); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/packager.c b/tests/libpakfire/packager.c index 615511102..8a97efc6a 100644 --- a/tests/libpakfire/packager.c +++ b/tests/libpakfire/packager.c @@ -40,8 +40,7 @@ static int test_create(const struct test* t) { ASSERT(r == 0); // Add a file to the package - char* path = pakfire_path_join(TEST_SRC_PATH, "data/beep-1.3-2.ip3.x86_64.pfm"); - ASSERT(path); + const char* path = TEST_SRC_PATH "data/beep-1.3-2.ip3.x86_64.pfm"; r = pakfire_packager_add(packager, path, NULL); ASSERT(r == 0); @@ -55,7 +54,6 @@ static int test_create(const struct test* t) { pakfire_packager_unref(packager); pakfire_package_unref(pkg); pakfire_repo_unref(repo); - free(path); return EXIT_SUCCESS; } diff --git a/tests/libpakfire/parser.c b/tests/libpakfire/parser.c index 0a93a91b3..25929810a 100644 --- a/tests/libpakfire/parser.c +++ b/tests/libpakfire/parser.c @@ -18,6 +18,7 @@ # # #############################################################################*/ +#include #include #include @@ -112,10 +113,11 @@ static const char* files[] = { static int test_parser_files(const struct test* t) { const char** file = files; + char path[PATH_MAX]; while (*file) { - char* path = pakfire_path_join(TEST_SRC_PATH, *file); - ASSERT(path); + int r = snprintf(path, sizeof(path) - 1, "%s/%s", TEST_SRC_PATH, *file); + ASSERT(r >= 0); // Create a new parser PakfireParser parser = pakfire_parser_create(t->pakfire, NULL, NULL, 0); @@ -123,7 +125,7 @@ static int test_parser_files(const struct test* t) { FILE* f = fopen(path, "r"); ASSERT(f); - int r = pakfire_parser_read(parser, f, NULL); + r = pakfire_parser_read(parser, f, NULL); if (r) { fprintf(stderr, "Could not parse %s\n", path); return EXIT_FAILURE; @@ -131,7 +133,6 @@ static int test_parser_files(const struct test* t) { fclose(f); pakfire_parser_unref(parser); - free(path); // Next file file++; diff --git a/tests/testsuite.c b/tests/testsuite.c index 4830e2c58..a615b3ed7 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -27,8 +27,6 @@ #include #include -const char* TEST_SRC_PATH = ABS_TOP_SRCDIR "/tests"; - struct testsuite ts; static int test_run(int i, struct test* t) { diff --git a/tests/testsuite.h b/tests/testsuite.h index c6d01c5fb..0549d49f2 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -29,7 +29,7 @@ #define MAX_TESTS 128 -extern const char* TEST_SRC_PATH; +#define TEST_SRC_PATH ABS_TOP_SRCDIR "/tests/" struct test { const char* name;