[PATCH] sbin/mount WARNS=6 cleanups

Peter Schuller peter.schuller at infidyne.com
Fri Apr 1 14:46:13 PST 2005


  * made appropriate functions static
  * replaced unchecked strdup():s with xstrdup()

mount2.patch:

  Depends on mount.patch.

  Separate patch since I suspect this might not be so popular. It fixes
  the discarded const qualifier problem (which is fundamentally caused by
  the signature of getopt()) by duping the array before calling mount_ufs()
  and before the execv. The rest of mount.c is heavily dependent on constant
  strings and pointer acrobatics that does not play well with the idea of
  making argv a char**. I felt this was the cleaner solution.

  Since this removes the last triggered warning, it also bumps WARNS to 6.

-- 
/ Peter Schuller, InfiDyne Technologies HB

PGP userID: 0xE9758B7D or 'Peter Schuller <peter.schuller at xxxxxxxxxxxx>'
Key retrieval: Send an E-Mail to getpgpkey at xxxxxxxxx
E-Mail: peter.schuller at xxxxxxxxxxxx Web: http://www.scode.org

--- sbin/mount/mount.c.orig	2005-04-01 12:33:26.000000000 +0000
+++ sbin/mount/mount.c	2005-04-01 14:12:18.000000000 +0000
@@ -61,21 +61,22 @@
 
 int debug, fstab_style, verbose;
 
-char   *catopt(char *, const char *);
-struct statfs
-       *getmntpt(const char *);
-int	hasopt(const char *, const char *);
-int	ismounted(struct fstab *, struct statfs *, int);
-int	isremountable(const char *);
-void	mangle(char *, int *, const char **);
-char   *update_options(char *, char *, int);
-int	mountfs(const char *, const char *, const char *,
-			int, const char *, const char *);
-void	remopt(char *, const char *);
-void	prmount(struct statfs *);
-void	putfsent(const struct statfs *);
-void	usage(void);
-char   *flags2opts(int);
+static char  *catopt(char *, const char *);
+static struct statfs
+             *getmntpt(const char *);
+static int    hasopt(const char *, const char *);
+static int    ismounted(struct fstab *, struct statfs *, int);
+static int    isremountable(const char *);
+static void   mangle(char *, int *, const char **);
+static char  *update_options(char *, char *, int);
+static int    mountfs(const char *, const char *, const char *,
+		      int, const char *, const char *);
+static void   remopt(char *, const char *);
+static void   prmount(struct statfs *);
+static void   putfsent(const struct statfs *);
+static void   usage(void);
+static char  *flags2opts(int);
+static char  *xstrdup(const char *str);
 
 /* Map from mount options to printable formats. */
 static struct opt {
@@ -301,7 +302,7 @@
 	exit(rval);
 }
 
-int
+static int
 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
 {
 	int i;
@@ -318,7 +319,7 @@
 	return (0);
 }
 
-int
+static int
 isremountable(const char *vfsname)
 {
 	const char **cp;
@@ -329,7 +330,7 @@
 	return (0);
 }
 
-int
+static int
 hasopt(const char *mntopts, const char *option)
 {
 	int negative, found;
@@ -340,7 +341,7 @@
 		option += 2;
 	} else
 		negative = 0;
-	optbuf = strdup(mntopts);
+	optbuf = xstrdup(mntopts);
 	found = 0;
 	for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
 		if (opt[0] == 'n' && opt[1] == 'o') {
@@ -353,7 +354,7 @@
 	return (found);
 }
 
-int
+static int
 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
         const char *options, const char *mntopts)
 {
@@ -388,7 +389,7 @@
 			mntopts = "";
 		}
 	}
-	optbuf = catopt(strdup(mntopts), options);
+	optbuf = catopt(xstrdup(mntopts), options);
 
 	if (strcmp(name, "/") == 0)
 		flags |= MNT_UPDATE;
@@ -484,7 +485,7 @@
 	return (0);
 }
 
-void
+static void
 prmount(struct statfs *sfp)
 {
 	int flags;
@@ -518,7 +519,7 @@
 	printf(")\n");
 }
 
-struct statfs *
+static struct statfs *
 getmntpt(const char *name)
 {
 	struct statfs *mntbuf;
@@ -533,7 +534,7 @@
 	return (NULL);
 }
 
-char *
+static char *
 catopt(char *s0, const char *s1)
 {
 	size_t i;
@@ -548,14 +549,14 @@
 			errx(1, "malloc failed");
 		snprintf(cp, i, "%s,%s", s0, s1);
 	} else
-		cp = strdup(s1);
+		cp = xstrdup(s1);
 
 	if (s0)
 		free(s0);
 	return (cp);
 }
 
-void
+static void
 mangle(char *options, int *argcp, const char **argv)
 {
 	char *p, *s;
@@ -581,7 +582,7 @@
 }
 
 
-char *
+static char *
 update_options(char *opts, char *fstab, int curflags)
 {
 	char *o, *p;
@@ -589,7 +590,7 @@
 	char *expopt, *newopt, *tmpopt;
 
 	if (opts == NULL)
-		return strdup("");
+		return xstrdup("");
 
 	/* remove meta options from list */
 	remopt(fstab, MOUNT_META_OPTION_FSTAB);
@@ -636,7 +637,7 @@
 	return newopt;
 }
 
-void
+static void
 remopt(char *string, const char *opt)
 {
 	char *o, *p, *r;
@@ -658,7 +659,7 @@
 	*r = '\0';
 }
 
-void
+static void
 usage(void)
 {
 
@@ -669,7 +670,7 @@
 	exit(1);
 }
 
-void
+static void
 putfsent(const struct statfs *ent)
 {
 	struct fstab *fst;
@@ -694,7 +695,7 @@
 }
 
 
-char *
+static char *
 flags2opts(int flags)
 {
 	char *res;
@@ -717,3 +718,13 @@
 
 	return res;
 }
+
+static char*
+xstrdup(const char *str)
+{
+	char* ret = strdup(str);
+	if(ret == NULL) {
+		errx(1, "strdup failed (could not allocate memory)");
+	}
+	return ret;
+}
--- sbin/mount/mount.c.intermediary	2005-04-01 14:28:23.000000000 +0000
+++ sbin/mount/mount.c	2005-04-01 14:38:29.000000000 +0000
@@ -77,6 +77,7 @@
 static void   usage(void);
 static char  *flags2opts(int);
 static char  *xstrdup(const char *str);
+static char **arrdup(const char** a);
 
 /* Map from mount options to printable formats. */
 static struct opt {
@@ -428,13 +429,13 @@
 		return (1);
 	case 0:					/* Child. */
 		if (strcmp(vfstype, "ufs") == 0)
-			exit(mount_ufs(argc, (char * const *) argv));
+			exit(mount_ufs(argc, arrdup(argv)));
 
 		/* Go find an executable. */
 		for (edir = edirs; *edir; edir++) {
 			snprintf(execname,
 			    sizeof(execname), "%s/mount_%s", *edir, vfstype);
-			execv(execname, (char * const *)argv);
+			execv(execname, arrdup(argv));
 		}
 		if (errno == ENOENT) {
 			int len = 0;
@@ -728,3 +729,27 @@
 	}
 	return ret;
 }
+
+/* Duplicate a zero-terminated array of strings. */
+static char **
+arrdup(const char** a)
+{
+	size_t len;
+	size_t i;
+	char** ret;
+
+	for(i = 0; a[i] != NULL; i++)
+		;
+	len = i;
+	
+	ret = malloc(len * sizeof(char*));
+	if(ret == NULL) {
+		errx(1, "malloc failed - cannot duplicate array");
+	}
+	for(i = 0; i < len; i++) {
+		ret[i] = xstrdup(a[i]);
+	}
+
+	return ret;
+}
+




More information about the Submit mailing list