Merged in the security fixes from 2.5.7.
[rsync/rsync.git] / util.c
diff --git a/util.c b/util.c
index da10ca2..4ba6caa 100644 (file)
--- a/util.c
+++ b/util.c
@@ -559,13 +559,6 @@ void strlower(char *s)
        }
 }
 
-void *Realloc(void *p, int size)
-{
-       if (!p) return (void *)malloc(size);
-       return (void *)realloc(p, size);
-}
-
-
 void clean_fname(char *name)
 {
        char *p;
@@ -774,6 +767,52 @@ int pop_dir(char *dir)
        return 0;
 }
 
+/**
+ * Return a quoted string with the full pathname of the indicated filename.
+ * The string " (in MODNAME)" may also be appended.  The returned pointer
+ * remains valid until the next time full_fname() is called.
+ **/
+char *full_fname(char *fn)
+{
+       extern int module_id;
+       static char *result = NULL;
+       char *m1, *m2, *m3;
+       char *p1, *p2;
+
+       if (result)
+               free(result);
+
+       if (*fn == '/')
+               p1 = p2 = "";
+       else {
+               p1 = curr_dir;
+               p2 = "/";
+       }
+       if (module_id >= 0) {
+               m1 = " (in ";
+               m2 = lp_name(module_id);
+               m3 = ")";
+               if (*p1) {
+                       if (!lp_use_chroot(module_id)) {
+                               char *p = lp_path(module_id);
+                               if (*p != '/' || p[1])
+                                       p1 += strlen(p);
+                       }
+                       if (!*p1)
+                               p2++;
+                       else
+                               p1++;
+               }
+               else
+                       fn++;
+       } else
+               m1 = m2 = m3 = "";
+
+       asprintf(&result, "\"%s%s%s\"%s%s%s", p1, p2, fn, m1, m2, m3);
+
+       return result;
+}
+
 /** We need to supply our own strcmp function for file list comparisons
    to ensure that signed/unsigned usage is consistent between machines. */
 int u_strcmp(const char *cs1, const char *cs2)
@@ -962,3 +1001,23 @@ int _Insure_trap_error(int a1, int a2, int a3, int a4, int a5, int a6)
        return ret;
 }
 #endif
+
+
+#define MALLOC_MAX 0x40000000
+
+void *_new_array(unsigned int size, unsigned long num)
+{
+       if (num >= MALLOC_MAX/size)
+               return NULL;
+       return malloc(size * num);
+}
+
+void *_realloc_array(void *ptr, unsigned int size, unsigned long num)
+{
+       if (num >= MALLOC_MAX/size)
+               return NULL;
+       /* No realloc should need this, but just in case... */
+       if (!ptr)
+               return malloc(size * num);
+       return realloc(ptr, size * num);
+}