Make out-of-memory handling more uniform
This fixes one real bug in dumpstr().
* defs.h: Declare die_out_of_memory().
* strace.c (die_out_of_memory): New function.
(strace_popen): If allocation fails, call die_out_of_memory().
(main): Likewise.
(expand_tcbtab): Likewise.
(rebuild_pollv): Likewise.
* count.c (count_syscall): Likewise.
(call_summary_pers): Likewise.
* desc.c (decode_select): Likewise.
* file.c (sys_getdents): Likewise.
(sys_getdents64): Likewise.
(sys_getdirentries): Likewise.
* pathtrace.c (pathtrace_match): Likewise.
* syscall.c (qualify): Likewise.
* util.c (printstr): Likewise.
(dumpiov): Likewise.
(dumpstr): Likewise.
(fixvfork): Likewise.
* mem.c (sys_mincore): Don't check free() parameter for NULL.
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
diff --git a/util.c b/util.c
index 7bf2698..75a449d 100644
--- a/util.c
+++ b/util.c
@@ -619,14 +619,15 @@
return;
}
/* Allocate static buffers if they are not allocated yet. */
- if (!str)
+ if (!str) {
str = malloc(max_strlen + 1);
- if (!outstr)
+ if (!str)
+ die_out_of_memory();
+ }
+ if (!outstr) {
outstr = malloc(4 * max_strlen + sizeof "\"...\"");
- if (!str || !outstr) {
- fprintf(stderr, "out of memory\n");
- tprintf("%#lx", addr);
- return;
+ if (!outstr)
+ die_out_of_memory();
}
if (len < 0) {
@@ -687,10 +688,9 @@
unsigned long size;
size = sizeof_iov * (unsigned long) len;
- if (size / sizeof_iov != len
+ if (size / sizeof_iov != len /* overflow? */
|| (iov = malloc(size)) == NULL) {
- fprintf(stderr, "out of memory\n");
- return;
+ die_out_of_memory();
}
if (umoven(tcp, addr, size, (char *) iov) >= 0) {
for (i = 0; i < len; i++) {
@@ -715,18 +715,14 @@
{
static int strsize = -1;
static unsigned char *str;
- static char outstr[80];
char *s;
int i, j;
if (strsize < len) {
free(str);
str = malloc(len);
- if (str == NULL) {
- fprintf(stderr, "out of memory\n");
- /* BUG! On next call we may use NULL str! */
- return;
- }
+ if (!str)
+ die_out_of_memory();
strsize = len;
}
@@ -734,6 +730,8 @@
return;
for (i = 0; i < len; i += 16) {
+ char outstr[80];
+
s = outstr;
sprintf(s, " | %05x ", i);
s += 9;
@@ -1741,10 +1739,8 @@
return -1;
}
strtab = malloc((unsigned)ld.ld_symb_size);
- if (strtab == NULL) {
- fprintf(stderr, "out of memory\n");
- return -1;
- }
+ if (!strtab)
+ die_out_of_memory();
if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
(int)ld.ld_symb_size, strtab) < 0)
goto err;