common/json_stream: remove useless attempt at oom handling.

We tell membuf to use tal, and tal never returns NULL, so this
code can never be triggered.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-07-03 20:37:20 +09:30 committed by neil saitug
parent 9685c1adaf
commit 0236d4e4da
5 changed files with 24 additions and 73 deletions

View file

@ -1065,8 +1065,7 @@ void json_add_literal(struct json_stream *result, const char *fieldname,
{
/* Literal may contain quotes, so bypass normal checks */
char *dest = json_member_direct(result, fieldname, len);
if (dest)
memcpy(dest, literal, len);
memcpy(dest, literal, len);
}
void json_add_stringn(struct json_stream *result, const char *fieldname,
@ -1100,12 +1099,10 @@ void json_add_hex(struct json_stream *js, const char *fieldname,
char *dest;
dest = json_member_direct(js, fieldname, 1 + hexlen + 1);
if (dest) {
dest[0] = '"';
if (!hex_encode(data, len, dest + 1, hexlen + 1))
abort();
dest[1+hexlen] = '"';
}
dest[0] = '"';
if (!hex_encode(data, len, dest + 1, hexlen + 1))
abort();
dest[1+hexlen] = '"';
}
void json_add_hex_talarr(struct json_stream *result,
@ -1122,11 +1119,9 @@ void json_add_escaped_string(struct json_stream *result, const char *fieldname,
char *dest = json_member_direct(result, fieldname,
1 + strlen(esc->s) + 1);
if (dest) {
dest[0] = '"';
memcpy(dest + 1, esc->s, strlen(esc->s));
dest[1+strlen(esc->s)] = '"';
}
dest[0] = '"';
memcpy(dest + 1, esc->s, strlen(esc->s));
dest[1+strlen(esc->s)] = '"';
if (taken(esc))
tal_free(esc);
}

View file

@ -37,8 +37,7 @@ struct json_stream *json_stream_dup(const tal_t *ctx,
{
struct json_stream *js = tal_dup(ctx, struct json_stream, original);
if (original->jout)
js->jout = json_out_dup(js, original->jout);
js->jout = json_out_dup(js, original->jout);
js->log = log;
return js;
}
@ -61,24 +60,12 @@ void json_stream_log_suppress(struct json_stream *js, const char *cmd_name)
js->log = NULL;
}
/* If we have an allocation failure. */
static void COLD js_oom(struct json_stream *js)
{
js->jout = tal_free(js->jout);
}
void json_stream_append(struct json_stream *js,
const char *str, size_t len)
{
char *dest;
if (!js->jout)
return;
dest = json_out_direct(js->jout, len);
if (!dest) {
js_oom(js);
return;
}
memcpy(dest, str, len);
}
@ -88,9 +75,6 @@ void json_stream_double_cr(struct json_stream *js)
const char *contents;
size_t len, cr_needed;
if (!js->jout)
return;
/* Must be well-formed at this point! */
json_out_finished(js->jout);
@ -131,46 +115,28 @@ char *json_member_direct(struct json_stream *js,
{
char *dest;
if (!js->jout)
return NULL;
dest = json_out_member_direct(js->jout, fieldname, extra);
if (!dest)
js_oom(js);
return dest;
}
void json_array_start(struct json_stream *js, const char *fieldname)
{
if (js->jout && !json_out_start(js->jout, fieldname, '['))
js_oom(js);
json_out_start(js->jout, fieldname, '[');
}
void json_array_end(struct json_stream *js)
{
if (js->jout && !json_out_end(js->jout, ']'))
js_oom(js);
json_out_end(js->jout, ']');
}
void json_object_start(struct json_stream *js, const char *fieldname)
{
if (js->jout && !json_out_start(js->jout, fieldname, '{'))
js_oom(js);
json_out_start(js->jout, fieldname, '{');
}
void json_object_end(struct json_stream *js)
{
if (js->jout && !json_out_end(js->jout, '}'))
js_oom(js);
}
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);
json_out_end(js->jout, '}');
}
void json_add_member(struct json_stream *js,
@ -181,8 +147,7 @@ void json_add_member(struct json_stream *js,
va_list ap;
va_start(ap, fmt);
if (js->jout && !json_out_addv(js->jout, fieldname, quote, fmt, ap))
js_oom(js);
json_out_addv(js->jout, fieldname, quote, fmt, ap);
va_end(ap);
}
@ -194,9 +159,7 @@ void json_add_jsonstr(struct json_stream *js,
size_t len = strlen(jsonstr);
p = json_member_direct(js, fieldname, len);
/* Could be OOM! */
if (p)
memcpy(p, jsonstr, len);
memcpy(p, jsonstr, len);
}
/* This is where we read the json_stream and write it to conn */
@ -205,10 +168,6 @@ static struct io_plan *json_stream_output_write(struct io_conn *conn,
{
const char *p;
/* Out of memory? Nothing we can do but close conn */
if (!js->jout)
return io_close(conn);
/* For when we've just done some output */
json_out_consume(js->jout, js->len_read);

View file

@ -11,7 +11,6 @@ struct io_conn;
struct log;
struct json_stream {
/* NULL if we ran OOM! */
struct json_out *jout;
/* Who is writing to this buffer now; NULL if nobody is. */
@ -73,8 +72,6 @@ void json_object_start(struct json_stream *ks, const char *fieldname);
void json_array_end(struct json_stream *js);
/* '},' */
void json_object_end(struct json_stream *js);
/* ' },' */
void json_object_compat_end(struct json_stream *js);
/**
* json_stream_append - literally insert this string into the json_stream.
@ -117,7 +114,7 @@ void json_add_jsonstr(struct json_stream *js,
* @fieldname: fieldname (if in object), otherwise must be NULL.
* @extra: the space to reserve.
*
* Returns NULL if oom, otherwise returns a ptr to @extra bytes.
* Returns a ptr to @extra bytes.
*/
char *json_member_direct(struct json_stream *js,
const char *fieldname, size_t extra);

View file

@ -459,7 +459,7 @@ struct command_result *command_success(struct command *cmd,
assert(cmd);
assert(cmd->json_stream == result);
json_object_end(result);
json_object_compat_end(result);
json_object_end(result);
return command_raw_complete(cmd, result);
}
@ -470,7 +470,7 @@ struct command_result *command_failed(struct command *cmd,
assert(cmd->json_stream == result);
/* Have to close error */
json_object_end(result);
json_object_compat_end(result);
json_object_end(result);
return command_raw_complete(cmd, result);
}
@ -516,7 +516,7 @@ static void json_command_malformed(struct json_connection *jcon,
json_add_member(js, "code", false, "%" PRIerrcode, JSONRPC2_INVALID_REQUEST);
json_add_string(js, "message", error);
json_object_end(js);
json_object_compat_end(js);
json_object_end(js);
json_stream_close(js, NULL);
}
@ -748,13 +748,13 @@ static void rpc_command_hook_final(struct rpc_command_hook_payload *p STEALS)
if (p->custom_result != NULL) {
struct json_stream *s = json_start(p->cmd);
json_add_jsonstr(s, "result", p->custom_result);
json_object_compat_end(s);
json_object_end(s);
return was_pending(command_raw_complete(p->cmd, s));
}
if (p->custom_error != NULL) {
struct json_stream *s = json_start(p->cmd);
json_add_jsonstr(s, "error", p->custom_error);
json_object_compat_end(s);
json_object_end(s);
return was_pending(command_raw_complete(p->cmd, s));
}
if (p->custom_replace != NULL)

View file

@ -177,7 +177,7 @@ const struct feature_set *plugin_feature_set(const struct plugin *p)
static void jsonrpc_finish_and_send(struct plugin *p, struct json_stream *js)
{
json_object_compat_end(js);
json_object_end(js);
json_stream_close(js, NULL);
ld_send(p, js);
}
@ -228,7 +228,7 @@ static struct command_result *command_complete(struct command *cmd,
struct json_stream *result)
{
/* Global object */
json_object_compat_end(result);
json_object_end(result);
json_stream_close(result, cmd);
ld_send(cmd->plugin, result);
tal_free(cmd);
@ -571,7 +571,7 @@ send_outreq(struct plugin *plugin, const struct out_req *req)
{
/* The "param" object. */
json_object_end(req->js);
json_object_compat_end(req->js);
json_object_end(req->js);
json_stream_close(req->js, req->cmd);
ld_rpc_send(plugin, req->js);