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: some variables should be read-only



details:   https://anonhg.NetBSD.org/src/rev/1f92e95e079b
branches:  trunk
changeset: 373242:1f92e95e079b
user:      sjg <sjg%NetBSD.org@localhost>
date:      Thu Jan 26 20:48:17 2023 +0000

description:
make: some variables should be read-only

Make variables like .newline and .MAKE.{GID,PID,PPID,UID} read-only.

Reviewed by: rillig

diffstat:

 usr.bin/make/main.c                             |  16 ++++++++--------
 usr.bin/make/make.1                             |  10 ++++++++--
 usr.bin/make/make.h                             |   3 ++-
 usr.bin/make/unit-tests/varname-dot-newline.exp |   1 -
 usr.bin/make/unit-tests/varname-dot-newline.mk  |  15 ++++-----------
 usr.bin/make/var.c                              |  10 ++++++++--
 6 files changed, 30 insertions(+), 25 deletions(-)

diffs (206 lines):

diff -r 55834c00f596 -r 1f92e95e079b usr.bin/make/main.c
--- a/usr.bin/make/main.c       Thu Jan 26 17:16:57 2023 +0000
+++ b/usr.bin/make/main.c       Thu Jan 26 20:48:17 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.588 2023/01/24 00:24:02 sjg Exp $   */
+/*     $NetBSD: main.c,v 1.589 2023/01/26 20:48:17 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*     "@(#)main.c     8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: main.c,v 1.588 2023/01/24 00:24:02 sjg Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.589 2023/01/26 20:48:17 sjg Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
            "The Regents of the University of California.  "
@@ -1346,13 +1346,13 @@
         */
        Targ_Init();
        Var_Init();
-       Global_Set(".MAKE.OS", utsname.sysname);
+       Global_Set_ReadOnly(".MAKE.OS", utsname.sysname);
        Global_Set("MACHINE", machine);
        Global_Set("MACHINE_ARCH", machine_arch);
 #ifdef MAKE_VERSION
        Global_Set("MAKE_VERSION", MAKE_VERSION);
 #endif
-       Global_Set(".newline", "\n");   /* handy for :@ loops */
+       Global_Set_ReadOnly(".newline", "\n");  /* handy for :@ loops */
 #ifndef MAKEFILE_PREFERENCE_LIST
        /* This is the traditional preference for makefiles. */
 # define MAKEFILE_PREFERENCE_LIST "makefile Makefile"
@@ -1398,13 +1398,13 @@
                snprintf(buf, sizeof buf, "%d", makelevel);
                Global_Set(MAKE_LEVEL, buf);
                snprintf(buf, sizeof buf, "%u", myPid);
-               Global_Set(".MAKE.PID", buf);
+               Global_Set_ReadOnly(".MAKE.PID", buf);
                snprintf(buf, sizeof buf, "%u", getppid());
-               Global_Set(".MAKE.PPID", buf);
+               Global_Set_ReadOnly(".MAKE.PPID", buf);
                snprintf(buf, sizeof buf, "%u", getuid());
-               Global_Set(".MAKE.UID", buf);
+               Global_Set_ReadOnly(".MAKE.UID", buf);
                snprintf(buf, sizeof buf, "%u", getgid());
-               Global_Set(".MAKE.GID", buf);
+               Global_Set_ReadOnly(".MAKE.GID", buf);
        }
        if (makelevel > 0) {
                char pn[1024];
diff -r 55834c00f596 -r 1f92e95e079b usr.bin/make/make.1
--- a/usr.bin/make/make.1       Thu Jan 26 17:16:57 2023 +0000
+++ b/usr.bin/make/make.1       Thu Jan 26 20:48:17 2023 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: make.1,v 1.359 2023/01/24 00:24:02 sjg Exp $
+.\"    $NetBSD: make.1,v 1.360 2023/01/26 20:48:17 sjg Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    from: @(#)make.1        8.4 (Berkeley) 3/19/94
 .\"
-.Dd January 23, 2023
+.Dd January 26, 2023
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -940,6 +940,7 @@
 .It Va .MAKE.GID
 The numeric group ID of the user running
 .Nm .
+It is read-only.
 .It Va .MAKE.JOB.PREFIX
 If
 .Nm
@@ -1130,6 +1131,7 @@
 .It Va .MAKE.OS
 The name of the operating system, see
 .Xr uname 1 .
+It is read-only.
 .It Va .MAKEOVERRIDES
 This variable is used to record the names of variables assigned to
 on the command line, so that they may be exported as part of
@@ -1154,9 +1156,11 @@
 .It Va .MAKE.PID
 The process ID of
 .Nm .
+It is read-only.
 .It Va .MAKE.PPID
 The parent process ID of
 .Nm .
+It is read-only.
 .It Va MAKE_PRINT_VAR_ON_ERROR
 When
 .Nm
@@ -1198,6 +1202,7 @@
 .It Va .MAKE.UID
 The numeric ID of the user running
 .Nm .
+It is read-only.
 .\" 'MAKE_VERSION' is intentionally undocumented
 .\" since it is only defined in the bmake distribution,
 .\" but not in NetBSD's native make.
@@ -1209,6 +1214,7 @@
 .\" since it is obsolete.
 .It Va .newline
 This variable is simply assigned a newline character as its value.
+It is read-only.
 This allows expansions using the
 .Cm \&:@
 modifier to put a newline between
diff -r 55834c00f596 -r 1f92e95e079b usr.bin/make/make.h
--- a/usr.bin/make/make.h       Thu Jan 26 17:16:57 2023 +0000
+++ b/usr.bin/make/make.h       Thu Jan 26 20:48:17 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: make.h,v 1.310 2023/01/23 23:01:52 sjg Exp $   */
+/*     $NetBSD: make.h,v 1.311 2023/01/26 20:48:17 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -1031,6 +1031,7 @@
 void Global_Set(const char *, const char *);
 void Global_Append(const char *, const char *);
 void Global_Delete(const char *);
+void Global_Set_ReadOnly(const char *, const char *);
 
 /* util.c */
 typedef void (*SignalProc)(int);
diff -r 55834c00f596 -r 1f92e95e079b usr.bin/make/unit-tests/varname-dot-newline.exp
--- a/usr.bin/make/unit-tests/varname-dot-newline.exp   Thu Jan 26 17:16:57 2023 +0000
+++ b/usr.bin/make/unit-tests/varname-dot-newline.exp   Thu Jan 26 20:48:17 2023 +0000
@@ -1,4 +1,3 @@
-make: "varname-dot-newline.mk" line 30: The .newline variable can be overwritten.  Just don't do that.
 first
 second
 backslash newline: <\
diff -r 55834c00f596 -r 1f92e95e079b usr.bin/make/unit-tests/varname-dot-newline.mk
--- a/usr.bin/make/unit-tests/varname-dot-newline.mk    Thu Jan 26 17:16:57 2023 +0000
+++ b/usr.bin/make/unit-tests/varname-dot-newline.mk    Thu Jan 26 20:48:17 2023 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: varname-dot-newline.mk,v 1.5 2023/01/17 19:42:47 rillig Exp $
+# $NetBSD: varname-dot-newline.mk,v 1.6 2023/01/26 20:48:18 sjg Exp $
 #
 # Tests for the special .newline variable, which contains a single newline
 # character (U+000A).
@@ -16,23 +16,16 @@
 BACKSLASH_NEWLINE:=    \${.newline}
 
 
-# Contrary to the special variable named "" that is used in expressions like
-# ${:Usome-value}, the variable ".newline" is not protected against
-# modification.  Nobody exploits that though.
+# Check that .newline is read-only
 
 NEWLINE:=      ${.newline}
 
 .newline=      overwritten
 
-.if ${.newline} == ${NEWLINE}
-.  info The .newline variable cannot be overwritten.  Good.
-.else
-.  info The .newline variable can be overwritten.  Just don't do that.
+.if ${.newline} != ${NEWLINE}
+.  error The .newline variable can be overwritten.  It should be read-only.
 .endif
 
-# Restore the original value.
-.newline=      ${NEWLINE}
-
 all:
        @echo 'first${.newline}second'
        @echo 'backslash newline: <${BACKSLASH_NEWLINE}>'
diff -r 55834c00f596 -r 1f92e95e079b usr.bin/make/var.c
--- a/usr.bin/make/var.c        Thu Jan 26 17:16:57 2023 +0000
+++ b/usr.bin/make/var.c        Thu Jan 26 20:48:17 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.1038 2023/01/24 00:19:14 sjg Exp $   */
+/*     $NetBSD: var.c,v 1.1039 2023/01/26 20:48:17 sjg Exp $   */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -139,7 +139,7 @@
 #include "metachar.h"
 
 /*     "@(#)var.c      8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.1038 2023/01/24 00:19:14 sjg Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.1039 2023/01/26 20:48:17 sjg Exp $");
 
 /*
  * Variables are defined using one of the VAR=value assignments.  Their
@@ -1078,6 +1078,12 @@
        Var_Delete(SCOPE_GLOBAL, name);
 }
 
+void
+Global_Set_ReadOnly(const char *name, const char *value)
+{
+       Var_SetWithFlags(SCOPE_GLOBAL, name, value, VAR_SET_READONLY);
+}
+
 /*
  * Append the value to the named variable.
  *



Home | Main Index | Thread Index | Old Index