cli: handle OOM by directly streaming output.

We use raw malloc here, again, to handle the failure cases more easily.

I tested it with this hack, then ran the result through `jq --stream '.'`
before and after to make sure it was the same.

    diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c
    index f840c0786..d83555a51 100644
    --- a/cli/lightning-cli.c
    +++ b/cli/lightning-cli.c
    @@ -295,6 +295,14 @@ static void oom_dump(int fd, char *resp, size_t resp_len, size_t off)
     	exit(0);
     }
     
    +static void *xrealloc(void *p, size_t len)
    +{
    +	if (len > 1000000)
    +		return NULL;
    +	return realloc(p, len);
    +}
    +#define realloc xrealloc
    +
     int main(int argc, char *argv[])
     {
     	setup_locale();

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-05-21 16:43:24 +09:30
parent 0b79538b18
commit 4ea1d13077
2 changed files with 80 additions and 10 deletions

View File

@@ -13,6 +13,8 @@ int test_connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
int test_getpid(void);
int test_printf(const char *format, ...);
void *test_malloc(size_t n);
void *test_realloc(void *p, size_t n);
#define main test_main
#define read test_read
@@ -20,6 +22,8 @@ int test_printf(const char *format, ...);
#define connect test_connect
#define getpid test_getpid
#define printf test_printf
#define malloc test_malloc
#define realloc test_realloc
#include "../lightning-cli.c"
#undef main
@@ -55,6 +59,17 @@ int test_printf(const char *fmt UNUSED, ...)
static char *response;
static size_t response_off, max_read_return;
void *test_malloc(size_t n)
{
return tal_arr(response, u8, n);
}
void *test_realloc(void *p, size_t n)
{
tal_resize(&p, n);
return p;
}
ssize_t test_read(int fd UNUSED, void *buf, size_t len)
{
if (len > max_read_return)