Skip to content

Commit f47e5a7

Browse files
committed
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.
1 parent 91fff80 commit f47e5a7

8 files changed

+40
-25
lines changed

cleanup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ NORETURN void _exit_cleanup(int code, const char *file, int line)
137137
if (DEBUG_GTE(EXIT, 2)) {
138138
rprintf(FINFO,
139139
"[%s] _exit_cleanup(code=%d, file=%s, line=%d): entered\n",
140-
who_am_i(), code, file, line);
140+
who_am_i(), code, src_file(file), line);
141141
}
142142

143143
#include "case_N.h"

flist.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ static char empty_sum[MAX_DIGEST_LEN];
133133
static int flist_count_offset; /* for --delete --progress */
134134
static int show_filelist_progress;
135135

136+
static struct file_list *flist_new(int flags, const char *msg);
136137
static void flist_sort_and_clean(struct file_list *flist, int strip_root);
137138
static void output_flist(struct file_list *flist);
138139

@@ -2797,25 +2798,20 @@ void clear_file(struct file_struct *file)
27972798
}
27982799

27992800
/* Allocate a new file list. */
2800-
struct file_list *flist_new(int flags, char *msg)
2801+
static struct file_list *flist_new(int flags, const char *msg)
28012802
{
28022803
struct file_list *flist;
28032804

28042805
flist = new0(struct file_list);
28052806

28062807
if (flags & FLIST_TEMP) {
2807-
if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0,
2808-
out_of_memory,
2809-
POOL_INTERN)))
2808+
if (!(flist->file_pool = pool_create(SMALL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
28102809
out_of_memory(msg);
28112810
} else {
28122811
/* This is a doubly linked list with prev looping back to
28132812
* the end of the list, but the last next pointer is NULL. */
28142813
if (!first_flist) {
2815-
flist->file_pool = pool_create(NORMAL_EXTENT, 0,
2816-
out_of_memory,
2817-
POOL_INTERN);
2818-
if (!flist->file_pool)
2814+
if (!(flist->file_pool = pool_create(NORMAL_EXTENT, 0, _out_of_memory, POOL_INTERN)))
28192815
out_of_memory(msg);
28202816

28212817
flist->ndx_start = flist->flist_num = inc_recurse ? 1 : 0;

lib/pool_alloc.3

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pool_alloc, pool_free, pool_free_old, pool_talloc, pool_tfree, pool_create, pool
3333
.SH SYNOPSIS
3434
.B #include "pool_alloc.h"
3535

36-
\fBstruct alloc_pool *pool_create(size_t \fIsize\fB, size_t \fIquantum\fB, void (*\fIbomb\fB)(char *), int \fIflags\fB);
36+
\fBstruct alloc_pool *pool_create(size_t \fIsize\fB, size_t \fIquantum\fB, void (*\fIbomb\fB)(char*,char*,int), int \fIflags\fB);
3737

3838
\fBvoid pool_destroy(struct alloc_pool *\fIpool\fB);
3939

lib/pool_alloc.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ struct align_test {
4545
#define PTR_ADD(b,o) ( (void*) ((char*)(b) + (o)) )
4646

4747
alloc_pool_t
48-
pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags)
48+
pool_create(size_t size, size_t quantum, void (*bomb)(const char*, const char*, int), int flags)
4949
{
5050
struct alloc_pool *pool;
5151

5252
if ((MINALIGN & (MINALIGN - 1)) != 0) {
5353
if (bomb)
54-
(*bomb)("Compiler error: MINALIGN is not a power of 2\n");
54+
(*bomb)("Compiler error: MINALIGN is not a power of 2", __FILE__, __LINE__);
5555
return NULL;
5656
}
5757

@@ -169,7 +169,7 @@ pool_alloc(alloc_pool_t p, size_t len, const char *bomb_msg)
169169

170170
bomb_out:
171171
if (pool->bomb)
172-
(*pool->bomb)(bomb_msg);
172+
(*pool->bomb)(bomb_msg, __FILE__, __LINE__);
173173
return NULL;
174174
}
175175

lib/pool_alloc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
typedef void *alloc_pool_t;
99

10-
alloc_pool_t pool_create(size_t size, size_t quantum, void (*bomb)(const char *), int flags);
10+
alloc_pool_t pool_create(size_t size, size_t quantum, void (*bomb)(const char*, const char*, int), int flags);
1111
void pool_destroy(alloc_pool_t pool);
1212
void *pool_alloc(alloc_pool_t pool, size_t size, const char *bomb_msg);
1313
void pool_free(alloc_pool_t pool, size_t size, void *addr);

log.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -890,10 +890,10 @@ void log_exit(int code, const char *file, int line)
890890
/* VANISHED is not an error, only a warning */
891891
if (code == RERR_VANISHED) {
892892
rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
893-
name, code, file, line, who_am_i(), RSYNC_VERSION);
893+
name, code, src_file(file), line, who_am_i(), RSYNC_VERSION);
894894
} else {
895895
rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
896-
name, code, file, line, who_am_i(), RSYNC_VERSION);
896+
name, code, src_file(file), line, who_am_i(), RSYNC_VERSION);
897897
}
898898
}
899899
}

rsync.h

+3
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,9 @@ extern char *do_malloc;
13251325
#undef strdup
13261326
#define strdup(s) my_strdup(s, __FILE__, __LINE__)
13271327

1328+
#define out_of_memory(msg) _out_of_memory(msg, __FILE__, __LINE__)
1329+
#define overflow_exit(msg) _overflow_exit(msg, __FILE__, __LINE__)
1330+
13281331
/* use magic gcc attributes to catch format errors */
13291332
void rprintf(enum logcode , const char *, ...)
13301333
__attribute__((format (printf, 2, 3)))

util2.c

+25-9
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
7676
if (!file)
7777
return NULL;
7878
rprintf(FERROR, "[%s] exceeded --max-alloc=%s setting (file=%s, line=%d)\n",
79-
who_am_i(), do_big_num(max_alloc, 0, NULL), file, line);
79+
who_am_i(), do_big_num(max_alloc, 0, NULL), src_file(file), line);
8080
exit_cleanup(RERR_MALLOC);
8181
}
8282
if (!ptr)
@@ -85,10 +85,8 @@ void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
8585
ptr = malloc(num * size);
8686
else
8787
ptr = realloc(ptr, num * size);
88-
if (!ptr && file) {
89-
rprintf(FERROR, "[%s] out of memory (file=%s, line=%d)\n", who_am_i(), file, line);
90-
exit_cleanup(RERR_MALLOC);
91-
}
88+
if (!ptr && file)
89+
_out_of_memory("my_alloc caller", file, line);
9290
return ptr;
9391
}
9492

@@ -119,14 +117,32 @@ const char *sum_as_hex(int csum_type, const char *sum, int flist_csum)
119117
return buf;
120118
}
121119

122-
NORETURN void out_of_memory(const char *str)
120+
NORETURN void _out_of_memory(const char *msg, const char *file, int line)
123121
{
124-
rprintf(FERROR, "ERROR: out of memory in %s [%s]\n", str, who_am_i());
122+
rprintf(FERROR, "[%s] out of memory: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line);
125123
exit_cleanup(RERR_MALLOC);
126124
}
127125

128-
NORETURN void overflow_exit(const char *str)
126+
NORETURN void _overflow_exit(const char *msg, const char *file, int line)
129127
{
130-
rprintf(FERROR, "ERROR: buffer overflow in %s [%s]\n", str, who_am_i());
128+
rprintf(FERROR, "[%s] buffer overflow: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line);
131129
exit_cleanup(RERR_MALLOC);
132130
}
131+
132+
const char *src_file(const char *file)
133+
{
134+
static const char *util2 = __FILE__;
135+
static int prefix = -1;
136+
137+
if (prefix < 0) {
138+
const char *cp;
139+
for (cp = util2, prefix = 0; *cp; cp++) {
140+
if (*cp == '/')
141+
prefix = cp - util2 + 1;
142+
}
143+
}
144+
145+
if (prefix && strncmp(file, util2, prefix) == 0)
146+
return file + prefix;
147+
return file;
148+
}

0 commit comments

Comments
 (0)