From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 1905 invoked by alias); 8 Jul 2014 01:08:06 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 32853 Received: (qmail 4211 invoked from network); 8 Jul 2014 01:08:00 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 From: Bart Schaefer Message-id: <140707180806.ZM20805@torch.brasslantern.com> Date: Mon, 07 Jul 2014 18:08:06 -0700 In-reply-to: <20140707203312.27566ab8@pws-pc.ntlworld.com> Comments: In reply to Peter Stephenson "Re: segmentation fault with {1..1234567}" (Jul 7, 8:33pm) References: <20140704172545.GA29213@xvii.vinc17.org> <140704184036.ZM18558@torch.brasslantern.com> <20140705111233.GA19385@xvii.vinc17.org> <140705095703.ZM12012@torch.brasslantern.com> <20140705233916.GA18368@xvii.vinc17.org> <140706091609.ZM18865@torch.brasslantern.com> <20140706193055.209f7a2b@pws-pc.ntlworld.com> <140706124629.ZM19578@torch.brasslantern.com> <20140707203312.27566ab8@pws-pc.ntlworld.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-workers@zsh.org Subject: Re: segmentation fault with {1..1234567} MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Jul 7, 8:33pm, Peter Stephenson wrote: } Subject: Re: segmentation fault with {1..1234567} } } On Sun, 06 Jul 2014 12:46:29 -0700 } Bart Schaefer wrote: } > The question is, do we fix this one instance (your patch), do we redefine } > VARARR() [possibly conditionally] to change all the usages at once, or do } > we individually evaluate the 50-odd uses of VARARR()? } } Probably the third option is the right one, but it's the most work. The } other possibilities are the pragmatic option of only fixing things when } they turn out to be buggy, or changing everything to heap allocation (or } something else known not to be problematic). The best argument for the } last one is that it removes quite a lot of variant behaviour between } systems --- even if using zhalloc() isn't particularly efficient we can } all see whatever effects there are and are in a better position to fix } them (e.g. with appropriate heap manipulation commands). For that } reason, I'm vaguely wondering if trying out that might not be a } reasonable start. Well, that one is easy, as I mentioned earlier; see patch below. One added hunk in mem.c for a place where we didn't check the return of malloc, but I did not attempt to change the usual fatal-error behavior. diff --git a/Src/mem.c b/Src/mem.c index a8f0c37..7e0667a 100644 --- a/Src/mem.c +++ b/Src/mem.c @@ -950,7 +950,10 @@ zrealloc(void *ptr, size_t size) ptr = NULL; } else { /* If ptr is NULL, then behave like malloc */ - ptr = malloc(size); + if (!(ptr = (void *) malloc(size))) { + zerr("fatal error: out of memory"); + exit(1); + } } unqueue_signals(); diff --git a/Src/zsh_system.h b/Src/zsh_system.h index 601de69..811340d 100644 --- a/Src/zsh_system.h +++ b/Src/zsh_system.h @@ -286,11 +286,15 @@ struct timezone { # include #endif +#ifdef USE_STACK_ALLOCATION #ifdef HAVE_VARIABLE_LENGTH_ARRAYS # define VARARR(X,Y,Z) X (Y)[Z] #else # define VARARR(X,Y,Z) X *(Y) = (X *) alloca(sizeof(X) * (Z)) #endif +#else +# define VARARR(X,Y,Z) X *(Y) = (X *) zhalloc(sizeof(X) * (Z)) +#endif /* we should handle unlimited sizes from pathconf(_PC_PATH_MAX) */ /* but this is too much trouble */ diff --git a/configure.ac b/configure.ac index 0f87a6c..37f3585 100644 --- a/configure.ac +++ b/configure.ac @@ -140,6 +140,16 @@ AC_HELP_STRING([--enable-zsh-hash-debug], [turn on debugging of internal hash ta AC_DEFINE(ZSH_HASH_DEBUG) fi]) +dnl Do you want to dynamically allocate memory on the stack where possible? +ifdef([stack-allocation],[undefine([stack-allocation])])dnl +AH_TEMPLATE([USE_STACK_ALLOCATION], +[Define to 1 if you want to allocate stack memory e.g. with `alloca'.]) +AC_ARG_ENABLE(stack-allocation, +AC_HELP_STRING([--enable-stack-allocation], [allocate stack memory e.g. with `alloca']), +[if test x$enableval = xyes; then + AC_DEFINE(USE_STACK_ALLOCATION) +fi]) + dnl Pathnames for global zsh scripts ifdef([etcdir],[undefine([etcdir])])dnl AC_ARG_ENABLE(etcdir,