Fix --remove-source-files sanity check w/--copy-links the right way.
[rsync.git] / util.c
diff --git a/util.c b/util.c
index d50900c8d387747a76b51f25abb0ac8dd4051252..235afa82a85978b70be24d0150f4cf8bdb249086 100644 (file)
--- a/util.c
+++ b/util.c
@@ -4,7 +4,7 @@
  * Copyright (C) 1996-2000 Andrew Tridgell
  * Copyright (C) 1996 Paul Mackerras
  * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
- * Copyright (C) 2003-2015 Wayne Davison
+ * Copyright (C) 2003-2018 Wayne Davison
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -135,7 +135,6 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
                if (errno != ENOSYS)
                        return -1;
                switch_step++;
-               /* FALLTHROUGH */
 #endif
 
 #ifdef HAVE_UTIMENSAT
@@ -145,7 +144,6 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
                if (errno != ENOSYS)
                        return -1;
                switch_step++;
-               /* FALLTHROUGH */
 #endif
 
 #ifdef HAVE_LUTIMES
@@ -155,7 +153,6 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
                if (errno != ENOSYS)
                        return -1;
                switch_step++;
-               /* FALLTHROUGH */
 #endif
 
 #include "case_N.h"
@@ -165,7 +162,6 @@ int set_modtime(const char *fname, time_t modtime, uint32 mod_nsec, mode_t mode)
                        if (S_ISLNK(mode))
                                return 1;
                }
-               /* FALLTHROUGH */
 
 #include "case_N.h"
 #ifdef HAVE_UTIMES
@@ -346,6 +342,7 @@ int copy_file(const char *source, const char *dest, int ofd, mode_t mode)
                if (robust_unlink(dest) && errno != ENOENT) {
                        int save_errno = errno;
                        rsyserr(FERROR_XFER, errno, "unlink %s", full_fname(dest));
+                       close(ifd);
                        errno = save_errno;
                        return -1;
                }
@@ -1009,7 +1006,7 @@ char *sanitize_path(char *dest, const char *p, const char *rootdir, int depth,
        int rlen = 0, drop_dot_dirs = !relative_paths || !(flags & SP_KEEP_DOT_DIRS);
 
        if (dest != p) {
-               int plen = strlen(p);
+               int plen = strlen(p); /* the path len INCLUDING any separating slash */
                if (*p == '/') {
                        if (!rootdir)
                                rootdir = module_dir;
@@ -1020,11 +1017,11 @@ char *sanitize_path(char *dest, const char *p, const char *rootdir, int depth,
                if (dest) {
                        if (rlen + plen + 1 >= MAXPATHLEN)
                                return NULL;
-               } else if (!(dest = new_array(char, rlen + plen + 1)))
+               } else if (!(dest = new_array(char, MAX(rlen + plen + 1, 2))))
                        out_of_memory("sanitize_path");
-               if (rlen) {
+               if (rlen) { /* only true if p previously started with a slash */
                        memcpy(dest, rootdir, rlen);
-                       if (rlen > 1)
+                       if (rlen > 1) /* a rootdir of len 1 is "/", so this avoids a 2nd slash */
                                dest[rlen++] = '/';
                }
        }
@@ -1686,3 +1683,11 @@ void *expand_item_list(item_list *lp, size_t item_size,
        }
        return (char*)lp->items + (lp->count++ * item_size);
 }
+
+/* This zeroing of memory won't be optimized away by the compiler. */
+void force_memzero(void *buf, size_t len)
+{
+    volatile uchar *z = buf;
+    while (len-- > 0)
+       *z++ = '\0';
+}