src/util.c (strip_leading_slashes): Handle /dev/null in the patch file.
(fetchname): New variable.
---
 src/util.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/util.c b/src/util.c
index 15a374dcf..afa8f4875 100644
--- a/src/util.c
+++ b/src/util.c
@@ -54,6 +54,8 @@
 
 #include <safe.h>
 
+static bool devnull = false;
+
 static void makedirs (char const *);
 
 typedef struct
@@ -1440,17 +1442,25 @@ strip_leading_slashes (char *name, int strip_leading)
          if (guess_strip && strip_leading == -1)
            {
              struct stat st;
-             /* If "n" is an existent file and is not a directory,
-                we successfully guessed strip.  */
-             if (safe_stat (n, &st) == 0 && !(st.st_mode & S_IFDIR))
+             char *dirname = dir_name (n);
+             if (safe_stat (dirname, &st) == 0 && (st.st_mode & S_IFDIR))
                {
-                 /* We guessed the strip value. As this value should
-                    be the same for each files being patched, assign
-                    global strippath to this value.  */
-                 strippath = gs;
-                 break;
+                 /* If input file is "/dev/null" or "n" is an existent file
+                    and is not a directory, we successfully guessed strip.  */
+                 if (devnull
+                     || (safe_stat (n, &st) == 0 && !(st.st_mode & S_IFDIR)))
+                   {
+                     /* We guessed the strip value. As this value should
+                        be the same for each files being patched, assign
+                        global strippath to this value.  */
+                     strippath = gs;
+                     devnull = false;
+                     free (dirname);
+                     break;
+                   }
                }
              gs++;
+             free (dirname);
            }
 
          while (ISSLASH (p[1]))
@@ -1522,6 +1532,7 @@ fetchname (char const *at, int strip_leading, char 
**pname,
        regardless of how NULL_DEVICE is spelled.  */
     if (strcmp (name, "/dev/null") == 0)
       {
+       devnull = true;
        free (name);
        if (pstamp)
          {
-- 
2.24.1


Reply via email to