From: Michael Tremer Date: Mon, 19 Apr 2021 15:57:35 +0000 (+0000) Subject: Have pakfire_make_path write to stack X-Git-Tag: 0.9.28~1285^2~325 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=097b6ca69f68718963183b5a5c0c732a90e7bbba;p=pakfire.git Have pakfire_make_path write to stack Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index ad12e8a39..bbd19a9d5 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -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; diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index 0b27c6b2f..bde27ce23 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include @@ -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; } diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index a8063b13e..7ba1eeec6 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -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); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 9607d8c5b..61e1997c6 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -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; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 0635c49f9..0ac98c47d 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -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) { diff --git a/src/libpakfire/pwd.c b/src/libpakfire/pwd.c index 01a965399..0e25bbe54 100644 --- a/src/libpakfire/pwd.c +++ b/src/libpakfire/pwd.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -29,10 +30,11 @@ 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); diff --git a/src/libpakfire/snapshot.c b/src/libpakfire/snapshot.c index 5ace04968..e6c6b4ce0 100644 --- a/src/libpakfire/snapshot.c +++ b/src/libpakfire/snapshot.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include @@ -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); diff --git a/src/libpakfire/step.c b/src/libpakfire/step.c index c3b7d42f6..2ff6d8e6c 100644 --- a/src/libpakfire/step.c +++ b/src/libpakfire/step.c @@ -19,6 +19,7 @@ #############################################################################*/ #include +#include #include #include #include @@ -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; }