]> git.ipfire.org Git - pakfire.git/commitdiff
Have pakfire_make_path write to stack
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 19 Apr 2021 15:57:35 +0000 (15:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 19 Apr 2021 15:57:35 +0000 (15:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/db.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/libpakfire.sym
src/libpakfire/pakfire.c
src/libpakfire/pwd.c
src/libpakfire/snapshot.c
src/libpakfire/step.c

index ad12e8a394fc3fa6272ef2f5d4878b3848d8ef60..bbd19a9d561c108942cd11d6c99f719cb0c35513 100644 (file)
@@ -319,20 +319,18 @@ static Py_ssize_t Pakfire_len(PakfireObject* self) {
 
 static PyObject* Pakfire_make_path(PakfireObject* self, PyObject* args) {
        const char* path = NULL;
+       char abspath[PATH_MAX];
 
        if (!PyArg_ParseTuple(args, "s", &path))
                return NULL;
 
-       char* abspath = pakfire_make_path(self->pakfire, path);
-       if (!abspath) {
+       int r = pakfire_make_path(self->pakfire, abspath, path);
+       if (r < 0) {
                PyErr_SetFromErrno(PyExc_OSError);
                return NULL;
        }
 
-       PyObject* ret = PyUnicode_FromString(abspath);
-       free(abspath);
-
-       return ret;
+       return PyUnicode_FromString(abspath);
 }
 
 static PyObject* Pakfire_execute_logging_callback = NULL;
index 0b27c6b2f7407952d61e91d149fff08fdbe767f5..bde27ce2302c0404e9476651422b39713c681d30 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <linux/limits.h>
 #include <stdlib.h>
 #include <time.h>
 
@@ -45,6 +46,7 @@ struct pakfire_db {
        Pakfire pakfire;
        int nrefs;
 
+       char path[PATH_MAX];
        int mode;
 
        sqlite3* handle;
@@ -473,15 +475,15 @@ PAKFIRE_EXPORT int pakfire_db_open(struct pakfire_db** db, Pakfire pakfire, int
        }
 
        // Make the filename
-       char* path = pakfire_make_path(o->pakfire, DATABASE_PATH);
-       if (!path)
+       r = pakfire_make_path(o->pakfire, o->path, DATABASE_PATH);
+       if (r < 0)
                goto END;
 
        // Try to open the sqlite3 database file
-       r = sqlite3_open_v2(path, &o->handle, sqlite3_flags, NULL);
+       r = sqlite3_open_v2(o->path, &o->handle, sqlite3_flags, NULL);
        if (r != SQLITE_OK) {
                ERROR(pakfire, "Could not open database %s: %s\n",
-                       path, sqlite3_errmsg(o->handle));
+                       o->path, sqlite3_errmsg(o->handle));
 
                r = 1;
                goto END;
@@ -499,9 +501,6 @@ END:
        if (r)
                pakfire_db_free(o);
 
-       if (path)
-               free(path);
-
        return r;
 }
 
index a8063b13e9fe1bd0300305428452be07e879a986..7ba1eeec64474cd9a030fcb0c9576dbe52fed6e7 100644 (file)
@@ -36,7 +36,10 @@ Pakfire pakfire_ref(Pakfire pakfire);
 Pakfire pakfire_unref(Pakfire pakfire);
 
 const char* pakfire_get_path(Pakfire pakfire);
-char* pakfire_make_path(Pakfire pakfire, const char* path);
+
+#define pakfire_make_path(pakfire, dst, path) \
+       __pakfire_make_path(pakfire, dst, sizeof(dst) - 1, path)
+int __pakfire_make_path(Pakfire pakfire, char* dst, size_t length, const char* path);
 
 int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags);
 
index 9607d8c5b611d16417ac3b12d720edc072befe42..61e1997c68a0d2e901b57263cebf8655f342fa21 100644 (file)
@@ -38,7 +38,6 @@ global:
        pakfire_get_repo;
        pakfire_get_repos;
        pakfire_make_cache_path;
-       pakfire_make_path;
        pakfire_read_makefile;
        pakfire_ref;
        pakfire_search;
@@ -47,6 +46,7 @@ global:
        pakfire_unref;
        pakfire_version_compare;
        pakfire_whatprovides;
+       __pakfire_make_path;
 
        # arch
        pakfire_arch_is_compatible;
index 0635c49f999c9bfd0a162f374bf810cf83f8113d..0ac98c47d4e4f562674a8190c3f0335aeb1d0c00 100644 (file)
@@ -257,39 +257,38 @@ static const struct pakfire_symlink {
 };
 
 static int pakfire_populate_dev(Pakfire pakfire) {
+       char path[PATH_MAX];
+
        // Create device nodes
        for (const struct pakfire_devnode* devnode = devnodes; devnode->path; devnode++) {
                DEBUG(pakfire, "Creating device node %s\n", devnode->path);
 
-               char* path = pakfire_make_path(pakfire, devnode->path);
+               int r = pakfire_make_path(pakfire, path, devnode->path);
+               if (r < 0)
+                       return 1;
+
                dev_t dev = makedev(devnode->major, devnode->minor);
 
-               int r = mknod(path, devnode->mode, dev);
+               r = mknod(path, devnode->mode, dev);
                if (r) {
                        ERROR(pakfire, "Could not create %s: %s\n", devnode->path, strerror(errno));
-                       free(path);
-
                        return r;
                }
-
-               free(path);
        }
 
        // Create symlinks
        for (const struct pakfire_symlink* s = symlinks; s->target; s++) {
                DEBUG(pakfire, "Creating symlink %s -> %s\n", s->path, s->target);
 
-               char* path = pakfire_make_path(pakfire, s->path);
+               int r = pakfire_make_path(pakfire, path, s->path);
+               if (r < 0)
+                       return 1;
 
-               int r = symlink(s->target, path);
+               r = symlink(s->target, path);
                if (r) {
                        ERROR(pakfire, "Could not create symlink %s: %s\n", s->path, strerror(errno));
-                       free(path);
-
                        return r;
                }
-
-               free(path);
        }
 
        return 0;
@@ -401,14 +400,16 @@ static int pakfire_safety_checks(Pakfire pakfire) {
 }
 
 static int pakfire_read_repo_config(Pakfire pakfire) {
-       char* path = pakfire_make_path(pakfire, PAKFIRE_CONFIG_PATH "/repos");
-       if (!path)
-               return ENOMEM;
+       char path[PATH_MAX];
+
+       int r = pakfire_make_path(pakfire, path, PAKFIRE_CONFIG_PATH "/repos");
+       if (r < 0)
+               return 1;
 
-       int r = 0;
        char* paths[2] = {
                path, NULL,
        };
+       r = 1;
 
        FTS* d = fts_open(paths, FTS_NOCHDIR|FTS_NOSTAT, NULL);
        if (!d)
@@ -443,7 +444,6 @@ static int pakfire_read_repo_config(Pakfire pakfire) {
 ERROR:
        if (d)
                fts_close(d);
-       free(path);
 
        return r;
 }
@@ -498,12 +498,12 @@ static int pakfire_config_import_distro(Pakfire pakfire) {
 }
 
 static int pakfire_read_config(Pakfire pakfire, const char* path) {
-       char* default_path = NULL;
+       char default_path[PATH_MAX];
 
        // Use default path if none set
        if (!path) {
-               default_path = pakfire_make_path(pakfire, PAKFIRE_CONFIG_PATH "/general.conf");
-               if (!default_path)
+               int r = pakfire_make_path(pakfire, default_path, PAKFIRE_CONFIG_PATH "/general.conf");
+               if (r < 0)
                        return 1;
 
                path = default_path;
@@ -514,7 +514,7 @@ static int pakfire_read_config(Pakfire pakfire, const char* path) {
        FILE* f = fopen(path, "r");
        if (!f) {
                // Silently ignore when there is no default configuration file
-               if (default_path && errno == ENOENT)
+               if (*default_path && errno == ENOENT)
                        return 0;
 
                return 1;
@@ -538,29 +538,29 @@ static int pakfire_read_config(Pakfire pakfire, const char* path) {
 ERROR:
        fclose(f);
 
-       if (default_path)
-               free(default_path);
-
        return r;
 }
 
 static int pakfire_read_os_release(Pakfire pakfire) {
+       char path[PATH_MAX];
        char* line = NULL;
        size_t l = 0;
-       int r = 1;
 
-       char* path = pakfire_make_path(pakfire, "/etc/os-release");
-       if (!path)
-               return ENOMEM;
+       int r = pakfire_make_path(pakfire, path, "/etc/os-release");
+       if (r < 0)
+               return 1;
+
+       r = 1;
 
        FILE* f = fopen(path, "r");
        if (!f) {
                // Ignore when the file does not exist
-               if (errno == ENOENT)
+               if (errno == ENOENT) {
+                       r = 0;
                        goto ERROR;
+               }
 
                ERROR(pakfire, "Could not open %s: %s\n", path, strerror(errno));
-               r = 1;
                goto ERROR;
        }
 
@@ -615,7 +615,6 @@ ERROR:
                fclose(f);
        if (line)
                free(line);
-       free(path);
 
        return r;
 }
@@ -728,14 +727,16 @@ PAKFIRE_EXPORT int pakfire_create(
        if (r)
                goto ERROR;
 
+       // Make path for private files
+       char private_dir[PATH_MAX];
+       r = pakfire_make_path(p, private_dir, PAKFIRE_PRIVATE_DIR);
+       if (r < 0)
+               goto ERROR;
+
        // Make sure that our private directory exists
-       char* private_dir = pakfire_make_path(p, PAKFIRE_PRIVATE_DIR);
        r = pakfire_mkdir(private_dir, 0);
        if (r && errno != EEXIST) {
-               ERROR(p, "Could not create private directory %s: %s\n",
-                       private_dir, strerror(errno));
-               free(private_dir);
-
+               ERROR(p, "Could not create private directory %s: %s\n", private_dir, strerror(errno));
                goto ERROR;
        }
 
@@ -778,41 +779,34 @@ PAKFIRE_EXPORT const char* pakfire_get_path(Pakfire pakfire) {
        return pakfire->path;
 }
 
-PAKFIRE_EXPORT char* pakfire_make_path(Pakfire pakfire, const char* path) {
-       char buffer[PATH_MAX];
-
+PAKFIRE_EXPORT int __pakfire_make_path(Pakfire pakfire,
+               char* dst, size_t length, const char* path) {
        // Make sure that path never starts with /
        while (path && *path == '/')
                path++;
 
-       int r = pakfire_path_join(buffer, pakfire->path, path);
-       if (r < 0)
-               return NULL;
-
-       return strdup(buffer);
+       return pakfire_path_join(dst, pakfire->path, path);
 }
 
 PAKFIRE_EXPORT int pakfire_bind(Pakfire pakfire, const char* src, const char* dst, int flags) {
+       char mountpoint[PATH_MAX];
+
        if (!dst)
                dst = src;
 
-       char* mountpoint = pakfire_make_path(pakfire, dst);
-       if (!mountpoint)
+       int r = pakfire_make_path(pakfire, mountpoint, dst);
+       if (r < 0)
                return 1;
 
        DEBUG(pakfire, "Mounting %s to %s\n", src, mountpoint);
 
        // Make sure the directory exists
-       int r = pakfire_mkdir(mountpoint, 0);
+       r = pakfire_mkdir(mountpoint, 0);
        if (r && errno != EEXIST)
-               goto ERROR;
+               return r;
 
        // Perform mount
-       r = __mount(pakfire, src, mountpoint, NULL, flags|MS_BIND, NULL);
-
-ERROR:
-       free(mountpoint);
-       return r;
+       return __mount(pakfire, src, mountpoint, NULL, flags|MS_BIND, NULL);
 }
 
 static int pakfire_copy(Pakfire pakfire, const char* src, const char* dst) {
@@ -900,14 +894,12 @@ PAKFIRE_EXPORT int pakfire_copy_in(Pakfire pakfire, const char* src, const char*
                return 1;
        }
 
-       char* path = pakfire_make_path(pakfire, dst);
-       if (!path)
+       char path[PATH_MAX];
+       int r = pakfire_make_path(pakfire, path, dst);
+       if (r < 0)
                return 1;
 
-       int r = pakfire_copy(pakfire, src, path);
-       free(path);
-
-       return r;
+       return pakfire_copy(pakfire, src, path);
 }
 
 PAKFIRE_EXPORT int pakfire_copy_out(Pakfire pakfire, const char* src, const char* dst) {
@@ -916,14 +908,12 @@ PAKFIRE_EXPORT int pakfire_copy_out(Pakfire pakfire, const char* src, const char
                return 1;
        }
 
-       char* path = pakfire_make_path(pakfire, src);
-       if (!path)
+       char path[PATH_MAX];
+       int r = pakfire_make_path(pakfire, path, src);
+       if (r < 0)
                return 1;
 
-       int r = pakfire_copy(pakfire, path, dst);
-       free(path);
-
-       return r;
+       return pakfire_copy(pakfire, path, dst);
 }
 
 PAKFIRE_EXPORT const char* pakfire_get_arch(Pakfire pakfire) {
index 01a965399bc195d780e9e9b29fedb7cf05eb0836..0e25bbe54890e11198d5a8efda3d0df7aea517d0 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <grp.h>
+#include <linux/limits.h>
 #include <pwd.h>
 #include <stdio.h>
 #include <stdlib.h>
 static struct passwd* pakfire_getpwent(Pakfire pakfire,
                int(*cmp)(struct passwd* entry, const void* value), const void* value) {
        struct passwd* entry = NULL;
+       char path[PATH_MAX];
 
        // Get path to /etc/passwd
-       char* path = pakfire_make_path(pakfire, "/etc/passwd");
-       if (!path)
+       int r = pakfire_make_path(pakfire, path, "/etc/passwd");
+       if (r < 0)
                return NULL;
 
        FILE* f = fopen(path, "r");
@@ -53,8 +55,6 @@ static struct passwd* pakfire_getpwent(Pakfire pakfire,
        }
 
 END:
-       free(path);
-
        if (f)
                fclose(f);
 
@@ -90,10 +90,11 @@ struct passwd* pakfire_getpwuid(Pakfire pakfire, uid_t uid) {
 static struct group* pakfire_getgrent(Pakfire pakfire,
                int(*cmp)(struct group* entry, const void* value), const void* value) {
        struct group* entry = NULL;
+       char path[PATH_MAX];
 
        // Get path to /etc/group
-       char* path = pakfire_make_path(pakfire, "/etc/group");
-       if (!path)
+       int r = pakfire_make_path(pakfire, path, "/etc/group");
+       if (r < 0)
                return NULL;
 
        FILE* f = fopen(path, "r");
@@ -114,8 +115,6 @@ static struct group* pakfire_getgrent(Pakfire pakfire,
        }
 
 END:
-       free(path);
-
        if (f)
                fclose(f);
 
index 5ace04968d4c952ba7154090aa19991255140745..e6c6b4ce0403c08d2b4fd5b1ef570938cd52b5f5 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <linux/limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -207,6 +208,7 @@ ERROR:
 }
 
 static int pakfire_snapshot_extract(Pakfire pakfire, FILE* f) {
+       char sourcepath[PATH_MAX];
        int r = 1;
 
        struct archive* a = archive_read_new();
@@ -245,12 +247,11 @@ static int pakfire_snapshot_extract(Pakfire pakfire, FILE* f) {
                DEBUG(pakfire, "Extracting %s...\n", path);
 
                // Update path
-               char* sourcepath = pakfire_make_path(pakfire, path);
-               if (!sourcepath)
+               r = pakfire_make_path(pakfire, sourcepath, path);
+               if (r < 0)
                        goto ERROR;
 
                archive_entry_set_pathname(e, sourcepath);
-               free(sourcepath);
 
                // Extract the file
                r = archive_read_extract2(a, e, disk);
index c3b7d42f6b59a20107b762b70a12957fed8f8984..2ff6d8e6c76a6bde34c2a2a18758daed8f1e5593 100644 (file)
@@ -19,6 +19,7 @@
 #############################################################################*/
 
 #include <errno.h>
+#include <linux/limits.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -266,21 +267,21 @@ static int pakfire_step_run_script(PakfireStep step,
 }
 
 static int pakfire_run_ldconfig(PakfireStep step) {
-       int r = -1;
+       char path[PATH_MAX];
 
        // Check if ldconfig exists before calling it to avoid overhead
-       char* path = pakfire_make_path(step->pakfire, LDCONFIG);
-       if (!path)
+       int r = pakfire_make_path(step->pakfire, path, LDCONFIG);
+       if (r < 0)
                return 1;
 
+       r = -1;
+
        if (access(path, X_OK) == 0) {
                r = pakfire_execute_command(step->pakfire, LDCONFIG, NULL, 0, NULL, NULL);
 
                DEBUG(step->pakfire, "ldconfig returned %d\n", r);
        }
 
-       free(path);
-
        return r;
 }