Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/make make(1): extract ExportVars from Var_Export



details:   https://anonhg.NetBSD.org/src/rev/46d89ba3c528
branches:  trunk
changeset: 957845:46d89ba3c528
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Dec 12 19:31:17 2020 +0000

description:
make(1): extract ExportVars from Var_Export

diffstat:

 usr.bin/make/unit-tests/directive-export.exp |   2 +-
 usr.bin/make/unit-tests/directive-export.mk  |   6 ++-
 usr.bin/make/var.c                           |  50 +++++++++++++++------------
 3 files changed, 34 insertions(+), 24 deletions(-)

diffs (115 lines):

diff -r 95fb063725c4 -r 46d89ba3c528 usr.bin/make/unit-tests/directive-export.exp
--- a/usr.bin/make/unit-tests/directive-export.exp      Sat Dec 12 19:13:47 2020 +0000
+++ b/usr.bin/make/unit-tests/directive-export.exp      Sat Dec 12 19:31:17 2020 +0000
@@ -1,4 +1,4 @@
-make: "directive-export.mk" line 25: Unknown directive "expor"
+make: "directive-export.mk" line 29: Unknown directive "expor"
 make: Fatal errors encountered -- cannot continue
 make: stopped in unit-tests
 exit status 1
diff -r 95fb063725c4 -r 46d89ba3c528 usr.bin/make/unit-tests/directive-export.mk
--- a/usr.bin/make/unit-tests/directive-export.mk       Sat Dec 12 19:13:47 2020 +0000
+++ b/usr.bin/make/unit-tests/directive-export.mk       Sat Dec 12 19:31:17 2020 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: directive-export.mk,v 1.4 2020/11/03 17:17:31 rillig Exp $
+# $NetBSD: directive-export.mk,v 1.5 2020/12/12 19:31:18 rillig Exp $
 #
 # Tests for the .export directive.
 
@@ -7,6 +7,10 @@
 INDIRECT=      indirect
 VAR=           value $$ ${INDIRECT}
 
+# Before 2020-12-13, this unusual expression invoked undefined behavior since
+# it accessed out-of-bounds memory via Var_Export -> ExportVar -> MayExport.
+.export ${:U }
+
 # A variable is exported using the .export directive.
 # During that, its value is expanded, just like almost everywhere else.
 .export VAR
diff -r 95fb063725c4 -r 46d89ba3c528 usr.bin/make/var.c
--- a/usr.bin/make/var.c        Sat Dec 12 19:13:47 2020 +0000
+++ b/usr.bin/make/var.c        Sat Dec 12 19:31:17 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.725 2020/12/12 18:53:53 rillig Exp $ */
+/*     $NetBSD: var.c,v 1.726 2020/12/12 19:31:17 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.725 2020/12/12 18:53:53 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.726 2020/12/12 19:31:17 rillig Exp $");
 
 /* A string that may need to be freed after use. */
 typedef struct FStr {
@@ -671,6 +671,28 @@
        free(val);
 }
 
+static void
+ExportVars(const char *varnames, Boolean isExport, VarExportFlags flags)
+{
+       if (varnames[0] != '\0') {
+               Words words = Str_Words(varnames, FALSE);
+
+               size_t i;
+               for (i = 0; i < words.len; i++) {
+                       const char *name = words.words[i];
+                       if (ExportVar(name, flags)) {
+                               if (var_exportedVars == VAR_EXPORTED_NONE)
+                                       var_exportedVars = VAR_EXPORTED_SOME;
+                               if (isExport && (flags & VAR_EXPORT_PARENT)) {
+                                       Var_Append(MAKE_EXPORTED, name,
+                                           VAR_GLOBAL);
+                               }
+                       }
+               }
+               Words_Free(words);
+       }
+}
+
 /*
  * This is called when .export is seen or .MAKE.EXPORTED is modified.
  *
@@ -683,7 +705,7 @@
 Var_Export(const char *str, Boolean isExport)
 {
        VarExportFlags flags;
-       char *val;
+       char *varnames;
 
        if (isExport && str[0] == '\0') {
                var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
@@ -700,26 +722,10 @@
                flags = VAR_EXPORT_PARENT;
        }
 
-       (void)Var_Subst(str, VAR_GLOBAL, VARE_WANTRES, &val);
+       (void)Var_Subst(str, VAR_GLOBAL, VARE_WANTRES, &varnames);
        /* TODO: handle errors */
-       if (val[0] != '\0') {
-               Words words = Str_Words(val, FALSE);
-
-               size_t i;
-               for (i = 0; i < words.len; i++) {
-                       const char *name = words.words[i];
-                       if (ExportVar(name, flags)) {
-                               if (var_exportedVars == VAR_EXPORTED_NONE)
-                                       var_exportedVars = VAR_EXPORTED_SOME;
-                               if (isExport && (flags & VAR_EXPORT_PARENT)) {
-                                       Var_Append(MAKE_EXPORTED, name,
-                                           VAR_GLOBAL);
-                               }
-                       }
-               }
-               Words_Free(words);
-       }
-       free(val);
+       ExportVars(varnames, isExport, flags);
+       free(varnames);
 }
 
 



Home | Main Index | Thread Index | Old Index