From 9a460514997d9731923225483a90e7ea45a9955c Mon Sep 17 00:00:00 2001 From: Duncan Overbruck Date: Thu, 22 Dec 2022 21:42:32 +0100 Subject: [PATCH] bin/xbps-create: cleanup readlink related code --- bin/xbps-create/main.c | 45 ++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/bin/xbps-create/main.c b/bin/xbps-create/main.c index 985a18d6..d61f0aa0 100644 --- a/bin/xbps-create/main.c +++ b/bin/xbps-create/main.c @@ -311,9 +311,8 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED struct xentry *xe = NULL; xbps_dictionary_t fileinfo = NULL; const char *filep = NULL; - char *buf, *p, *p2, *dname; + char *p, *p2, *dname; char sha256[XBPS_SHA256_SIZE]; - ssize_t r; /* Ignore metadata files generated by xbps-src and destdir */ if ((strcmp(fpath, ".") == 0) || @@ -353,6 +352,9 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED } if (S_ISLNK(sb->st_mode)) { + char buf[PATH_MAX]; + ssize_t len; + /* * Symlinks. * @@ -362,13 +364,12 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED xe->type = strdup("links"); assert(xe->type); xbps_dictionary_set_cstring_nocopy(fileinfo, "type", "links"); - buf = malloc(sb->st_size+1); - assert(buf); - r = readlink(fpath, buf, sb->st_size+1); - if (r < 0 || r > sb->st_size) - die("failed to process symlink %s:", fpath); - buf[sb->st_size] = '\0'; + len = readlink(fpath, buf, sizeof(buf)); + if (len < 0 || len >= (int)sizeof(buf)) + die("readlink: %s:", fpath); + buf[len] = '\0'; + /* * Check if symlink is absolute or relative; on the former * make it absolute for the target object. @@ -422,7 +423,6 @@ ftw_cb(const char *fpath, const struct stat *sb, const struct dirent *dir UNUSED } assert(xe->target); assert(xbps_dictionary_get(fileinfo, "target")); - free(buf); } else if (S_ISREG(sb->st_mode)) { struct xentry *xep; bool hlink = false; @@ -671,8 +671,7 @@ process_entry_file(struct archive *ar, { struct archive_entry *entry, *sparse_entry; struct stat st; - char *buf = NULL, *p; - ssize_t len; + char path[PATH_MAX]; assert(ar); assert(xe); @@ -680,8 +679,9 @@ process_entry_file(struct archive *ar, if (filematch && strcmp(xe->file, filematch)) return; - p = xbps_xasprintf("%s/%s", destdir, xe->file); - if (lstat(p, &st) == -1) + if (xbps_path_join(path, sizeof(path), destdir, xe->file, (char *)NULL) == -1) + die("path too long %s:", xe->file); + if (lstat(path, &st) == -1) die("failed to add entry (fstat) %s to archive:", xe->file); entry = archive_entry_new(); @@ -695,24 +695,23 @@ process_entry_file(struct archive *ar, st.st_gid = 0; archive_entry_copy_stat(entry, &st); - archive_entry_copy_sourcepath(entry, p); + archive_entry_copy_sourcepath(entry, path); if (st.st_uid == geteuid()) archive_entry_set_uname(entry, "root"); if (st.st_gid == getegid()) archive_entry_set_gname(entry, "root"); if (S_ISLNK(st.st_mode)) { - buf = malloc(st.st_size+1); - if (buf == NULL) - die("failed to allocate readlink buffer", xe->file); - len = readlink(p, buf, st.st_size+1); - if (len < 0 || len > st.st_size) - die("failed to add entry %s (readlink) to archive:", - xe->file); + char buf[PATH_MAX]; + ssize_t len; + + len = readlink(path, buf, sizeof(buf)); + if (len < 0 || len >= (int)sizeof(buf)) + die("readlink: %s:", xe->file); buf[len] = '\0'; + archive_entry_set_symlink(entry, buf); } - free(p); archive_entry_linkify(resolver, &entry, &sparse_entry); @@ -720,8 +719,6 @@ process_entry_file(struct archive *ar, write_entry(ar, entry); if (sparse_entry != NULL) write_entry(ar, sparse_entry); - if (buf) - free(buf); } static void