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:
parent
dea541e9f3
commit
ad0b72d9e9
|
@ -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
|
* Copyright (c) 1993
|
||||||
|
@ -37,7 +37,7 @@
|
||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
|
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
|
||||||
#else
|
#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
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
|
@ -1195,7 +1195,7 @@ parent: /* parent process gets here (if we forked) */
|
||||||
out:
|
out:
|
||||||
if (lastarg)
|
if (lastarg)
|
||||||
/* implement $_ for whatever use that really is */
|
/* implement $_ for whatever use that really is */
|
||||||
setvar("_", lastarg, 0);
|
(void) setvarsafe("_", lastarg, VNOERROR);
|
||||||
popstackmark(&smark);
|
popstackmark(&smark);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
21
bin/sh/var.c
21
bin/sh/var.c
|
@ -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
|
* Copyright (c) 1991, 1993
|
||||||
|
@ -37,7 +37,7 @@
|
||||||
#if 0
|
#if 0
|
||||||
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
|
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 5/4/95";
|
||||||
#else
|
#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
|
||||||
#endif /* not lint */
|
#endif /* not lint */
|
||||||
|
|
||||||
|
@ -353,10 +353,18 @@ setvareq(char *s, int flags)
|
||||||
flags |= VEXPORT;
|
flags |= VEXPORT;
|
||||||
vp = find_var(s, &vpp, &nlen);
|
vp = find_var(s, &vpp, &nlen);
|
||||||
if (vp != NULL) {
|
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);
|
error("%.*s: is read only", vp->name_len, s);
|
||||||
if (flags & VNOSET)
|
}
|
||||||
|
if (flags & VNOSET) {
|
||||||
|
if ((flags & (VTEXTFIXED|VSTACK)) == 0)
|
||||||
|
ckfree(s);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
INTOFF;
|
INTOFF;
|
||||||
|
|
||||||
|
@ -385,8 +393,11 @@ setvareq(char *s, int flags)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* not found */
|
/* not found */
|
||||||
if (flags & VNOSET)
|
if (flags & VNOSET) {
|
||||||
|
if ((flags & (VTEXTFIXED|VSTACK)) == 0)
|
||||||
|
ckfree(s);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
vp = ckmalloc(sizeof (*vp));
|
vp = ckmalloc(sizeof (*vp));
|
||||||
vp->flags = flags & ~(VNOFUNC|VFUNCREF);
|
vp->flags = flags & ~(VNOFUNC|VFUNCREF);
|
||||||
vp->text = s;
|
vp->text = s;
|
||||||
|
|
23
bin/sh/var.h
23
bin/sh/var.h
|
@ -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
|
* Copyright (c) 1991, 1993
|
||||||
|
@ -39,17 +39,20 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* flags */
|
/* flags */
|
||||||
#define VEXPORT 0x0001 /* variable is exported */
|
#define VUNSET 0x0001 /* the variable is not set */
|
||||||
#define VREADONLY 0x0002 /* variable cannot be modified */
|
#define VEXPORT 0x0002 /* variable is exported */
|
||||||
#define VSTRFIXED 0x0004 /* variable struct is statically allocated */
|
#define VREADONLY 0x0004 /* variable cannot be modified */
|
||||||
#define VTEXTFIXED 0x0008 /* text is statically allocated */
|
#define VNOEXPORT 0x0008 /* variable may not be exported */
|
||||||
#define VSTACK 0x0010 /* text is allocated on the stack */
|
|
||||||
#define VUNSET 0x0020 /* the variable is not set */
|
#define VSTRFIXED 0x0010 /* variable struct is statically allocated */
|
||||||
#define VNOFUNC 0x0040 /* don't call the callback function */
|
#define VTEXTFIXED 0x0020 /* text is statically allocated */
|
||||||
#define VNOSET 0x0080 /* do not set variable - just readonly test */
|
#define VSTACK 0x0040 /* text is allocated on the stack */
|
||||||
#define VNOEXPORT 0x0100 /* variable may not be exported */
|
#define VNOFUNC 0x0100 /* don't call the callback function */
|
||||||
#define VFUNCREF 0x0200 /* the function is called on ref, not set */
|
#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;
|
struct var;
|
||||||
|
|
||||||
union var_func_union { /* function to be called when: */
|
union var_func_union { /* function to be called when: */
|
||||||
|
|
Loading…
Reference in New Issue