From 8f17effeeb604e35501dad7d2213ea62bdb33aaf Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 1 Nov 2017 10:07:33 +1030 Subject: [PATCH] json: just blatt weird characters in string. Don't try to escape them. It's whack-a-mole and they shouldn't do it anyway. Suggested-by: Christian Decker Signed-off-by: Rusty Russell --- common/json.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/common/json.c b/common/json.c index 9f96ca81e..6ae98637d 100644 --- a/common/json.c +++ b/common/json.c @@ -412,16 +412,17 @@ void json_add_literal(struct json_result *result, const char *fieldname, void json_add_string(struct json_result *result, const char *fieldname, const char *value) { - char *escaped = tal_arr(result, char, strlen(value) * 2 + 1); - size_t i, n; + char *escaped = tal_strdup(result, value); + size_t i; json_start_member(result, fieldname); - for (i = n = 0; value[i]; i++) { - if (value[i] == '\\' || value[i] == '"') - escaped[n++] = '\\'; - escaped[n++] = value[i]; + for (i = 0; escaped[i]; i++) { + /* Replace any funny business. Better safe than accurate! */ + if (escaped[i] == '\\' + || escaped[i] == '"' + || !cisprint(escaped[i])) + escaped[i] = '?'; } - escaped[n] = '\0'; result_append_fmt(result, "\"%s\"", escaped); }