X-Git-Url: https://granicus.if.org/sourcecode?a=blobdiff_plain;f=strace.c;h=9c633887265228a6c6c0f00783dba7b5c99e2367;hb=5940e6593911dcace424c668a1c0934c71fccb9e;hp=bb41ce2b01bffa6785b270894b7fafdb80cfbbce;hpb=a673f3fa62e1c2f34d27e88fb0772b9ee3d6f0f1;p=strace diff --git a/strace.c b/strace.c index bb41ce2b..9c633887 100644 --- a/strace.c +++ b/strace.c @@ -264,6 +264,15 @@ void perror_msg_and_die(const char *fmt, ...) die(); } +void die_out_of_memory(void) +{ + static bool recursed = 0; + if (recursed) + exit(1); + recursed = 1; + error_msg_and_die("Out of memory"); +} + #ifdef SVR4 #ifdef MIPS void @@ -383,7 +392,7 @@ strace_popen(const char *command) swap_uid(); fp = fdopen(fds[1], "w"); if (!fp) - error_msg_and_die("Out of memory"); + die_out_of_memory(); return fp; } @@ -947,11 +956,11 @@ main(int argc, char *argv[]) /* Allocate the initial tcbtab. */ tcbtabsize = argc; /* Surely enough for all -p args. */ tcbtab = calloc(tcbtabsize, sizeof(tcbtab[0])); - if (tcbtab == NULL) - error_msg_and_die("Out of memory"); + if (!tcbtab) + die_out_of_memory(); tcp = calloc(tcbtabsize, sizeof(*tcp)); - if (tcp == NULL) - error_msg_and_die("Out of memory"); + if (!tcp) + die_out_of_memory(); for (c = 0; c < tcbtabsize; c++) tcbtab[c] = tcp++; @@ -1078,9 +1087,8 @@ main(int argc, char *argv[]) username = strdup(optarg); break; case 'E': - if (putenv(optarg) < 0) { - error_msg_and_die("Out of memory"); - } + if (putenv(optarg) < 0) + die_out_of_memory(); break; default: usage(stderr, 1); @@ -1090,7 +1098,7 @@ main(int argc, char *argv[]) acolumn_spaces = malloc(acolumn + 1); if (!acolumn_spaces) - error_msg_and_die("Out of memory"); + die_out_of_memory(); memset(acolumn_spaces, ' ', acolumn); acolumn_spaces[acolumn] = '\0'; @@ -1240,8 +1248,8 @@ expand_tcbtab(void) int i = tcbtabsize; struct tcb *newtcbs = calloc(tcbtabsize, sizeof(newtcbs[0])); struct tcb **newtab = realloc(tcbtab, tcbtabsize * 2 * sizeof(tcbtab[0])); - if (newtab == NULL || newtcbs == NULL) - error_msg_and_die("Out of memory"); + if (!newtab || !newtcbs) + die_out_of_memory(); tcbtabsize *= 2; tcbtab = newtab; while (i < tcbtabsize) @@ -1866,9 +1874,8 @@ rebuild_pollv(void) free(pollv); pollv = malloc(nprocs * sizeof(pollv[0])); - if (pollv == NULL) { - error_msg_and_die("Out of memory"); - } + if (!pollv) + die_out_of_memory(); for (i = j = 0; i < tcbtabsize; i++) { struct tcb *tcp = tcbtab[i]; @@ -2639,7 +2646,21 @@ tprintf(const char *fmt, ...) curcol += n; } va_end(args); - return; +} + +void +tprints(const char *str) +{ + if (outf) { + int n = fputs(str, outf); + if (n >= 0) { + curcol += strlen(str); + return; + } + if (outf != stderr) + perror(outfname == NULL + ? "" : outfname); + } } void @@ -2698,7 +2719,7 @@ void tabto(void) { if (curcol < acolumn) - tprintf(acolumn_spaces + curcol); + tprints(acolumn_spaces + curcol); } void