From f47e5a7732f9b5da71080fdc229acde4ec6da3c1 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Sun, 12 Jul 2020 22:46:21 -0700 Subject: [PATCH] Mention file & line on OOM and overflow errors. Also simplify output of src file paths in errors & warnings when built in a alternate build dir. --- cleanup.c | 2 +- flist.c | 12 ++++-------- lib/pool_alloc.3 | 2 +- lib/pool_alloc.c | 6 +++--- lib/pool_alloc.h | 2 +- log.c | 4 ++-- rsync.h | 3 +++ util2.c | 34 +++++++++++++++++++++++++--------- 8 files changed, 40 insertions(+), 25 deletions(-) diff --git a/cleanup.c b/cleanup.c index f9a5d76b..40d26baa 100644 --- 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 39f942b0..6c2543cd 100644 --- 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; diff --git a/lib/pool_alloc.3 b/lib/pool_alloc.3 index 6c22b924..128c1f7f 100644 --- a/lib/pool_alloc.3 +++ b/lib/pool_alloc.3 @@ -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); diff --git a/lib/pool_alloc.c b/lib/pool_alloc.c index a70a3f1a..f6c6faf6 100644 --- a/lib/pool_alloc.c +++ b/lib/pool_alloc.c @@ -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; } diff --git a/lib/pool_alloc.h b/lib/pool_alloc.h index c7368a77..28059212 100644 --- a/lib/pool_alloc.h +++ b/lib/pool_alloc.h @@ -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 633bcdbc..85eae3d5 100644 --- 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 9942a705..c8715747 100644 --- 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 6ea6981d..8879c987 100644 --- 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; +} -- 2.34.1