From ada909acfa49fa59bbd8005e5b118c32447dfad0 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 31 Mar 2018 10:23:59 +0100 Subject: ash: revise handling of builtin environment variables during fork Commit db03ff5ea made some improvements to the handling of builtin environment variables during forkshell. However, more can be done. Currently the calculated size of varinit doesn't match what's actually copied. It overestimates the amount of string space and the number of pointers. The text of each builtin environment variable is also copied twice: once as part of vartab and again from varinit. Remove the code to copy varinit via the forkshell block. Instead use information from vartab and varinit_data to reinitialise varinit and fix the links in vartab. In addition, ensure that the LINENO variable is properly initialised in the child. --- shell/ash.c | 56 ++++++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/shell/ash.c b/shell/ash.c index 2d51b48f9..57f87f0e1 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -14946,27 +14946,31 @@ forkshell_child(struct forkshell *fs) } /* - * Reset the pointers to the builtin environment variables in the hash - * table to point to varinit rather than the bogus copy created during - * forkshell_prepare. + * Reinitialise the builtin environment variables in varinit. Their + * current settings have been copied from the parent in vartab. Look + * these up using the names from varinit_data, copy the details from + * vartab to varinit and replace the old copy in vartab with the new + * one in varinit. + * + * Also reinitialise the function pointers and line number variable. */ static void reinitvar(void) { - struct var *vp; - struct var *end; - struct var **vpp; - struct var **old; + int i; + const char *name; + struct var **vpp, **old; - vp = varinit; - end = vp + ARRAY_SIZE(varinit); - do { - vpp = hashvar(vp->var_text); - if ( (old=findvar(vpp, vp->var_text)) != NULL ) { - vp->next = (*old)->next; - *old = vp; + for (i=0; i