Browse Source

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 <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
8f17effeeb
  1. 15
      common/json.c

15
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) void json_add_string(struct json_result *result, const char *fieldname, const char *value)
{ {
char *escaped = tal_arr(result, char, strlen(value) * 2 + 1); char *escaped = tal_strdup(result, value);
size_t i, n; size_t i;
json_start_member(result, fieldname); json_start_member(result, fieldname);
for (i = n = 0; value[i]; i++) { for (i = 0; escaped[i]; i++) {
if (value[i] == '\\' || value[i] == '"') /* Replace any funny business. Better safe than accurate! */
escaped[n++] = '\\'; if (escaped[i] == '\\'
escaped[n++] = value[i]; || escaped[i] == '"'
|| !cisprint(escaped[i]))
escaped[i] = '?';
} }
escaped[n] = '\0';
result_append_fmt(result, "\"%s\"", escaped); result_append_fmt(result, "\"%s\"", escaped);
} }

Loading…
Cancel
Save