Module Name:    src
Committed By:   rillig
Date:           Mon Oct  5 22:45:47 UTC 2020

Modified Files:
        src/usr.bin/make: dir.c dir.h main.c

Log Message:
make(1): remove pathname limit for Dir_FindHereOrAbove

While trying to compile the code with GCC's -Wformat-truncation, the
snprintf calls felt quite complicated.  The function Dir_FindHereOrAbove
is not in a bottleneck execution path, therefore it doesn't hurt to
dynamically allocate the memory instead of using size-limited stack
memory.


To generate a diff of this commit:
cvs rdiff -u -r1.160 -r1.161 src/usr.bin/make/dir.c
cvs rdiff -u -r1.28 -r1.29 src/usr.bin/make/dir.h
cvs rdiff -u -r1.370 -r1.371 src/usr.bin/make/main.c

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/dir.c
diff -u src/usr.bin/make/dir.c:1.160 src/usr.bin/make/dir.c:1.161
--- src/usr.bin/make/dir.c:1.160	Mon Oct  5 20:21:30 2020
+++ src/usr.bin/make/dir.c	Mon Oct  5 22:45:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.160 2020/10/05 20:21:30 rillig Exp $	*/
+/*	$NetBSD: dir.c,v 1.161 2020/10/05 22:45:47 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -135,7 +135,7 @@
 #include "job.h"
 
 /*	"@(#)dir.c	8.2 (Berkeley) 1/2/94"	*/
-MAKE_RCSID("$NetBSD: dir.c,v 1.160 2020/10/05 20:21:30 rillig Exp $");
+MAKE_RCSID("$NetBSD: dir.c,v 1.161 2020/10/05 22:45:47 rillig Exp $");
 
 #define DIR_DEBUG0(text) DEBUG0(DIR, text)
 #define DIR_DEBUG1(fmt, arg1) DEBUG1(DIR, fmt, arg1)
@@ -1355,43 +1355,32 @@ Dir_FindFile(const char *name, SearchPat
 }
 
 
-/*-
- *-----------------------------------------------------------------------
- * Dir_FindHereOrAbove  --
- *	search for a path starting at a given directory and then working
- *	our way up towards the root.
+/* Search for a path starting at a given directory and then working our way
+ * up towards the root.
  *
  * Input:
  *	here		starting directory
- *	search_path	the path we are looking for
- *	result		the result of a successful search is placed here
- *	result_len	the length of the result buffer
- *			(typically MAXPATHLEN + 1)
+ *	search_path	the relative path we are looking for
  *
  * Results:
- *	0 on failure, 1 on success [in which case the found path is put
- *	in the result buffer].
- *
- * Side Effects:
- *-----------------------------------------------------------------------
+ *	The found path, or NULL.
  */
-Boolean
-Dir_FindHereOrAbove(const char *here, const char *search_path,
-		    char *result, size_t result_len)
+char *
+Dir_FindHereOrAbove(const char *here, const char *search_path)
 {
     struct make_stat mst;
-    char dirbase[MAXPATHLEN + 1], *dirbase_end;
-    char try[MAXPATHLEN + 1], *try_end;
+    char *dirbase, *dirbase_end;
+    char *try, *try_end;
 
     /* copy out our starting point */
-    snprintf(dirbase, sizeof(dirbase), "%s", here);
+    dirbase = bmake_strdup(here);
     dirbase_end = dirbase + strlen(dirbase);
 
     /* loop until we determine a result */
-    while (TRUE) {
+    for (;;) {
 
 	/* try and stat(2) it ... */
-	snprintf(try, sizeof(try), "%s/%s", dirbase, search_path);
+	try = str_concat3(dirbase, "/", search_path);
 	if (cached_stat(try, &mst) != -1) {
 	    /*
 	     * success!  if we found a file, chop off
@@ -1405,9 +1394,10 @@ Dir_FindHereOrAbove(const char *here, co
 		    *try_end = '\0';	/* chop! */
 	    }
 
-	    snprintf(result, result_len, "%s", try);
-	    return TRUE;
+	    free(dirbase);
+	    return try;
 	}
+	free(try);
 
 	/*
 	 * nope, we didn't find it.  if we used up dirbase we've
@@ -1422,10 +1412,10 @@ Dir_FindHereOrAbove(const char *here, co
 	while (dirbase_end > dirbase && *dirbase_end != '/')
 	    dirbase_end--;
 	*dirbase_end = '\0';	/* chop! */
+    }
 
-    } /* while (TRUE) */
-
-    return FALSE;
+    free(dirbase);
+    return NULL;
 }
 
 /*-

Index: src/usr.bin/make/dir.h
diff -u src/usr.bin/make/dir.h:1.28 src/usr.bin/make/dir.h:1.29
--- src/usr.bin/make/dir.h:1.28	Mon Oct  5 20:21:30 2020
+++ src/usr.bin/make/dir.h	Mon Oct  5 22:45:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.h,v 1.28 2020/10/05 20:21:30 rillig Exp $	*/
+/*	$NetBSD: dir.h,v 1.29 2020/10/05 22:45:47 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -99,7 +99,7 @@ void Dir_SetPATH(void);
 Boolean Dir_HasWildcards(const char *);
 void Dir_Expand(const char *, SearchPath *, StringList *);
 char *Dir_FindFile(const char *, SearchPath *);
-Boolean Dir_FindHereOrAbove(const char *, const char *, char *, size_t);
+char *Dir_FindHereOrAbove(const char *, const char *);
 time_t Dir_MTime(GNode *, Boolean);
 CachedDir *Dir_AddDir(SearchPath *, const char *);
 char *Dir_MakeFlags(const char *, SearchPath *);

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.370 src/usr.bin/make/main.c:1.371
--- src/usr.bin/make/main.c:1.370	Mon Oct  5 21:37:07 2020
+++ src/usr.bin/make/main.c	Mon Oct  5 22:45:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.370 2020/10/05 21:37:07 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.371 2020/10/05 22:45:47 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -122,7 +122,7 @@
 #endif
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.370 2020/10/05 21:37:07 rillig Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.371 2020/10/05 22:45:47 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -459,14 +459,13 @@ MainParseArgJobs(const char *argvalue)
 static void
 MainParseArgSysInc(const char *argvalue)
 {
-	char found_path[MAXPATHLEN + 1];
-
 	/* look for magic parent directory search string */
 	if (strncmp(".../", argvalue, 4) == 0) {
-		if (!Dir_FindHereOrAbove(curdir, argvalue + 4,
-					 found_path, sizeof(found_path)))
+		char *found_path = Dir_FindHereOrAbove(curdir, argvalue + 4);
+		if (found_path == NULL)
 			return;
 		(void)Dir_AddDir(sysIncPath, found_path);
+		free(found_path);
 	} else {
 		(void)Dir_AddDir(sysIncPath, argvalue);
 	}
@@ -1058,8 +1057,7 @@ main(int argc, char **argv)
 	char *cp = NULL, *start;
 					/* avoid faults on read-only strings */
 	static char defsyspath[] = _PATH_DEFSYSPATH;
-	char found_path[MAXPATHLEN + 1];	/* for searching for sys.mk */
-	struct timeval rightnow;		/* to initialize random seed */
+	struct timeval rightnow;	/* to initialize random seed */
 	struct utsname utsname;
 
 	/* default to writing debug to stderr */
@@ -1348,9 +1346,10 @@ main(int argc, char **argv)
 		if (strncmp(".../", start, 4) != 0) {
 			(void)Dir_AddDir(defIncPath, start);
 		} else {
-			if (Dir_FindHereOrAbove(curdir, start+4,
-			    found_path, sizeof(found_path))) {
-				(void)Dir_AddDir(defIncPath, found_path);
+			char *dir = Dir_FindHereOrAbove(curdir, start + 4);
+			if (dir != NULL) {
+				(void)Dir_AddDir(defIncPath, dir);
+				free(dir);
 			}
 		}
 	}

Reply via email to