]> git.ipfire.org Git - pakfire.git/commitdiff
Make pakfire_path_join write to stack
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 10 Apr 2021 14:29:17 +0000 (14:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 10 Apr 2021 14:29:17 +0000 (14:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
15 files changed:
src/libpakfire/archive.c
src/libpakfire/execute.c
src/libpakfire/include/pakfire/util.h
src/libpakfire/key.c
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c
src/libpakfire/util.c
tests/libpakfire/archive.c
tests/libpakfire/compress.c
tests/libpakfire/db.c
tests/libpakfire/makefile.c
tests/libpakfire/packager.c
tests/libpakfire/parser.c
tests/testsuite.c
tests/testsuite.h

index 47b954fc2ecfac5f4fd325fd1359632ed785a7f4..f7ddbaca045c9c41dd763b8c26a097f3931f550e 100644 (file)
@@ -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);
                }
        }
 
index 75609143bfe3688d1d592bedd7acdf71e95f3023..e5cba090816f67d03dcb5839e753b532b6471c52 100644 (file)
@@ -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;
 }
index e30760a583eb0f021636c92ef759b52edb550456..7de9630d8d2f5da6f450deb3f8304903e260a608 100644 (file)
@@ -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);
index d04de89ee03e532cf771ccf91c02ba8720324502..94590842322543f9ed391305b7f651f3eafcd901 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <errno.h>
 #include <gpgme.h>
+#include <linux/limits.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
@@ -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;
 
index 6f53cf8138b3f1aaab6b2d3af8e8f21b180e5b2d..905928282292edd2059a32aa60c81f8f98685d1d 100644 (file)
@@ -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;
index ccd7747ae2491e3bc2325a34b6e01ff27f9f9eda..ba69a06610f2dfacdfef83e7b55ba3f075e15c2a 100644 (file)
@@ -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) {
index 3b437ac02927902f3bf5c42d48eb87512731b05e..8bfe96243147838c0fd6f55e3367c92a4addf5cb 100644 (file)
@@ -23,6 +23,7 @@
 #include <fcntl.h>
 #include <ftw.h>
 #include <libgen.h>
+#include <linux/limits.h>
 #include <math.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -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;
 }
 
index a17ba0aff66f648593290947f44e8723e5164f97..6e84c0b5da2f0d253e512283c426bc91889a75f7 100644 (file)
 
 #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);
index a318bd5832a4b958e086b7d518e340a64b6271bd..a6c153f12550d765fdf60f18a4c6bea22394dbc8 100644 (file)
@@ -18,7 +18,9 @@
 #                                                                             #
 #############################################################################*/
 
+#include <linux/limits.h>
 #include <stdio.h>
+#include <string.h>
 
 #include <pakfire/compress.h>
 #include <pakfire/util.h>
@@ -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;
 }
index 96f81cbf4d64797420e23882da2cd69269ff392a..2bd1756bfb48b9dc74c670db4cb97a0d590045f0 100644 (file)
@@ -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;
 }
index 95da58ef898410a1aee7214a8cb99282903e779c..18d4582705a235c221637137714af41a1943ec60 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <glob.h>
+#include <linux/limits.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -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;
 }
index 615511102ba1c771fcdc51fe885a394d7b548630..8a97efc6a957761692195bf44961a8736ef1ea2a 100644 (file)
@@ -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;
 }
index 0a93a91b323230d66acf957cbf930bef965ae153..25929810ac29e730dfe0a46ca3410a82d1cc532e 100644 (file)
@@ -18,6 +18,7 @@
 #                                                                             #
 #############################################################################*/
 
+#include <linux/limits.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -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++;
index 4830e2c5883c663d7b1982dd659381c249ce1844..a615b3ed7943bfd332f3bd8e1dba8a82911d41f9 100644 (file)
@@ -27,8 +27,6 @@
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
 
-const char* TEST_SRC_PATH = ABS_TOP_SRCDIR "/tests";
-
 struct testsuite ts;
 
 static int test_run(int i, struct test* t) {
index c6d01c5fba38a85d517a7188c6df2e4bd8faf3b9..0549d49f2c55a558e8c821b5bce3bfbe0f78b82c 100644 (file)
@@ -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;