From 43d81d52a93d8a99ab9697acd284b55d951a3e2d Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sat, 2 Aug 2014 10:30:51 +0200 Subject: [PATCH] Prints more info in low memory error. --- cmdline/util.c | 51 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/cmdline/util.c b/cmdline/util.c index 99fff70c..929a0438 100644 --- a/cmdline/util.c +++ b/cmdline/util.c @@ -1438,16 +1438,54 @@ size_t malloc_counter(void) return mcounter; } +static int malloc_printn(int f, size_t value) +{ + char buf[32]; + int i; + + if (!value) { + return write(2, buf, 2); + } + + i = sizeof(buf); + while (value) { + buf[--i] = (value % 10) + '0'; + value /= 10; + } + + return write(f, buf + i, sizeof(buf) - i); +} + +static void malloc_fail(size_t size) +{ + /* don't use printf to avoid any possible extra allocation */ + /* LCOV_EXCL_START */ + int f = 2; /* stderr */ + + if (write(f, "Low Memory\n", 11) < 0) + return; + if (write(f, "Allocating ", 11) < 0) + return; + if (malloc_printn(f, size) < 0) + return; + if (write(f, " bytes\n", 7) < 0) + return; + if (write(f, "Allocated ", 10) < 0) + return; + if (malloc_printn(f, malloc_counter()) < 0) + return; + if (write(f, " bytes\n", 7) < 0) + return; + /* LCOV_EXCL_STOP */ +} + void* malloc_nofail(size_t size) { void* ptr = malloc(size); if (!ptr) { /* LCOV_EXCL_START */ - /* don't use printf to avoid any possible extra allocation */ - if (write(2, "Low Memory\n", 11) != 11) { - /* ignore error */ - } + malloc_fail(size); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ } @@ -1478,10 +1516,7 @@ char* strdup_nofail(const char* str) if (!ptr) { /* LCOV_EXCL_START */ - /* don't use printf to avoid any possible extra allocation */ - if (write(2, "Low Memory\n", 11) != 11) { - /* ignore error */ - } + malloc_fail(size); exit(EXIT_FAILURE); /* LCOV_EXCL_STOP */ }