Module Name:    src
Committed By:   sjg
Date:           Thu Jan 26 20:48:18 UTC 2023

Modified Files:
        src/usr.bin/make: main.c make.1 make.h var.c
        src/usr.bin/make/unit-tests: varname-dot-newline.exp
            varname-dot-newline.mk

Log Message:
make: some variables should be read-only

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

Reviewed by: rillig


To generate a diff of this commit:
cvs rdiff -u -r1.588 -r1.589 src/usr.bin/make/main.c
cvs rdiff -u -r1.359 -r1.360 src/usr.bin/make/make.1
cvs rdiff -u -r1.310 -r1.311 src/usr.bin/make/make.h
cvs rdiff -u -r1.1038 -r1.1039 src/usr.bin/make/var.c
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/varname-dot-newline.exp
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/make/unit-tests/varname-dot-newline.mk

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

Modified files:

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.588 src/usr.bin/make/main.c:1.589
--- src/usr.bin/make/main.c:1.588	Tue Jan 24 00:24:02 2023
+++ src/usr.bin/make/main.c	Thu Jan 26 20:48:17 2023
@@ -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 @@ main_Init(int argc, char **argv)
 	 */
 	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 @@ main_Init(int argc, char **argv)
 		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];

Index: src/usr.bin/make/make.1
diff -u src/usr.bin/make/make.1:1.359 src/usr.bin/make/make.1:1.360
--- src/usr.bin/make/make.1:1.359	Tue Jan 24 00:24:02 2023
+++ src/usr.bin/make/make.1	Thu Jan 26 20:48:17 2023
@@ -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 @@ executes.
 .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 @@ Used to create files in a separate direc
 .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 @@ This allows makefiles to test for this s
 .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 @@ treated as normal sources.
 .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 @@ The numeric ID of the user running
 .\" 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

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.310 src/usr.bin/make/make.h:1.311
--- src/usr.bin/make/make.h:1.310	Mon Jan 23 23:01:52 2023
+++ src/usr.bin/make/make.h	Thu Jan 26 20:48:17 2023
@@ -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 Var_ReadOnly(const char *, bool);
 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);

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.1038 src/usr.bin/make/var.c:1.1039
--- src/usr.bin/make/var.c:1.1038	Tue Jan 24 00:19:14 2023
+++ src/usr.bin/make/var.c	Thu Jan 26 20:48:17 2023
@@ -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 @@ Global_Delete(const char *name)
 	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.
  *

Index: src/usr.bin/make/unit-tests/varname-dot-newline.exp
diff -u src/usr.bin/make/unit-tests/varname-dot-newline.exp:1.3 src/usr.bin/make/unit-tests/varname-dot-newline.exp:1.4
--- src/usr.bin/make/unit-tests/varname-dot-newline.exp:1.3	Tue Jan 17 19:42:47 2023
+++ src/usr.bin/make/unit-tests/varname-dot-newline.exp	Thu Jan 26 20:48:18 2023
@@ -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: <\

Index: src/usr.bin/make/unit-tests/varname-dot-newline.mk
diff -u src/usr.bin/make/unit-tests/varname-dot-newline.mk:1.5 src/usr.bin/make/unit-tests/varname-dot-newline.mk:1.6
--- src/usr.bin/make/unit-tests/varname-dot-newline.mk:1.5	Tue Jan 17 19:42:47 2023
+++ src/usr.bin/make/unit-tests/varname-dot-newline.mk	Thu Jan 26 20:48:18 2023
@@ -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}>'

Reply via email to