Mention file & line on OOM and overflow errors.
authorWayne Davison <wayne@opencoder.net>
Mon, 13 Jul 2020 05:46:21 +0000 (22:46 -0700)
committerWayne Davison <wayne@opencoder.net>
Mon, 13 Jul 2020 06:25:21 +0000 (23:25 -0700)
Also simplify output of src file paths in errors & warnings when
built in a alternate build dir.

cleanup.c
flist.c
lib/pool_alloc.3
lib/pool_alloc.c
lib/pool_alloc.h
log.c
rsync.h
util2.c

index f9a5d76ba6ff0e30252d2042ec28d9be22f4cad8..40d26baa8f8f79b2bb6b330910036169acb3314f 100644 (file)
--- a/cleanup.c
+++ b/cleanup.c
@@ -137,7 +137,7 @@ NORETURN void _exit_cleanup(int code, const char *file, int line)
                if (DEBUG_GTE(EXIT, 2)) {
                        rprintf(FINFO,
                                "[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n",
-                               who_am_i(), code, file, line);
+                               who_am_i(), code, src_file(file), line);
                }
 
 #include "case_N.h"
diff --git a/flist.c b/flist.c
index 39f942b05f2c9a6bd59b22bb846d23b85f82087d..6c2543cd2e9e3b0be1bf1ca4ff3b20a6734a6cb8 100644 (file)
--- a/flist.c
+++ b/flist.c
@@ -133,6 +133,7 @@ static char empty_sum[MAX_DIGEST_LEN];
 static int flist_count_offset; /* for --delete --progress */
 static int show_filelist_progress;
 
+static struct file_list *flist_new(int flags, const char *msg);
 static void flist_sort_and_clean(struct file_list *flist, int strip_root);
 static void output_flist(struct file_list *flist);
 
@@ -2797,25 +2798,20 @@ void clear_file(struct file_struct *file)
 }
 
 /* Allocate a new file list. */
-struct file_list *flist_new(int flags, char *msg)
+static struct file_list *flist_new(int flags, const char *msg)
 {
        struct file_list *flist;
 
        flist = new0(struct file_list);
 
        if (flags & FLIST_TEMP) {
-               if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
-                                                    out_of_memory,
-                                                    POOL_INTERN)))
+               if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
                        out_of_memory(msg);
        } else {
                /* This is a doubly linked list with prev looping back to
                 * the end of the list, but the last next pointer is NULL. */
                if (!first_flist) {
-                       flist->file_pool = pool_create(NORMAL_EXTENT, 0,
-                                                      out_of_memory,
-                                                      POOL_INTERN);
-                       if (!flist->file_pool)
+                       if (!(flist->file_pool = pool_create(NORMAL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
                                out_of_memory(msg);
 
                        flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;
index 6c22b9247bb917b5a4877db6b4ea57717ab2cf80..128c1f7f33cb224214ebc92183238244b8fab991 100644 (file)
@@ -33,7 +33,7 @@ pool_alloc, pool_free, pool_free_old, pool_talloc, pool_tfree, pool_create, pool
 .SH SYNOPSIS
 .B #include "pool_alloc.h"
 
-\fBstruct alloc_pool *pool_create(size_t \fIsize\fB, size_t \fIquantum\fB, void (*\fIbomb\fB)(char *), int \fIflags\fB);
+\fBstruct alloc_pool *pool_create(size_t \fIsize\fB, size_t \fIquantum\fB, void (*\fIbomb\fB)(char*,char*,int), int \fIflags\fB);
 
 \fBvoid pool_destroy(struct alloc_pool *\fIpool\fB);
 
index a70a3f1a46b2c0332867f2aef64191ab7c5d1ad9..f6c6faf6344fb6c4b94812728c4175204cf4dd9a 100644 (file)
@@ -45,13 +45,13 @@ struct align_test {
 #define PTR_ADD(b,o)   ( (void*) ((char*)(b) + (o)) )
 
 alloc_pool_t
-pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags)
+pool_create(size_t size, size_t quantum, void (*bomb)(const char*, const char*, int), int flags)
 {
        struct alloc_pool *pool;
 
        if ((MINALIGN & (MINALIGN - 1)) != 0) {
                if (bomb)
-                       (*bomb)("Compiler error: MINALIGN is not a power of 2\n");
+                       (*bomb)("Compiler error: MINALIGN is not a power of 2", __FILE__, __LINE__);
                return NULL;
        }
 
@@ -169,7 +169,7 @@ pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg)
 
   bomb_out:
        if (pool->bomb)
-               (*pool->bomb)(bomb_msg);
+               (*pool->bomb)(bomb_msg, __FILE__, __LINE__);
        return NULL;
 }
 
index c7368a77b748616b0830df855bd89435ed6d34d3..280592123532bdaa909f4ee9862dcb3727383783 100644 (file)
@@ -7,7 +7,7 @@
 
 typedef void *alloc_pool_t;
 
-alloc_pool_t pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags);
+alloc_pool_t pool_create(size_t size, size_t quantum, void (*bomb)(const char*, const char*, int), int flags);
 void pool_destroy(alloc_pool_t pool);
 void *pool_alloc(alloc_pool_t pool, size_t size, const char *bomb_msg);
 void pool_free(alloc_pool_t pool, size_t size, void *addr);
diff --git a/log.c b/log.c
index 633bcdbce261933fd46993d60b502e313711161a..85eae3d5ff383a2b3f29c927c5d2d7a0fbaac42a 100644 (file)
--- a/log.c
+++ b/log.c
@@ -890,10 +890,10 @@ void log_exit(int code, const char *file, int line)
                /* VANISHED is not an error, only a warning */
                if (code == RERR_VANISHED) {
                        rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
-                               name, code, file, line, who_am_i(), RSYNC_VERSION);
+                               name, code, src_file(file), line, who_am_i(), RSYNC_VERSION);
                } else {
                        rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
-                               name, code, file, line, who_am_i(), RSYNC_VERSION);
+                               name, code, src_file(file), line, who_am_i(), RSYNC_VERSION);
                }
        }
 }
diff --git a/rsync.h b/rsync.h
index 9942a7054b8c1f1c7d9b7447da865fba3825d9c3..c871574748c92459a5bc792c69212529883e989e 100644 (file)
--- a/rsync.h
+++ b/rsync.h
@@ -1325,6 +1325,9 @@ extern char *do_malloc;
 #undef strdup
 #define strdup(s) my_strdup(s, __FILE__, __LINE__)
 
+#define out_of_memory(msg) _out_of_memory(msg, __FILE__, __LINE__)
+#define overflow_exit(msg) _overflow_exit(msg, __FILE__, __LINE__)
+
 /* use magic gcc attributes to catch format errors */
  void rprintf(enum logcode , const char *, ...)
      __attribute__((format (printf, 2, 3)))
diff --git a/util2.c b/util2.c
index 6ea6981d00de3de72033bc221efe87c2c25b8ab4..8879c987afa0888fbd008a9eb28863c558f95954 100644 (file)
--- a/util2.c
+++ b/util2.c
@@ -76,7 +76,7 @@ void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
                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)
@@ -85,10 +85,8 @@ void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
                ptr = malloc(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,32 @@ 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;
+               for (cp = util2, prefix = 0; *cp; cp++) {
+                       if (*cp == '/')
+                               prefix = cp - util2 + 1;
+               }
+       }
+
+       if (prefix && strncmp(file, util2, prefix) == 0)
+               return file + prefix;
+       return file;
+}