Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): remove pathname limit for Dir_FindHere...



details:   https://anonhg.NetBSD.org/src/rev/742c8436801d
branches:  trunk
changeset: 940210:742c8436801d
user:      rillig <rillig%NetBSD.org@localhost>
date:      Mon Oct 05 22:45:47 2020 +0000

description:
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.

diffstat:

 usr.bin/make/dir.c  |  48 +++++++++++++++++++-----------------------------
 usr.bin/make/dir.h  |   4 ++--
 usr.bin/make/main.c |  21 ++++++++++-----------
 3 files changed, 31 insertions(+), 42 deletions(-)

diffs (178 lines):

diff -r 67e62a2a2ee6 -r 742c8436801d usr.bin/make/dir.c
--- a/usr.bin/make/dir.c        Mon Oct 05 22:15:45 2020 +0000
+++ b/usr.bin/make/dir.c        Mon Oct 05 22:45:47 2020 +0000
@@ -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_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 @@
                    *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 @@
        while (dirbase_end > dirbase && *dirbase_end != '/')
            dirbase_end--;
        *dirbase_end = '\0';    /* chop! */
+    }
 
-    } /* while (TRUE) */
-
-    return FALSE;
+    free(dirbase);
+    return NULL;
 }
 
 /*-
diff -r 67e62a2a2ee6 -r 742c8436801d usr.bin/make/dir.h
--- a/usr.bin/make/dir.h        Mon Oct 05 22:15:45 2020 +0000
+++ b/usr.bin/make/dir.h        Mon Oct 05 22:45:47 2020 +0000
@@ -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 @@
 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 *);
diff -r 67e62a2a2ee6 -r 742c8436801d usr.bin/make/main.c
--- a/usr.bin/make/main.c       Mon Oct 05 22:15:45 2020 +0000
+++ b/usr.bin/make/main.c       Mon Oct 05 22:45:47 2020 +0000
@@ -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 @@
 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 @@
        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 @@
                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);
                        }
                }
        }



Home | Main Index | Thread Index | Old Index