common/json_stream: use json_out_addstrn for better efficiency.
What changed, and why it matters
This is a small internal code cleanup in how JSON strings are added to output streams. It replaces a less efficient formatting-based path with a direct string-copying helper, and consolidates two similar functions. There is no user-visible behavior change and no security relevance in the diff itself.
No security action required; treat as normal code-quality refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors common/json_stream.c so that json_add_string() calls a new json_add_stringn() which directly invokes json_out_addstrn(), instead of the old json_add_stringn() implementation that used json_add_str_fmt() with a ‘%.*s’ format. The old json_add_stringn() is removed and reimplemented inline above json_add_string. This is purely an efficiency/cleanup change; the public API and produced JSON output remain the same.
Changed components
common/json_stream.cInspect captured patch +12 / −10
diff --git a/common/json_stream.c b/common/json_stream.c
index 3daefe46..d1ec9d41 100644
--- a/common/json_stream.c
+++ b/common/json_stream.c
@@ -193,13 +193,22 @@ void json_add_primitive(struct json_stream *js,
tal_free_if_taken(val);
}
+void json_add_stringn(struct json_stream *js,
+ const char *fieldname,
+ const char *str TAKES,
+ size_t len)
+{
+ if (json_filter_ok(js->filter, fieldname))
+ json_out_addstrn(js->jout, fieldname, str, len);
+ if (taken(str))
+ tal_free(str);
+}
+
void json_add_string(struct json_stream *js,
const char *fieldname,
const char *str TAKES)
{
- if (json_filter_ok(js->filter, fieldname))
- json_out_addstr(js->jout, fieldname, str);
- tal_free_if_taken(str);
+ json_add_stringn(js, fieldname, str, strlen(str));
}
static char *json_member_direct(struct json_stream *js,
@@ -298,13 +307,6 @@ void json_add_s32(struct json_stream *result, const char *fieldname,
json_add_primitive_fmt(result, fieldname, "%d", value);
}
-void json_add_stringn(struct json_stream *result, const char *fieldname,
- const char *value TAKES, size_t value_len)
-{
- json_add_str_fmt(result, fieldname, "%.*s", (int)value_len, value);
- tal_free_if_taken(value);
-}
-
void json_add_bool(struct json_stream *result, const char *fieldname, bool value)
{
json_add_primitive(result, fieldname, value ? "true" : "false");
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.