[PATCH 1/2] runcmd: Fix quoting issues when running commands

Robin Sonefors robin.sonefors at op5.com
Fri Feb 22 16:20:24 CET 2013


The important part of this patch is one line that makes sure that if
we're in single quoted mode, we should ignore '\' as an escape
character.

Without this, foo\\bar was interprented exactly the same as 'foo\\bar'.
With it, the former is executed as one backslash, the latter as two.

There's is also a new test-runcmd, with a small set of test cases for
the command quoter.

Signed-off-by: Robin Sonefors <robin.sonefors at op5.com>
---
 lib/Makefile.in   |  4 ++--
 lib/runcmd.c      |  3 ++-
 lib/runcmd.h      |  2 ++
 lib/test-runcmd.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 63 insertions(+), 3 deletions(-)
 create mode 100644 lib/test-runcmd.c

diff --git a/lib/Makefile.in b/lib/Makefile.in
index f853ce7..fdf3962 100644
--- a/lib/Makefile.in
+++ b/lib/Makefile.in
@@ -8,8 +8,8 @@ LIBNAME = libnagios.a
 all: $(LIBNAME)
 
 SNPRINTF_O=@SNPRINTF_O@
-TESTED_SRC_C := squeue.c kvvec.c iocache.c iobroker.c bitmap.c dkhash.c
-SRC_C := $(TESTED_SRC_C) pqueue.c runcmd.c worker.c skiplist.c nsock.c
+TESTED_SRC_C := squeue.c kvvec.c iocache.c iobroker.c bitmap.c dkhash.c runcmd.c
+SRC_C := $(TESTED_SRC_C) pqueue.c worker.c skiplist.c nsock.c
 SRC_C += nspath.c
 SRC_O := $(patsubst %.c,%.o,$(SRC_C)) $(SNPRINTF_O)
 TESTS := $(patsubst %.c,test-%,$(TESTED_SRC_C))
diff --git a/lib/runcmd.c b/lib/runcmd.c
index 8664fcd..95bd4c5 100644
--- a/lib/runcmd.c
+++ b/lib/runcmd.c
@@ -151,7 +151,8 @@ int runcmd_cmd2strv(const char *str, int *out_argc, char **out_argv)
 			break;
 
 		case '\\':
-			i++;
+			if (!have_state(STATE_INSQ))
+				i++;
 			break;
 
 		case '\'':
diff --git a/lib/runcmd.h b/lib/runcmd.h
index 2217dc4..63e94d2 100644
--- a/lib/runcmd.h
+++ b/lib/runcmd.h
@@ -1,6 +1,8 @@
 #ifndef INCLUDE_runcmd_h__
 #define INCLUDE_runcmd_h__
 
+#include <signal.h>
+
 /**
  * @file runcmd.h
  * @brief runcmd library function declarations
diff --git a/lib/test-runcmd.c b/lib/test-runcmd.c
new file mode 100644
index 0000000..8bbff42
--- /dev/null
+++ b/lib/test-runcmd.c
@@ -0,0 +1,57 @@
+#include "runcmd.c"
+#include "t-utils.h"
+#include <stdio.h>
+
+#define BUF_SIZE 1024
+
+struct cases {
+	char *input;
+	char *output;
+};
+
+struct cases cases[] = {
+	{"te\\st1", "test1"},
+	{"te\\\\st2", "te\\st2"},
+	{"te\\\\\\st3", "te\\st3"},
+	{"te\\\\\\\\st4", "te\\\\st4"},
+
+	{"\"te\\st5\"", "test5"},
+	{"\"te\\\\st6\"", "te\\st6"},
+	{"\"te\\\\\\st7\"", "te\\st7"},
+	{"\"te\\\\\\\\st8\"", "te\\\\st8"},
+
+	{"'te\\st9'", "te\\st9"},
+	{"'te\\\\st10'", "te\\\\st10"},
+	{"'te\\\\\\st11'", "te\\\\\\st11"},
+	{"'te\\\\\\\\st12'", "te\\\\\\\\st12"},
+
+	{"\\'te\\\\st13", "'te\\st13"},
+	{"'test14\"'", "test14\""},
+	{NULL},
+};
+
+int main(int argc, char **argv)
+{
+	runcmd_init();
+	t_set_colors(0);
+	t_start("runcmd test");
+	{
+		int i;
+		char *out = calloc(1, BUF_SIZE);
+		for (i = 0; cases[i].input != NULL; i++) {
+			memset(out, 0, BUF_SIZE);
+			int pfd[2] = {-1, -1}, pfderr[2] = {-1, -1};
+			int fd;
+			char *cmd;
+			asprintf(&cmd, "/bin/echo -n %s", cases[i].input);
+			fd = runcmd_open(cmd, pfd, pfderr, NULL);
+			read(pfd[0], out, BUF_SIZE);
+			ok_str(cases[i].output, out, "Echoing a command should give expected output");
+			close(pfd[0]);
+			close(pfderr[0]);
+			close(fd);
+		}
+	}
+	t_end();
+	return 0;
+}
-- 
1.7.11.7


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb




More information about the Developers mailing list