summaryrefslogtreecommitdiff
path: root/findutils/xargs.c
diff options
context:
space:
mode:
authorRon Yorston2020-01-24 13:16:45 +0000
committerDenys Vlasenko2020-01-29 14:39:13 +0100
commit1ff7002b1d229c678fdffebec602fb4c54439a31 (patch)
tree71b5f1f29037301c29e6ce41ddca51da3cd237ce /findutils/xargs.c
parent16bcd504a32e6a7bf2015ffb241133f9ead6100b (diff)
downloadbusybox-1ff7002b1d229c678fdffebec602fb4c54439a31.zip
busybox-1ff7002b1d229c678fdffebec602fb4c54439a31.tar.gz
xargs: fix handling of quoted arguments, closes 11441
As reported in bug 11441 when presented with a large number of quoted arguments xargs can return 'argument line too long': seq 10000 29999 | sed -e 's/^/"/' -e 's/$/"/' | busybox xargs echo This happens because the variant of process_stdin() which handles quoted arguments doesn't preserve state between calls. If the allowed number of characters is exceeded part way through a quoted argument the next call to process_stdin() incorrectly treats the terminating quote as a starting quote, thus quoting all of the argument separators. function old new delta process_stdin 274 303 +29 xargs_main 731 745 +14 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/0 up/down: 43/0) Total: 43 bytes Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'findutils/xargs.c')
-rw-r--r--findutils/xargs.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c
index 7263158..4fb306b 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -114,17 +114,28 @@ struct globals {
int max_procs;
#endif
smalluint xargs_exitcode;
+#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
+#define NORM 0
+#define QUOTE 1
+#define BACKSLASH 2
+#define SPACE 4
+ smalluint process_stdin__state;
+ char process_stdin__q;
+#endif
} FIX_ALIASING;
#define G (*(struct globals*)bb_common_bufsiz1)
#define INIT_G() do { \
setup_common_bufsiz(); \
- G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
+ IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
+ IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
+ /* Even zero values are set because we are NOEXEC applet */ \
+ G.eof_str = NULL; \
G.idx = 0; \
IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.running_procs = 0;) \
IF_FEATURE_XARGS_SUPPORT_PARALLEL(G.max_procs = 1;) \
G.xargs_exitcode = 0; \
- IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
- IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
+ IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__state = NORM;) \
+ IF_FEATURE_XARGS_SUPPORT_QUOTES(G.process_stdin__q = '\0';) \
} while (0)
@@ -257,12 +268,8 @@ static void store_param(char *s)
#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
{
-#define NORM 0
-#define QUOTE 1
-#define BACKSLASH 2
-#define SPACE 4
- char q = '\0'; /* quote char */
- char state = NORM;
+#define q G.process_stdin__q
+#define state G.process_stdin__state
char *s = buf; /* start of the word */
char *p = s + strlen(buf); /* end of the word */
@@ -339,6 +346,8 @@ static char* FAST_FUNC process_stdin(int n_max_chars, int n_max_arg, char *buf)
/* store_param(NULL) - caller will do it */
dbg_msg("return:'%s'", s);
return s;
+#undef q
+#undef state
}
#else
/* The variant does not support single quotes, double quotes or backslash */