Changes for 3.3.1dev.
[rsync.git] / util2.c
diff --git a/util2.c b/util2.c
index 6ea6981d00de3de72033bc221efe87c2c25b8ab4..b59bff0a0b541b8f6837f6d87938ced8e5bac9b9 100644 (file)
--- a/util2.c
+++ b/util2.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-2020 Wayne Davison
+ * Copyright (C) 2003-2024 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
@@ -26,7 +26,7 @@
 
 extern size_t max_alloc;
 
-char *do_malloc = "42";
+char *do_calloc = "42";
 
 /**
  * Sleep for a specified number of milliseconds.
@@ -72,23 +72,21 @@ int msleep(int t)
 
 void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
 {
-       if (max_alloc && num >= max_alloc/size) {
+       if (num >= max_alloc/size) {
                if (!file)
                        return NULL;
                rprintf(FERROR, "[%s] exceeded --max-alloc=%s setting (file=%s, line=%d)\n",
-                       who_am_i(), do_big_num(max_alloc, 0, NULL), file, line);
+                       who_am_i(), do_big_num(max_alloc, 0, NULL), src_file(file), line);
                exit_cleanup(RERR_MALLOC);
        }
        if (!ptr)
-               ptr = calloc(num, size);
-       else if (ptr == do_malloc)
                ptr = malloc(num * size);
+       else if (ptr == do_calloc)
+               ptr = calloc(num, size);
        else
                ptr = realloc(ptr, num * size);
-       if (!ptr && file) {
-               rprintf(FERROR, "[%s] out of memory (file=%s, line=%d)\n", who_am_i(), file, line);
-               exit_cleanup(RERR_MALLOC);
-       }
+       if (!ptr && file)
+               _out_of_memory("my_alloc caller", file, line);
        return ptr;
 }
 
@@ -119,14 +117,29 @@ const char *sum_as_hex(int csum_type, const char *sum, int flist_csum)
        return buf;
 }
 
-NORETURN void out_of_memory(const char *str)
+NORETURN void _out_of_memory(const char *msg, const char *file, int line)
 {
-       rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i());
+       rprintf(FERROR, "[%s] out of memory: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line);
        exit_cleanup(RERR_MALLOC);
 }
 
-NORETURN void overflow_exit(const char *str)
+NORETURN void _overflow_exit(const char *msg, const char *file, int line)
 {
-       rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i());
+       rprintf(FERROR, "[%s] buffer overflow: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line);
        exit_cleanup(RERR_MALLOC);
 }
+
+const char *src_file(const char *file)
+{
+       static const char *util2 = __FILE__;
+       static int prefix = -1;
+
+       if (prefix < 0) {
+               const char *cp = strrchr(util2, '/');
+               prefix = cp ? cp - util2 + 1 : 0;
+       }
+
+       if (prefix && strncmp(file, util2, prefix) == 0)
+               return file + prefix;
+       return file;
+}