[git commit master 1/1] modprobe/insmod: fix parameter quoting

Denys Vlasenko vda.linux at googlemail.com
Tue Feb 1 23:01:07 UTC 2011


commit: http://git.busybox.net/busybox/commit/?id=c5830bdf6558edbc567d7c5dce1ff8059049e202
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
parse_cmdline_module_options                         102     157     +55
modprobe_main                                        657     662      +5
insmod_main                                           68      70      +2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 62/0)               Total: 62 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 modutils/insmod.c   |    2 +-
 modutils/modprobe.c |    2 +-
 modutils/modutils.c |   32 +++++++++++++++++++++++++-------
 modutils/modutils.h |    2 +-
 4 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/modutils/insmod.c b/modutils/insmod.c
index e5b46f4..94e4e28 100644
--- a/modutils/insmod.c
+++ b/modutils/insmod.c
@@ -59,7 +59,7 @@ int insmod_main(int argc UNUSED_PARAM, char **argv)
 	if (!filename)
 		bb_show_usage();
 
-	rc = bb_init_module(filename, parse_cmdline_module_options(argv));
+	rc = bb_init_module(filename, parse_cmdline_module_options(argv, /*quote_spaces:*/ 0));
 	if (rc)
 		bb_error_msg("can't insert '%s': %s", filename, moderror(rc));
 
diff --git a/modutils/modprobe.c b/modutils/modprobe.c
index e3bacac..8d2ccc5 100644
--- a/modutils/modprobe.c
+++ b/modutils/modprobe.c
@@ -589,7 +589,7 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv)
 		/* First argument is module name, rest are parameters */
 		DBG("probing just module %s", *argv);
 		add_probe(argv[0]);
-		G.cmdline_mopts = parse_cmdline_module_options(argv);
+		G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
 	}
 
 	/* Happens if all requested modules are already loaded */
diff --git a/modutils/modutils.c b/modutils/modutils.c
index 415dbbe..6187ca7 100644
--- a/modutils/modutils.c
+++ b/modutils/modutils.c
@@ -62,7 +62,7 @@ char* FAST_FUNC filename2modname(const char *filename, char *modname)
 	return modname;
 }
 
-char* FAST_FUNC parse_cmdline_module_options(char **argv)
+char* FAST_FUNC parse_cmdline_module_options(char **argv, int quote_spaces)
 {
 	char *options;
 	int optlen;
@@ -70,13 +70,31 @@ char* FAST_FUNC parse_cmdline_module_options(char **argv)
 	options = xzalloc(1);
 	optlen = 0;
 	while (*++argv) {
-		options = xrealloc(options, optlen + 2 + strlen(*argv) + 2);
-		/* Spaces handled by "" pairs, but no way of escaping quotes */
-//TODO: module-init-tools version 3.11.1 quotes only value:
-//it generates var="val with spaces", not "var=val with spaces"
-//(and it won't quote var *name* even if it has spaces)
-		optlen += sprintf(options + optlen, (strchr(*argv, ' ') ? "\"%s\" " : "%s "), *argv);
+		const char *fmt;
+		const char *var;
+		const char *val;
+
+		var = *argv;
+		options = xrealloc(options, optlen + 2 + strlen(var) + 2);
+		fmt = "%.*s%s ";
+		val = strchrnul(var, '=');
+		if (quote_spaces) {
+			/*
+			 * modprobe (module-init-tools version 3.11.1) compat:
+			 * quote only value:
+			 * var="val with spaces", not "var=val with spaces"
+			 * (note: var *name* is not checked for spaces!)
+			 */
+			if (*val) { /* has var=val format. skip '=' */
+				val++;
+				if (strchr(val, ' '))
+					fmt = "%.*s\"%s\" ";
+			}
+		}
+		optlen += sprintf(options + optlen, fmt, (int)(val - var), var, val);
 	}
+	/* Remove trailing space. Disabled */
+	/* if (optlen != 0) options[optlen-1] = '\0'; */
 	return options;
 }
 
diff --git a/modutils/modutils.h b/modutils/modutils.h
index 863bc26..5f059c7 100644
--- a/modutils/modutils.h
+++ b/modutils/modutils.h
@@ -21,7 +21,7 @@ void replace(char *s, char what, char with) FAST_FUNC;
 char *replace_underscores(char *s) FAST_FUNC;
 int string_to_llist(char *string, llist_t **llist, const char *delim) FAST_FUNC;
 char *filename2modname(const char *filename, char *modname) FAST_FUNC;
-char *parse_cmdline_module_options(char **argv) FAST_FUNC;
+char *parse_cmdline_module_options(char **argv, int quote_spaces) FAST_FUNC;
 
 /* insmod for 2.4 and modprobe's options (insmod 2.6 has no options at all): */
 #define INSMOD_OPTS \
-- 
1.7.3.4



More information about the busybox-cvs mailing list