>From 98fb1642f4b809f4984390871b982cc37155d9ed Mon Sep 17 00:00:00 2001 From: Kamil Dudka Date: Tue, 18 Apr 2017 15:03:55 +0200 Subject: [PATCH] execcmd_exec: reduce stack allocation in favour of heap --- Src/exec.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Src/exec.c b/Src/exec.c index cde549e..b28db61 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -2654,9 +2654,9 @@ execcmd_exec(Estate state, Execcmd_params eparams, LinkList filelist = NULL; LinkNode node; Redir fn; - struct multio *mfds[10]; + struct multio **mfds; char *text; - int save[10]; + int *save; int fil, dfil, is_cursh, do_exec = 0, redir_err = 0, i; int nullexec = 0, magic_assign = 0, forked = 0; int is_shfunc = 0, is_builtin = 0, is_exec = 0, use_defpath = 0; @@ -2689,11 +2689,6 @@ execcmd_exec(Estate state, Execcmd_params eparams, */ use_cmdoutval = !args; - for (i = 0; i < 10; i++) { - save[i] = -2; - mfds[i] = NULL; - } - /* If the command begins with `%', then assume it is a * * reference to a job in the job table. */ if ((type == WC_SIMPLE || type == WC_TYPESET) && args && nonempty(args) && @@ -3370,6 +3365,13 @@ execcmd_exec(Estate state, Execcmd_params eparams, } } + save = zalloc(10 * sizeof(int)); + mfds = zalloc(10 * sizeof(struct multio *)); + for (i = 0; i < 10; i++) { + save[i] = -2; + mfds[i] = NULL; + } + /* Add pipeline input/output to mnodes */ if (input) addfd(forked, save, mfds, 0, input, 0, NULL); @@ -3616,6 +3618,8 @@ execcmd_exec(Estate state, Execcmd_params eparams, if (mfds[i] && mfds[i]->ct >= 2) closemn(mfds, i, REDIR_CLOSE); + zfree(mfds, 10 * sizeof(struct multio *)); + if (nullexec) { /* * If nullexec is 2, we have variables to add with the redirections @@ -4003,6 +4007,7 @@ execcmd_exec(Estate state, Execcmd_params eparams, fixfds(save); done: + zfree(save, 10 * sizeof(int)); if (isset(POSIXBUILTINS) && (cflags & (BINF_PSPECIAL|BINF_EXEC)) && !(orig_cflags & BINF_COMMAND)) { -- 2.10.2