Module Name:    src
Committed By:   kre
Date:           Sat Jun 17 10:46:34 UTC 2017

Modified Files:
        src/bin/sh: eval.c var.c var.h

Log Message:
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...


To generate a diff of this commit:
cvs rdiff -u -r1.146 -r1.147 src/bin/sh/eval.c
cvs rdiff -u -r1.59 -r1.60 src/bin/sh/var.c
cvs rdiff -u -r1.30 -r1.31 src/bin/sh/var.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/eval.c
diff -u src/bin/sh/eval.c:1.146 src/bin/sh/eval.c:1.147
--- src/bin/sh/eval.c:1.146	Thu Jun  8 13:12:17 2017
+++ src/bin/sh/eval.c	Sat Jun 17 10:46:34 2017
@@ -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 
 out:
 	if (lastarg)
 		/* implement $_ for whatever use that really is */
-		setvar("_", lastarg, 0);
+		(void) setvarsafe("_", lastarg, VNOERROR);
 	popstackmark(&smark);
 }
 

Index: src/bin/sh/var.c
diff -u src/bin/sh/var.c:1.59 src/bin/sh/var.c:1.60
--- src/bin/sh/var.c:1.59	Sat Jun 17 04:12:06 2017
+++ src/bin/sh/var.c	Sat Jun 17 10:46:34 2017
@@ -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;

Index: src/bin/sh/var.h
diff -u src/bin/sh/var.h:1.30 src/bin/sh/var.h:1.31
--- src/bin/sh/var.h:1.30	Wed Jun  7 05:08:32 2017
+++ src/bin/sh/var.h	Sat Jun 17 10:46:34 2017
@@ -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:  */

Reply via email to