]> git.ipfire.org Git - thirdparty/make.git/commitdiff
* src/implicit.c (pattern_search): Set lastslash correctly
authorJouke Witteveen <j.witteveen@gmail.com>
Sat, 26 Oct 2019 10:24:29 +0000 (12:24 +0200)
committerPaul Smith <psmith@gnu.org>
Tue, 17 Dec 2019 20:06:28 +0000 (15:06 -0500)
If filename contained multiple slashes lastslash is wrongly set to 0.
* configure.ac: Check for the GNU memrchr() extension function.
* src/misc.c (memrchr): Supply memrchr() if not available.

configure.ac
src/implicit.c
src/misc.c

index e12a5b839a49b3dd0e3ffb0c31113611d7b829e5..af58c1b4688993960764dae77554b42ae807eddb 100644 (file)
@@ -138,7 +138,7 @@ AS_IF([test "$ac_cv_func_gettimeofday" = yes],
             [Define to 1 if you have a standard gettimeofday function])
 ])
 
-AC_CHECK_FUNCS([strdup strndup umask mkstemp mktemp fdopen \
+AC_CHECK_FUNCS([strdup strndup memrchr umask mkstemp mktemp fdopen \
                 dup dup2 getcwd realpath sigsetmask sigaction \
                 getgroups seteuid setegid setlinebuf setreuid setregid \
                 getrlimit setrlimit setvbuf pipe strsignal \
index 7bdc8bafa0b25bebdddc0d4317391fbebb5cffec..cd707b28514339d6ce8653e958aa485b90981546 100644 (file)
@@ -265,7 +265,7 @@ pattern_search (struct file *file, int archive,
       /* Set LASTSLASH to point at the last slash in FILENAME
          but not counting any slash at the end.  (foo/bar/ counts as
          bar/ in directory foo/, not empty in directory foo/bar/.)  */
-      lastslash = strrchr (filename, '/');
+      lastslash = memrchr (filename, '/', namelen - 1);
 #ifdef VMS
       if (lastslash == NULL)
         lastslash = strrchr (filename, ']');
@@ -278,18 +278,16 @@ pattern_search (struct file *file, int archive,
       /* Handle backslashes (possibly mixed with forward slashes)
          and the case of "d:file".  */
       {
-        char *bslash = strrchr (filename, '\\');
+        char *bslash = memrchr (filename, '\\', namelen - 1);
         if (lastslash == 0 || bslash > lastslash)
           lastslash = bslash;
         if (lastslash == 0 && filename[0] && filename[1] == ':')
           lastslash = filename + 1;
       }
 #endif
-      if (lastslash != 0 && lastslash[1] == '\0')
-        lastslash = 0;
     }
 
-  pathlen = lastslash - filename + 1;
+  pathlen = lastslash ? lastslash - filename + 1 : 0;
 
   /* First see which pattern rules match this target and may be considered.
      Put them in TRYRULES.  */
index 567c86f947b83da610dc42bfd5b408bdf9f6a9cd..2b7cc12ab27261b77879e5f20b92b2df0746c7df 100644 (file)
@@ -260,6 +260,30 @@ xstrndup (const char *str, size_t length)
 
   return result;
 }
+
+#ifndef HAVE_MEMRCHR
+void *
+memrchr(const void* str, int ch, size_t len)
+{
+  const char* sp = str;
+  const char* cp = sp;
+
+  if (len == 0)
+    return NULL;
+
+  cp += len - 1;
+
+  while (cp[0] != ch)
+    {
+      if (cp == sp)
+        return NULL;
+      --cp;
+    }
+
+  return (void*)cp;
+}
+#endif
+
 \f
 
 /* Limited INDEX: