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.
This commit is contained in:
rillig 2020-10-05 22:45:47 +00:00
parent ce82f0970c
commit 1b9c32a3c0
3 changed files with 31 additions and 42 deletions

View File

@ -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, SearchPath *path)
}
/*-
*-----------------------------------------------------------------------
* 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, const char *search_path,
*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, const char *search_path,
while (dirbase_end > dirbase && *dirbase_end != '/')
dirbase_end--;
*dirbase_end = '\0'; /* chop! */
}
} /* while (TRUE) */
return FALSE;
free(dirbase);
return NULL;
}
/*-

View File

@ -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 *);

View File

@ -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,7 +1057,6 @@ 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 utsname utsname;
@ -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);
}
}
}