Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh DTRT when dynamically generated variables return "uns...



details:   https://anonhg.NetBSD.org/src/rev/c740d440ae1b
branches:  trunk
changeset: 448803:c740d440ae1b
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Feb 09 09:38:11 2019 +0000

description:
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.

diffstat:

 bin/sh/var.c |  53 ++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 36 insertions(+), 17 deletions(-)

diffs (110 lines):

diff -r fd6218a89ec0 -r c740d440ae1b bin/sh/var.c
--- a/bin/sh/var.c      Sat Feb 09 09:34:43 2019 +0000
+++ b/bin/sh/var.c      Sat Feb 09 09:38:11 2019 +0000
@@ -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 @@
 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 @@
 {
        struct strlist *sp;
        struct var *v;
+       char *p;
 
        for (sp = cmdenviron ; sp ; sp = sp->next) {
                if (strequal(sp->text, name))
@@ -594,9 +601,15 @@
 
        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 @@
        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 @@
 {
        const char *p;
 
+       p = vp->text;
+       if (vp->rfunc && (vp->flags & VFUNCREF) != 0) {
+               p = (*vp->rfunc)(vp);
+               if (p == NULL) {
+                       if (!(show_value & 2))
+                               return;
+                       p = vp->text;
+                       show_value = 0;
+               }
+       }
        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)
-                       p = vp->text;
-       }
        for ( ; *p != '=' ; p++)
                out1c(*p);
        if (!(vp->flags & VUNSET) && show_value) {



Home | Main Index | Thread Index | Old Index