Cosmetic changes to variable flags - make their values more suited

to my delicate sensibilities...  (NFC).

Arrange not to barf (ever) if some turkey makes _ readonly.  Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)

From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd.  Free it instead...
This commit is contained in:
kre 2017-06-17 10:46:34 +00:00
parent dea541e9f3
commit ad0b72d9e9
3 changed files with 32 additions and 18 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: eval.c,v 1.146 2017/06/08 13:12:17 kre Exp $ */
/* $NetBSD: eval.c,v 1.147 2017/06/17 10:46:34 kre Exp $ */
/*-
* Copyright (c) 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
#else
__RCSID("$NetBSD: eval.c,v 1.146 2017/06/08 13:12:17 kre Exp $");
__RCSID("$NetBSD: eval.c,v 1.147 2017/06/17 10:46:34 kre Exp $");
#endif
#endif /* not lint */
@ -1195,7 +1195,7 @@ parent: /* parent process gets here (if we forked) */
out:
if (lastarg)
/* implement $_ for whatever use that really is */
setvar("_", lastarg, 0);
(void) setvarsafe("_", lastarg, VNOERROR);
popstackmark(&smark);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.c,v 1.59 2017/06/17 04:12:06 kre Exp $ */
/* $NetBSD: var.c,v 1.60 2017/06/17 10:46:34 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
#else
__RCSID("$NetBSD: var.c,v 1.59 2017/06/17 04:12:06 kre Exp $");
__RCSID("$NetBSD: var.c,v 1.60 2017/06/17 10:46:34 kre Exp $");
#endif
#endif /* not lint */
@ -353,10 +353,18 @@ setvareq(char *s, int flags)
flags |= VEXPORT;
vp = find_var(s, &vpp, &nlen);
if (vp != NULL) {
if (vp->flags & VREADONLY)
if (vp->flags & VREADONLY) {
if ((flags & (VTEXTFIXED|VSTACK)) == 0)
ckfree(s);
if (flags & VNOERROR)
return;
error("%.*s: is read only", vp->name_len, s);
if (flags & VNOSET)
}
if (flags & VNOSET) {
if ((flags & (VTEXTFIXED|VSTACK)) == 0)
ckfree(s);
return;
}
INTOFF;
@ -385,8 +393,11 @@ setvareq(char *s, int flags)
return;
}
/* not found */
if (flags & VNOSET)
if (flags & VNOSET) {
if ((flags & (VTEXTFIXED|VSTACK)) == 0)
ckfree(s);
return;
}
vp = ckmalloc(sizeof (*vp));
vp->flags = flags & ~(VNOFUNC|VFUNCREF);
vp->text = s;

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.h,v 1.30 2017/06/07 05:08:32 kre Exp $ */
/* $NetBSD: var.h,v 1.31 2017/06/17 10:46:34 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@ -39,17 +39,20 @@
*/
/* flags */
#define VEXPORT 0x0001 /* variable is exported */
#define VREADONLY 0x0002 /* variable cannot be modified */
#define VSTRFIXED 0x0004 /* variable struct is statically allocated */
#define VTEXTFIXED 0x0008 /* text is statically allocated */
#define VSTACK 0x0010 /* text is allocated on the stack */
#define VUNSET 0x0020 /* the variable is not set */
#define VNOFUNC 0x0040 /* don't call the callback function */
#define VNOSET 0x0080 /* do not set variable - just readonly test */
#define VNOEXPORT 0x0100 /* variable may not be exported */
#define VUNSET 0x0001 /* the variable is not set */
#define VEXPORT 0x0002 /* variable is exported */
#define VREADONLY 0x0004 /* variable cannot be modified */
#define VNOEXPORT 0x0008 /* variable may not be exported */
#define VSTRFIXED 0x0010 /* variable struct is statically allocated */
#define VTEXTFIXED 0x0020 /* text is statically allocated */
#define VSTACK 0x0040 /* text is allocated on the stack */
#define VNOFUNC 0x0100 /* don't call the callback function */
#define VFUNCREF 0x0200 /* the function is called on ref, not set */
#define VNOSET 0x4000 /* do not set variable - just readonly test */
#define VNOERROR 0x8000 /* be quiet if set fails (no error msg) */
struct var;
union var_func_union { /* function to be called when: */