Module Name:    src
Committed By:   rillig
Date:           Sat Oct 31 21:40:20 UTC 2020

Modified Files:
        src/usr.bin/make: var.c
        src/usr.bin/make/unit-tests: varmod-gmtime.exp varmod-localtime.exp

Log Message:
make(1): make parsing of the :gmtime and :localtime modifiers stricter

These variable modifiers accept an optional timestamp in seconds, to
select which date to print.  This feature is only used very rarely.  The
NetBSD build doesn't use it at all, and the FreeBSD build mainly uses
the plain modifiers :gmtime and :localtime, but not their optional
argument :gmtime=1500000000.

Therefore, this change is not going to affect many builds.  Those that
are indeed affected had been wrong all the time anyway.

At parse time, these errors stop the build, as intended.  After that,
when the actual shell commands of the targets are expanded and run,
these errors don't stop anything, the build just continues as if nothing
had happened.  This is a general problem with Var_Parse, see the many
"handle errors" markers in the code.  Another problem is that on parse
errors, parsing continues and spits out spurious strings of the form
"mtime" and "ocaltime".  This as well is a general problem with error
handling in make.

ok sjg


To generate a diff of this commit:
cvs rdiff -u -r1.630 -r1.631 src/usr.bin/make/var.c
cvs rdiff -u -r1.5 -r1.6 src/usr.bin/make/unit-tests/varmod-gmtime.exp
cvs rdiff -u -r1.3 -r1.4 src/usr.bin/make/unit-tests/varmod-localtime.exp

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/var.c
diff -u src/usr.bin/make/var.c:1.630 src/usr.bin/make/var.c:1.631
--- src/usr.bin/make/var.c:1.630	Sat Oct 31 18:41:07 2020
+++ src/usr.bin/make/var.c	Sat Oct 31 21:40:20 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.630 2020/10/31 18:41:07 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.631 2020/10/31 21:40:20 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -119,6 +119,7 @@
 #include <sys/types.h>
 #include <regex.h>
 #endif
+#include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
 #include <time.h>
@@ -129,7 +130,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.630 2020/10/31 18:41:07 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.631 2020/10/31 21:40:20 rillig Exp $");
 
 #define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
 #define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
@@ -2113,6 +2114,24 @@ ApplyModifier_Literal(const char **pp, A
     return AMR_OK;
 }
 
+static Boolean TryParseTime(const char **pp, time_t *out_time)
+{
+    char *end;
+    unsigned long n;
+
+    if (!ch_isdigit(**pp))
+	return FALSE;
+
+    errno = 0;
+    n = strtoul(*pp, &end, 10);
+    if (n == ULONG_MAX && errno == ERANGE)
+	return FALSE;
+
+    *pp = end;
+    *out_time = (time_t)n;	/* ignore possible truncation for now */
+    return TRUE;
+}
+
 /* :gmtime */
 static ApplyModifierResult
 ApplyModifier_Gmtime(const char **pp, ApplyModifiersState *st)
@@ -2124,9 +2143,12 @@ ApplyModifier_Gmtime(const char **pp, Ap
 	return AMR_UNKNOWN;
 
     if (mod[6] == '=') {
-	char *ep;
-	utc = (time_t)strtoul(mod + 7, &ep, 10);
-	*pp = ep;
+	const char *arg = mod + 7;
+	if (!TryParseTime(&arg, &utc)) {
+	    Parse_Error(PARSE_FATAL, "Invalid time value: %s\n", mod + 7);
+	    return AMR_CLEANUP;
+	}
+	*pp = arg;
     } else {
 	utc = 0;
 	*pp = mod + 6;
@@ -2146,9 +2168,12 @@ ApplyModifier_Localtime(const char **pp,
 	return AMR_UNKNOWN;
 
     if (mod[9] == '=') {
-	char *ep;
-	utc = (time_t)strtoul(mod + 10, &ep, 10);
-	*pp = ep;
+	const char *arg = mod + 10;
+	if (!TryParseTime(&arg, &utc)) {
+	    Parse_Error(PARSE_FATAL, "Invalid time value: %s\n", mod + 10);
+	    return AMR_CLEANUP;
+	}
+	*pp = arg;
     } else {
 	utc = 0;
 	*pp = mod + 9;

Index: src/usr.bin/make/unit-tests/varmod-gmtime.exp
diff -u src/usr.bin/make/unit-tests/varmod-gmtime.exp:1.5 src/usr.bin/make/unit-tests/varmod-gmtime.exp:1.6
--- src/usr.bin/make/unit-tests/varmod-gmtime.exp:1.5	Sat Oct 31 20:30:06 2020
+++ src/usr.bin/make/unit-tests/varmod-gmtime.exp	Sat Oct 31 21:40:20 2020
@@ -4,16 +4,24 @@ mod-gmtime:
 %Y
 localtime == localtime
 mod-gmtime-indirect:
-make: Unknown modifier '1'
+make: Invalid time value: ${:U1593536400}}
 
+mtime=1593536400}
 parse-errors:
-: -1 becomes Wed Dec 31 23:59:59 1969.
-: space 1 becomes Thu Jan  1 00:00:01 1970.
+make: Invalid time value: -1}.
+
+: -1 becomes mtime=-1}.
+make: Invalid time value:  1}.
+
+: space 1 becomes mtime= 1}.
 : 0 becomes ok.
 : 1 becomes Thu Jan  1 00:00:01 1970.
 : INT32_MAX becomes Tue Jan 19 03:14:07 2038.
 : INT32_MAX + 1 becomes Tue Jan 19 03:14:08 2038.
-: overflow becomes Wed Dec 31 23:59:59 1969.
-make: Unknown modifier 'e'
-: letter becomes .
+make: Invalid time value: 10000000000000000000000000000000}.
+
+: overflow becomes mtime=10000000000000000000000000000000}.
+make: Invalid time value: error}.
+
+: letter becomes mtime=error}.
 exit status 0

Index: src/usr.bin/make/unit-tests/varmod-localtime.exp
diff -u src/usr.bin/make/unit-tests/varmod-localtime.exp:1.3 src/usr.bin/make/unit-tests/varmod-localtime.exp:1.4
--- src/usr.bin/make/unit-tests/varmod-localtime.exp:1.3	Sat Oct 31 20:30:06 2020
+++ src/usr.bin/make/unit-tests/varmod-localtime.exp	Sat Oct 31 21:40:20 2020
@@ -4,16 +4,24 @@ mod-localtime
 %Y
 gmtime == gmtime
 mod-localtime-indirect:
-make: Unknown modifier '1'
+make: Invalid time value: ${:U1593536400}}
 
+ocaltime=1593536400}
 parse-errors:
-: -1 becomes Thu Jan  1 00:59:59 1970.
-: space 1 becomes Thu Jan  1 01:00:01 1970.
+make: Invalid time value: -1}.
+
+: -1 becomes ocaltime=-1}.
+make: Invalid time value:  1}.
+
+: space 1 becomes ocaltime= 1}.
 : 0 becomes ok.
 : 1 becomes Thu Jan  1 01:00:01 1970.
 : INT32_MAX becomes Tue Jan 19 04:14:07 2038.
 : INT32_MAX + 1 becomes Tue Jan 19 04:14:08 2038.
-: overflow becomes Thu Jan  1 00:59:59 1970.
-make: Unknown modifier 'e'
-: letter becomes .
+make: Invalid time value: 10000000000000000000000000000000}.
+
+: overflow becomes ocaltime=10000000000000000000000000000000}.
+make: Invalid time value: error}.
+
+: letter becomes ocaltime=error}.
 exit status 0

Reply via email to