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

Andrew Price andyp at fedoraproject.org
Fri Oct 10 16:19:32 UTC 2014


Gitweb:        http://git.fedorahosted.org/git/?p=gfs2-utils.git;a=commitdiff;h=4daadc09b2d0b6b83cee55451747486403343dc3
Commit:        4daadc09b2d0b6b83cee55451747486403343dc3
Parent:        89f0f242424a4cc23cd73f0288d17b52d04a900d
Author:        Andrew Price <anprice at redhat.com>
AuthorDate:    Thu Oct 9 23:35:23 2014 +0100
Committer:     Andrew Price <anprice at redhat.com>
CommitterDate: Thu Oct 9 23:35:23 2014 +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