]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Have pakfire_make_cache_path write to stack
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Mar 2021 18:59:29 +0000 (18:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 13 Mar 2021 18:59:29 +0000 (18:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/dist.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/package.c
src/libpakfire/pakfire.c
src/libpakfire/repo.c

index 1f41640ce135d1554589441c2f7c748909a78488..9e5e9f6dd86222609758d9eb60a9186894e0071e 100644 (file)
@@ -152,8 +152,8 @@ static int pakfire_dist_add_source(Pakfire pakfire, struct pakfire_packager* pac
        const char* name = pakfire_package_get_name(pkg);
 
        snprintf(archive_path, sizeof(archive_path) - 1, "files/%s", filename);
-       snprintf(cache_path, sizeof(cache_path) - 1, "%s/sources/%s/%s",
-               pakfire_make_cache_path(pakfire, ""), name, filename);
+       pakfire_make_cache_path(pakfire, cache_path, sizeof(cache_path) - 1,
+               "sources/%s/%s", name, filename);
 
        // Download the file if it does not exist in the cache
        if (access(cache_path, R_OK) != 0) {
index afff55f79a39afa2f25f2f150a51ffde40c47e48..00a8edeaf12772e27b7a8c59d4f7c03ea0e34c7f 100644 (file)
@@ -78,7 +78,9 @@ int pakfire_read_makefile(PakfireParser* parser, Pakfire pakfire, const char* pa
 
 #include <solv/pool.h>
 
-char* pakfire_make_cache_path(Pakfire pakfire, const char* format, ...);
+int pakfire_make_cache_path(Pakfire pakfire, char* path, size_t length,
+       const char* format, ...)
+       __attribute__((format(printf, 4, 5)));
 
 void pakfire_pool_has_changed(Pakfire pakfire);
 void pakfire_pool_apply_changes(Pakfire pakfire);
index c6300446b84876db2800883d9ab78b6a4825e14a..4de9ff6cc6a445f47f3e22ffcb446362d21fb6d9 100644 (file)
@@ -932,10 +932,10 @@ PAKFIRE_EXPORT char* pakfire_package_get_cache_path(PakfirePackage pkg) {
        if (strlen(checksum) < 3)
                return NULL;
 
-       snprintf(path, sizeof(path) - 1, "%c%c/%s/%s", checksum[0], checksum[1],
-               checksum + 2, filename);
+       pakfire_make_cache_path(pkg->pakfire, path, sizeof(path) - 1,
+               "%c%c/%s/%s", checksum[0], checksum[1], checksum + 2, filename);
 
-       return pakfire_make_cache_path(pkg->pakfire, path);
+       return strdup(path);
 }
 
 PAKFIRE_EXPORT PakfireArchive pakfire_package_get_archive(PakfirePackage pkg) {
index 87260978c21ba1d6f4fe554555bfdf24b555bc49..bcaf069669ed883b7d8862912cb353fca899c9e5 100644 (file)
@@ -651,16 +651,27 @@ PAKFIRE_EXPORT PakfirePackageList pakfire_search(Pakfire pakfire, const char* wh
 
 // Cache
 
-PAKFIRE_EXPORT char* pakfire_make_cache_path(Pakfire pakfire, const char* format, ...) {
-       char path[PATH_MAX];
+int pakfire_make_cache_path(Pakfire pakfire, char* path, size_t length,
+               const char* format, ...) {
        va_list args;
 
+       // Write cache path
+       ssize_t l = snprintf(path, length - 1, "%s/", pakfire->cache_path);
+       if (l < 0)
+               return l;
+
+       // We have run out of space
+       if ((size_t)l >= length)
+               return -1;
+
+       // Append everything after the format string
+       path += l;
+
        va_start(args, format);
-       vsnprintf(path, sizeof(path) - 1, format, args);
+       vsnprintf(path, length - l - 1, format, args);
        va_end(args);
 
-       // Prepend cache path
-       return pakfire_path_join(pakfire->cache_path, path);
+       return 0;
 }
 
 static int _unlink(const char* path, const struct stat* stat, int typeflag, struct FTW* ftwbuf) {
@@ -668,11 +679,12 @@ static int _unlink(const char* path, const struct stat* stat, int typeflag, stru
 }
 
 PAKFIRE_EXPORT int pakfire_cache_destroy(Pakfire pakfire, const char* path) {
-       char* cache_path = pakfire_make_cache_path(pakfire, path);
+       char cache_path[PATH_MAX];
+
+       pakfire_make_cache_path(pakfire, cache_path, sizeof(cache_path) - 1, "%s", path);
 
        // Completely delete the tree of files
        int r = nftw(cache_path, _unlink, 64, FTW_DEPTH|FTW_PHYS);
-       free(cache_path);
 
        // It is okay if the path doesn't exist
        if (r < 0 && errno == ENOENT)
index 4120d6ffaa693afbaa200439ece6954fecff512b..84772fff31a6021a1faa84567ee627495060122c 100644 (file)
@@ -63,11 +63,11 @@ struct pakfire_repo_appdata {
        char* baseurl;
        char* keyfile;
 
-       char* metadata;
+       char metadata[PATH_MAX];
 
        // Mirrorlist
        char* mirrorlist_url;
-       char* mirrorlist;
+       char mirrorlist[PATH_MAX];
 };
 
 struct _PakfireRepo {
@@ -143,6 +143,8 @@ static int pakfire_repo_read_metadata(PakfireRepo repo, const char* path, int re
        }
 
        char database_filename[NAME_MAX] = "";
+       char database_cache_path[PATH_MAX];
+
        struct json_object* database = NULL;
 
        // Search for the database name
@@ -154,11 +156,10 @@ static int pakfire_repo_read_metadata(PakfireRepo repo, const char* path, int re
                DEBUG(repo->pakfire, "Using package database %s\n", database_filename);
        }
 
-       char* database_cache_path = NULL;
 
        // Try loading the database
        if (*database_filename) {
-               database_cache_path = pakfire_make_cache_path(repo->pakfire,
+               pakfire_make_cache_path(repo->pakfire, database_cache_path, sizeof(database_cache_path) -1,
                        "repodata/%s/%s", pakfire_repo_get_name(repo), database_filename);
 
                // Download the database if necessary
@@ -178,9 +179,6 @@ static int pakfire_repo_read_metadata(PakfireRepo repo, const char* path, int re
        r = 0;
 
 ERROR:
-       if (database_cache_path)
-               free(database_cache_path);
-
        // Free the parsed JSON object
        json_object_put(json);
 
@@ -275,18 +273,12 @@ static void free_repo_appdata(struct pakfire_repo_appdata* appdata) {
        if (appdata->description)
                free(appdata->description);
 
-       if (appdata->baseurl)
-               free(appdata->baseurl);
-
        if (appdata->keyfile)
                free(appdata->keyfile);
 
        if (appdata->mirrorlist_url)
                free(appdata->mirrorlist_url);
 
-       if (appdata->mirrorlist)
-               free(appdata->mirrorlist);
-
        free(appdata);
 }
 
@@ -321,6 +313,7 @@ void pakfire_repo_free_all(Pakfire pakfire) {
 
 PAKFIRE_EXPORT PakfireRepo pakfire_repo_create(Pakfire pakfire, const char* name) {
        PakfireRepo repo;
+       int r;
 
        // Return existing repositories with the same name
        repo = pakfire_get_repo(pakfire, name);
@@ -366,19 +359,19 @@ PAKFIRE_EXPORT PakfireRepo pakfire_repo_create(Pakfire pakfire, const char* name
                return repo;
 
        // Make path for metadata
-       repo->appdata->metadata = pakfire_make_cache_path(
-               repo->pakfire, "repodata/%s/repomd.json", pakfire_repo_get_name(repo));
-       if (!repo->appdata->metadata)
+       r = pakfire_make_cache_path(pakfire, repo->appdata->metadata, sizeof(repo->appdata->metadata) - 1,
+               "repodata/%s/repomd.json", pakfire_repo_get_name(repo));
+       if (r < 0)
                goto ERROR;
 
        // Make path to mirrorlist
-       repo->appdata->mirrorlist = pakfire_make_cache_path(
-               repo->pakfire, "repodata/%s/mirrorlist", pakfire_repo_get_name(repo));
-       if (!repo->appdata->mirrorlist)
+       r = pakfire_make_cache_path(pakfire, repo->appdata->mirrorlist, sizeof(repo->appdata->mirrorlist) - 1,
+               "repodata/%s/mirrorlist", pakfire_repo_get_name(repo));
+       if (r < 0)
                goto ERROR;
 
        // Try loading metadata
-       int r = pakfire_repo_read_metadata(repo, repo->appdata->metadata, 0);
+       r = pakfire_repo_read_metadata(repo, repo->appdata->metadata, 0);
        if (r) {
                ERROR(repo->pakfire, "Could not initialize repository metadata: %s\n",
                        strerror(errno));
@@ -837,12 +830,14 @@ PAKFIRE_EXPORT PakfirePackage pakfire_repo_add_archive(PakfireRepo repo, Pakfire
 }
 
 PAKFIRE_EXPORT int pakfire_repo_clean(PakfireRepo repo) {
-       char* cache_path = pakfire_make_cache_path(repo->pakfire, pakfire_repo_get_name(repo));
+       char cache_path[PATH_MAX];
 
-       if (cache_path)
-               return pakfire_cache_destroy(repo->pakfire, cache_path);
+       int r = pakfire_make_cache_path(repo->pakfire, cache_path, sizeof(cache_path) - 1,
+               "%s", pakfire_repo_get_name(repo));
+       if (r < 0)
+               return r;
 
-       return -1;
+       return pakfire_cache_destroy(repo->pakfire, cache_path);
 }
 
 static int pakfire_repo_scan_file(PakfireRepo repo, const char* path) {