2018-11-20 02:46:32 +01:00
|
|
|
#include <ccan/io/io.h>
|
|
|
|
/* To reach into io_plan: not a public header! */
|
|
|
|
#include <ccan/io/backend.h>
|
2019-06-12 02:38:55 +02:00
|
|
|
#include <ccan/json_escape/json_escape.h>
|
2019-06-12 02:38:55 +02:00
|
|
|
#include <ccan/json_out/json_out.h>
|
2019-04-08 11:58:44 +02:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2019-06-12 02:38:55 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
#include <common/daemon.h>
|
2018-11-20 02:46:32 +01:00
|
|
|
#include <common/utils.h>
|
|
|
|
#include <lightningd/json.h>
|
|
|
|
#include <lightningd/json_stream.h>
|
2019-02-18 03:44:29 +01:00
|
|
|
#include <lightningd/log.h>
|
2018-11-20 02:46:32 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
struct json_stream {
|
2019-06-12 02:38:55 +02:00
|
|
|
/* NULL if we ran OOM! */
|
|
|
|
struct json_out *jout;
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* Who is writing to this buffer now; NULL if nobody is. */
|
|
|
|
struct command *writer;
|
|
|
|
|
|
|
|
/* Who is io_writing from this buffer now: NULL if nobody is. */
|
|
|
|
struct io_conn *reader;
|
2018-11-20 02:46:32 +01:00
|
|
|
struct io_plan *(*reader_cb)(struct io_conn *conn,
|
|
|
|
struct json_stream *js,
|
|
|
|
void *arg);
|
2018-11-20 02:46:32 +01:00
|
|
|
void *reader_arg;
|
|
|
|
size_t len_read;
|
|
|
|
|
2019-02-18 03:44:29 +01:00
|
|
|
/* Where to log I/O */
|
|
|
|
struct log *log;
|
2018-11-20 02:46:32 +01:00
|
|
|
};
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
static void adjust_io_write(struct json_out *jout,
|
|
|
|
ptrdiff_t delta,
|
|
|
|
struct json_stream *js)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
/* If io_write is in progress, we shift it to point to new buffer pos */
|
|
|
|
if (js->reader)
|
|
|
|
/* FIXME: This, or something prettier (io_replan?) belong in ccan/io! */
|
|
|
|
js->reader->plan[IO_OUT].arg.u1.cp += delta;
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
2019-02-18 03:44:29 +01:00
|
|
|
struct json_stream *new_json_stream(const tal_t *ctx,
|
|
|
|
struct command *writer,
|
|
|
|
struct log *log)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
|
|
|
struct json_stream *js = tal(ctx, struct json_stream);
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
/* FIXME: Add magic so tal_resize can fail! */
|
|
|
|
js->jout = json_out_new(js);
|
|
|
|
json_out_call_on_move(js->jout, adjust_io_write, js);
|
2018-11-20 02:46:32 +01:00
|
|
|
js->writer = writer;
|
|
|
|
js->reader = NULL;
|
2019-02-18 03:44:29 +01:00
|
|
|
js->log = log;
|
2018-11-20 02:46:32 +01:00
|
|
|
return js;
|
|
|
|
}
|
|
|
|
|
2019-05-31 04:01:07 +02:00
|
|
|
struct json_stream *json_stream_dup(const tal_t *ctx,
|
|
|
|
struct json_stream *original,
|
|
|
|
struct log *log)
|
2018-12-12 19:09:06 +01:00
|
|
|
{
|
|
|
|
struct json_stream *js = tal_dup(ctx, struct json_stream, original);
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
if (original->jout)
|
|
|
|
js->jout = json_out_dup(js, original->jout);
|
2019-05-31 04:01:07 +02:00
|
|
|
js->log = log;
|
2018-12-12 19:09:06 +01:00
|
|
|
return js;
|
|
|
|
}
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
bool json_stream_still_writing(const struct json_stream *js)
|
|
|
|
{
|
|
|
|
return js->writer != NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-23 12:09:17 +02:00
|
|
|
void json_stream_log_suppress(struct json_stream *js, const char *cmd_name)
|
|
|
|
{
|
|
|
|
/* Really shouldn't be used for anything else */
|
|
|
|
assert(streq(cmd_name, "getlog"));
|
|
|
|
js->log = NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
/* If we have an allocation failure. */
|
|
|
|
static void COLD js_oom(struct json_stream *js)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
js->jout = tal_free(js->jout);
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
void json_stream_append(struct json_stream *js,
|
|
|
|
const char *str, size_t len)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
char *dest;
|
|
|
|
|
|
|
|
if (!js->jout)
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
return;
|
2019-06-12 02:38:55 +02:00
|
|
|
dest = json_out_direct(js->jout, len);
|
|
|
|
if (!dest) {
|
|
|
|
js_oom(js);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(dest, str, len);
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
void json_stream_close(struct json_stream *js, struct command *writer)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
/* FIXME: We use writer == NULL for malformed: make writer a void *?
|
|
|
|
* I used to assert(writer); here. */
|
|
|
|
assert(js->writer == writer);
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
/* Should be well-formed at this point! */
|
|
|
|
json_out_finished(js->jout);
|
2019-06-12 02:38:55 +02:00
|
|
|
json_stream_append(js, "\n\n", strlen("\n\n"));
|
|
|
|
json_stream_flush(js);
|
|
|
|
js->writer = NULL;
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
/* Also called when we're oom, so it will kill reader. */
|
|
|
|
void json_stream_flush(struct json_stream *js)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
/* Wake the stream reader. FIXME: Could have a flag here to optimize */
|
|
|
|
io_wake(js);
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
char *json_member_direct(struct json_stream *js,
|
|
|
|
const char *fieldname, size_t extra)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
2019-04-08 11:58:44 +02:00
|
|
|
char *dest;
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
if (!js->jout)
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
return NULL;
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
dest = json_out_member_direct(js->jout, fieldname, extra);
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
if (!dest)
|
2019-06-12 02:38:55 +02:00
|
|
|
js_oom(js);
|
2019-04-08 11:58:44 +02:00
|
|
|
return dest;
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_array_start(struct json_stream *js, const char *fieldname)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
if (js->jout && !json_out_start(js->jout, fieldname, '['))
|
|
|
|
js_oom(js);
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_array_end(struct json_stream *js)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
if (js->jout && !json_out_end(js->jout, ']'))
|
|
|
|
js_oom(js);
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_object_start(struct json_stream *js, const char *fieldname)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
if (js->jout && !json_out_start(js->jout, fieldname, '{'))
|
|
|
|
js_oom(js);
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void json_object_end(struct json_stream *js)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
if (js->jout && !json_out_end(js->jout, '}'))
|
|
|
|
js_oom(js);
|
2018-11-20 02:46:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-17 07:52:26 +02:00
|
|
|
void json_object_compat_end(struct json_stream *js)
|
|
|
|
{
|
|
|
|
/* In 0.7.1 we upgraded pylightning to no longer need this. */
|
|
|
|
#ifdef COMPAT_V070
|
|
|
|
json_stream_append(js, " ", 1);
|
|
|
|
#endif
|
|
|
|
json_object_end(js);
|
|
|
|
}
|
|
|
|
|
2019-06-12 02:38:55 +02:00
|
|
|
void json_add_member(struct json_stream *js,
|
|
|
|
const char *fieldname,
|
|
|
|
bool quote,
|
|
|
|
const char *fmt, ...)
|
2018-11-20 02:46:32 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2019-06-12 02:38:55 +02:00
|
|
|
if (js->jout && !json_out_addv(js->jout, fieldname, quote, fmt, ap))
|
|
|
|
js_oom(js);
|
2018-11-20 02:46:32 +01:00
|
|
|
va_end(ap);
|
2019-04-08 11:58:44 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* This is where we read the json_stream and write it to conn */
|
|
|
|
static struct io_plan *json_stream_output_write(struct io_conn *conn,
|
|
|
|
struct json_stream *js)
|
|
|
|
{
|
2019-06-12 02:38:55 +02:00
|
|
|
const char *p;
|
|
|
|
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
/* Out of memory? Nothing we can do but close conn */
|
2019-06-12 02:38:55 +02:00
|
|
|
if (!js->jout)
|
lightningd: hang up on clients if they make us run out of memory.
This happened with the 800M JSON for the MCP listchannels on the raspberry
pi, and tal calls abort() by default.
We switch to raw malloc here; we could override the error hook for
tal, but this is neater since we're doing low-level things anyway,
I tested it manually with this patch:
diff --git a/lightningd/json_stream.c b/lightningd/json_stream.c
index cec9f5771..206ba37c0 100644
--- a/lightningd/json_stream.c
+++ b/lightningd/json_stream.c
@@ -43,6 +43,14 @@ static void free_json_stream_membuf(struct json_stream *js)
free(membuf_cleanup(&js->outbuf));
}
+static void *membuf_realloc_hack(struct membuf *mb, void *rawelems,
+ size_t newsize)
+{
+ if (newsize > 1000000000)
+ return NULL;
+ return realloc(rawelems, newsize);
+}
+
struct json_stream *new_json_stream(const tal_t *ctx,
struct command *writer,
struct log *log)
@@ -53,7 +61,7 @@ struct json_stream *new_json_stream(const tal_t *ctx,
js->reader = NULL;
/* We don't use tal here, because we handle failure externally (tal
* helpfully aborts with a msg, which is usually right) */
- membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc);
+ membuf_init(&js->outbuf, malloc(64), 64, membuf_realloc_hack);
tal_add_destructor(js, free_json_stream_membuf);
#if DEVELOPER
js->wrapping = tal_arr(js, jsmntype_t, 0);
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2019-05-16 21:57:17 +02:00
|
|
|
return io_close(conn);
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* For when we've just done some output */
|
2019-06-12 02:38:55 +02:00
|
|
|
json_out_consume(js->jout, js->len_read);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
|
|
|
/* Get how much we can write out from js */
|
2019-06-12 02:38:55 +02:00
|
|
|
p = json_out_contents(js->jout, &js->len_read);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
|
|
|
/* Nothing in buffer? */
|
2019-06-12 02:38:55 +02:00
|
|
|
if (!p) {
|
2018-11-20 02:46:32 +01:00
|
|
|
/* We're not doing io_write now, unset. */
|
|
|
|
js->reader = NULL;
|
|
|
|
if (!json_stream_still_writing(js))
|
2018-11-20 02:46:32 +01:00
|
|
|
return js->reader_cb(conn, js, js->reader_arg);
|
2018-11-20 02:46:32 +01:00
|
|
|
return io_out_wait(conn, js, json_stream_output_write, js);
|
|
|
|
}
|
|
|
|
|
|
|
|
js->reader = conn;
|
2019-04-08 01:52:19 +02:00
|
|
|
if (js->log)
|
2019-11-17 12:41:33 +01:00
|
|
|
log_io(js->log, LOG_IO_OUT, NULL, "", p, js->len_read);
|
2018-11-20 02:46:32 +01:00
|
|
|
return io_write(conn,
|
2019-06-12 02:38:55 +02:00
|
|
|
p, js->len_read,
|
2018-11-20 02:46:32 +01:00
|
|
|
json_stream_output_write, js);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct io_plan *json_stream_output_(struct json_stream *js,
|
|
|
|
struct io_conn *conn,
|
|
|
|
struct io_plan *(*cb)(struct io_conn *conn,
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_stream *js,
|
2018-11-20 02:46:32 +01:00
|
|
|
void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
assert(!js->reader);
|
|
|
|
|
|
|
|
js->reader_cb = cb;
|
|
|
|
js->reader_arg = arg;
|
|
|
|
|
|
|
|
js->len_read = 0;
|
|
|
|
return json_stream_output_write(conn, js);
|
|
|
|
}
|