gfs2-utils: RHEL7 - gfs2_grow: Don't use PATH_MAX in main_grow

Andrew Price andyp at fedoraproject.org
Tue Apr 7 21:22:53 UTC 2015


Gitweb:        http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=c319aec6eed4238492b1ed707c08d79ca2df6bef
Commit:        c319aec6eed4238492b1ed707c08d79ca2df6bef
Parent:        5bde01ccacb073f287d05930f33de6ab4c485e9f
Author:        Andrew Price <anprice at redhat.com>
AuthorDate:    Thu Oct 9 23:35:23 2014 +0100
Committer:     Andrew Price <anprice at redhat.com>
CommitterDate: Wed Apr 1 16:51:34 2015 +0100

gfs2_grow: Don't use PATH_MAX in main_grow

Although "large enough" for most cases, PATH_MAX is an arbitrary limit
for a file path and doesn't reflect reality. Use asprintf(3) to allocate
only as much memory as we need to hold the path to the rindex. open(2)
will tell us if it's too long.

Signed-off-by: Andrew Price <anprice at redhat.com>
---
 gfs2/mkfs/main_grow.c |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/gfs2/mkfs/main_grow.c b/gfs2/mkfs/main_grow.c
index 79ba56a..33618de 100644
--- a/gfs2/mkfs/main_grow.c
+++ b/gfs2/mkfs/main_grow.c
@@ -320,11 +320,28 @@ static void print_info(struct gfs2_sbd *sdp, char *device, char *mnt_path)
 		   (unsigned long long)(fsgrowth * sdp->bsize) / MB);
 }
 
+static int open_rindex(char *metafs_path, int mode)
+{
+	char *path;
+	int fd;
+
+	if (asprintf(&path, "%s/rindex", metafs_path) < 0) {
+		perror(_("Failed to open rindex"));
+		return -1;
+	}
+	fd = open(path, (mode | O_CLOEXEC));
+	if (fd < 0) {
+		perror(path);
+		fprintf(stderr, _("Please run fsck.gfs2\n"));
+	}
+	free(path);
+	return fd;
+}
+
 void main_grow(int argc, char *argv[])
 {
 	struct gfs2_sbd sbd, *sdp = &sbd;
 	int rindex_fd;
-	char rindex_name[PATH_MAX];
 	int error = EXIT_SUCCESS;
 	int devflags = (test ? O_RDONLY : O_RDWR) | O_CLOEXEC;
 
@@ -376,12 +393,9 @@ void main_grow(int argc, char *argv[])
 			perror(_("Failed to mount GFS2 meta file system"));
 			exit(EXIT_FAILURE);
 		}
-
-		sprintf(rindex_name, "%s/rindex", sdp->metafs_path);
-		rindex_fd = open(rindex_name, (test ? O_RDONLY : O_RDWR) | O_CLOEXEC);
+		rindex_fd = open_rindex(sdp->metafs_path, (test ? O_RDONLY : O_RDWR));
 		if (rindex_fd < 0) {
 			cleanup_metafs(sdp);
-			perror(_("GFS2 rindex not found. Please run fsck.gfs2.\n"));
 			exit(EXIT_FAILURE);
 		}
 		/* Get master dinode */


More information about the cluster-commits mailing list