mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user