Sat Oct 31 23:01:23 2020 UTC ()
make(1): fix off-by-one bug in ParseTrackInput (since 2015-11-26)


(rillig)
diff -r1.415 -r1.416 src/usr.bin/make/parse.c
diff -r1.2 -r1.3 src/usr.bin/make/unit-tests/directive-include.exp
diff -r1.2 -r1.3 src/usr.bin/make/unit-tests/directive-include.mk

cvs diff -r1.415 -r1.416 src/usr.bin/make/parse.c (expand / switch to unified diff)

--- src/usr.bin/make/parse.c 2020/10/31 21:52:56 1.415
+++ src/usr.bin/make/parse.c 2020/10/31 23:01:23 1.416
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: parse.c,v 1.415 2020/10/31 21:52:56 rillig Exp $ */ 1/* $NetBSD: parse.c,v 1.416 2020/10/31 23:01:23 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -107,27 +107,27 @@ @@ -107,27 +107,27 @@
107#ifndef MAP_FILE 107#ifndef MAP_FILE
108#define MAP_FILE 0 108#define MAP_FILE 0
109#endif 109#endif
110#ifndef MAP_COPY 110#ifndef MAP_COPY
111#define MAP_COPY MAP_PRIVATE 111#define MAP_COPY MAP_PRIVATE
112#endif 112#endif
113 113
114#include "make.h" 114#include "make.h"
115#include "dir.h" 115#include "dir.h"
116#include "job.h" 116#include "job.h"
117#include "pathnames.h" 117#include "pathnames.h"
118 118
119/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */ 119/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
120MAKE_RCSID("$NetBSD: parse.c,v 1.415 2020/10/31 21:52:56 rillig Exp $"); 120MAKE_RCSID("$NetBSD: parse.c,v 1.416 2020/10/31 23:01:23 rillig Exp $");
121 121
122/* types and constants */ 122/* types and constants */
123 123
124/* 124/*
125 * Structure for a file being read ("included file") 125 * Structure for a file being read ("included file")
126 */ 126 */
127typedef struct IFile { 127typedef struct IFile {
128 char *fname; /* name of file */ 128 char *fname; /* name of file */
129 Boolean fromForLoop; /* simulated .include by the .for loop */ 129 Boolean fromForLoop; /* simulated .include by the .for loop */
130 int lineno; /* current line number in file */ 130 int lineno; /* current line number in file */
131 int first_lineno; /* line number of start of text */ 131 int first_lineno; /* line number of start of text */
132 unsigned int cond_depth; /* 'if' nesting when file opened */ 132 unsigned int cond_depth; /* 'if' nesting when file opened */
133 Boolean depending; /* state of doing_depend on EOF */ 133 Boolean depending; /* state of doing_depend on EOF */
@@ -2356,27 +2356,27 @@ ParseSetParseFile(const char *filename) @@ -2356,27 +2356,27 @@ ParseSetParseFile(const char *filename)
2356static void 2356static void
2357ParseTrackInput(const char *name) 2357ParseTrackInput(const char *name)
2358{ 2358{
2359 void *fp = NULL; 2359 void *fp = NULL;
2360 2360
2361 const char *old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp); 2361 const char *old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
2362 if (old) { 2362 if (old) {
2363 size_t name_len = strlen(name); 2363 size_t name_len = strlen(name);
2364 const char *ep = old + strlen(old) - name_len; 2364 const char *ep = old + strlen(old) - name_len;
2365 /* does it contain name? */ 2365 /* does it contain name? */
2366 for (; old != NULL; old = strchr(old, ' ')) { 2366 for (; old != NULL; old = strchr(old, ' ')) {
2367 if (*old == ' ') 2367 if (*old == ' ')
2368 old++; 2368 old++;
2369 if (old >= ep) 2369 if (old > ep)
2370 break; /* cannot contain name */ 2370 break; /* cannot contain name */
2371 if (memcmp(old, name, name_len) == 0 && 2371 if (memcmp(old, name, name_len) == 0 &&
2372 (old[name_len] == '\0' || old[name_len] == ' ')) 2372 (old[name_len] == '\0' || old[name_len] == ' '))
2373 goto cleanup; 2373 goto cleanup;
2374 } 2374 }
2375 } 2375 }
2376 Var_Append(MAKE_MAKEFILES, name, VAR_GLOBAL); 2376 Var_Append(MAKE_MAKEFILES, name, VAR_GLOBAL);
2377cleanup: 2377cleanup:
2378 bmake_free(fp); 2378 bmake_free(fp);
2379} 2379}
2380 2380
2381 2381
2382/* Start Parsing from the given source. 2382/* Start Parsing from the given source.

cvs diff -r1.2 -r1.3 src/usr.bin/make/unit-tests/directive-include.exp (expand / switch to unified diff)

--- src/usr.bin/make/unit-tests/directive-include.exp 2020/10/31 22:55:35 1.2
+++ src/usr.bin/make/unit-tests/directive-include.exp 2020/10/31 23:01:23 1.3
@@ -1,7 +1,5 @@ @@ -1,7 +1,5 @@
1CondParser_Eval: ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null" 1CondParser_Eval: ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null"
2lhs = "directive-include.mk null", rhs = "directive-include.mk null", op = != 2lhs = "directive-include.mk null", rhs = "directive-include.mk null", op = !=
3CondParser_Eval: ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null null" 3CondParser_Eval: ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null"
4lhs = "directive-include.mk null null", rhs = "directive-include.mk null null", op = != 4lhs = "directive-include.mk null", rhs = "directive-include.mk null", op = !=
5CondParser_Eval: ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null null" 
6lhs = "directive-include.mk null null", rhs = "directive-include.mk null null", op = != 
7exit status 0 5exit status 0

cvs diff -r1.2 -r1.3 src/usr.bin/make/unit-tests/directive-include.mk (expand / switch to unified diff)

--- src/usr.bin/make/unit-tests/directive-include.mk 2020/10/31 22:55:35 1.2
+++ src/usr.bin/make/unit-tests/directive-include.mk 2020/10/31 23:01:23 1.3
@@ -1,33 +1,26 @@ @@ -1,33 +1,26 @@
1# $NetBSD: directive-include.mk,v 1.2 2020/10/31 22:55:35 rillig Exp $ 1# $NetBSD: directive-include.mk,v 1.3 2020/10/31 23:01:23 rillig Exp $
2# 2#
3# Tests for the .include directive, which includes another file. 3# Tests for the .include directive, which includes another file.
4 4
5# TODO: Implementation 5# TODO: Implementation
6 6
7.MAKEFLAGS: -dc 7.MAKEFLAGS: -dc
8 8
9# All included files are recorded in the variable .MAKE.MAKEFILES. 9# All included files are recorded in the variable .MAKE.MAKEFILES.
10# In this test, only the basenames of the files are compared since 10# In this test, only the basenames of the files are compared since
11# the directories can differ. 11# the directories can differ.
12.include "/dev/null" 12.include "/dev/null"
13.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null" 13.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null"
14. error 14. error
15.endif 15.endif
16 16
17# Each file is recorded only once in the variable .MAKE.MAKEFILES. 17# Each file is recorded only once in the variable .MAKE.MAKEFILES.
18# XXX: As of 2020-10-31, the very last file can be repeated, due to an 18# Between 2015-11-26 and 2020-10-31, the very last file could be repeated,
19# off-by-one bug in ParseTrackInput. 19# due to an off-by-one bug in ParseTrackInput.
20.include "/dev/null" 20.include "/dev/null"
21.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null null" 21.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null"
22. error 
23.endif 
24 
25# Since the file /dev/null is not only recorded at the very end of the 
26# variable .MAKE.MAKEFILES, it is not added a third time. 
27.include "/dev/null" 
28.if ${.MAKE.MAKEFILES:T} != "${.PARSEFILE} null null" 
29. error 22. error
30.endif 23.endif
31 24
32all: 25all:
33 @:; 26 @:;