Module Name:    src
Committed By:   kre
Date:           Sat Feb  9 09:38:11 UTC 2019

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

Log Message:
DTRT when dynamically generated variables return "unset" instead of
a value.   There are none which do that at the minute, so this is a NFCI
change, which is just making the code correct even though nothing
currently triggers any bugs.


To generate a diff of this commit:
cvs rdiff -u -r1.76 -r1.77 src/bin/sh/var.c

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/var.c
diff -u src/bin/sh/var.c:1.76 src/bin/sh/var.c:1.77
--- src/bin/sh/var.c:1.76	Sat Feb  9 03:35:55 2019
+++ src/bin/sh/var.c	Sat Feb  9 09:38:11 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.76 2019/02/09 03:35:55 kre Exp $	*/
+/*	$NetBSD: var.c,v 1.77 2019/02/09 09:38:11 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.76 2019/02/09 03:35:55 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.77 2019/02/09 09:38:11 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -562,13 +562,19 @@ char *
 lookupvar(const char *name)
 {
 	struct var *v;
+	char *p;
 
 	v = find_var(name, NULL, NULL);
 	if (v == NULL || v->flags & VUNSET)
 		return NULL;
-	if (v->rfunc && (v->flags & VFUNCREF) != 0)
-		return (*v->rfunc)(v) + v->name_len + 1;
-	return v->text + v->name_len + 1;
+	if (v->rfunc && (v->flags & VFUNCREF) != 0) {
+		p = (*v->rfunc)(v);
+		if (p == NULL)
+			return NULL;
+	} else
+		p = v->text;
+
+	return p + v->name_len + 1;
 }
 
 
@@ -584,6 +590,7 @@ bltinlookup(const char *name, int doall)
 {
 	struct strlist *sp;
 	struct var *v;
+	char *p;
 
 	for (sp = cmdenviron ; sp ; sp = sp->next) {
 		if (strequal(sp->text, name))
@@ -594,9 +601,15 @@ bltinlookup(const char *name, int doall)
 
 	if (v == NULL || v->flags & VUNSET || (!doall && !(v->flags & VEXPORT)))
 		return NULL;
-	if (v->rfunc && (v->flags & VFUNCREF) != 0)
-		return (*v->rfunc)(v) + v->name_len + 1;
-	return v->text + v->name_len + 1;
+
+	if (v->rfunc && (v->flags & VFUNCREF) != 0) {
+		p = (*v->rfunc)(v);
+		if (p == NULL)
+			return NULL;
+	} else
+		p = v->text;
+
+	return p + v->name_len + 1;
 }
 
 
@@ -626,9 +639,11 @@ environment(void)
 	for (vpp = vartab ; vpp < vartab + VTABSIZE ; vpp++) {
 		for (vp = *vpp ; vp ; vp = vp->next)
 			if ((vp->flags & (VEXPORT|VUNSET)) == VEXPORT) {
-				if (vp->rfunc && (vp->flags & VFUNCREF))
-					*ep++ = (*vp->rfunc)(vp);
-				else
+				if (vp->rfunc && (vp->flags & VFUNCREF)) {
+					*ep = (*vp->rfunc)(vp);
+					if (*ep != NULL)
+						ep++;
+				} else
 					*ep++ = vp->text;
 				VTRACE(DBG_VARS, ("environment: %s\n", ep[-1]));
 			}
@@ -763,16 +778,20 @@ showvar(struct var *vp, const char *cmd,
 {
 	const char *p;
 
-	if (cmd)
-		out1fmt("%s ", cmd);
-	if (xtra)
-		out1fmt("%s ", xtra);
 	p = vp->text;
 	if (vp->rfunc && (vp->flags & VFUNCREF) != 0) {
 		p = (*vp->rfunc)(vp);
-		if (p == NULL)
+		if (p == NULL) {
+			if (!(show_value & 2))
+				return;
 			p = vp->text;
+			show_value = 0;
+		}
 	}
+	if (cmd)
+		out1fmt("%s ", cmd);
+	if (xtra)
+		out1fmt("%s ", xtra);
 	for ( ; *p != '=' ; p++)
 		out1c(*p);
 	if (!(vp->flags & VUNSET) && show_value) {

Reply via email to