ACK: [PATCH] lib: fwts_json: fix output so that it is machine parsable (LP: #1902249)
Alex Hung
alex.hung at canonical.com
Mon Nov 23 07:21:31 UTC 2020
On 2020-11-20 3:32 p.m., Colin King wrote:
> From: Colin Ian King <colin.king at canonical.com>
>
> There is still an issue when escaped chars such as \n are being converted
> to a json string. Fix the escape char by converting the \n to \\ and n
> for the various escape chars.
>
> Signed-off-by: Colin Ian King <colin.king at canonical.com>
> ---
> src/lib/src/fwts_json.c | 27 +++++++++++++++++++++------
> 1 file changed, 21 insertions(+), 6 deletions(-)
>
> diff --git a/src/lib/src/fwts_json.c b/src/lib/src/fwts_json.c
> index c92dd512..16735b95 100644
> --- a/src/lib/src/fwts_json.c
> +++ b/src/lib/src/fwts_json.c
> @@ -755,20 +755,30 @@ static char *str_indent(char *str, int indent)
> return str_append(str, buf);
> }
>
> -static bool char_escape(const int ch)
> +/*
> + * char_escape()
> + * convert escape char to the unescaped char to
> + * be prefixed by \\. If not an escape char, return 0
> + */
> +static inline int char_escape(const int ch)
> {
> switch (ch) {
> case '"':
> + return '"';
> case '\b':
> + return 'b';
> case '\f':
> + return 'f';
> case '\n':
> + return 'n';
> case '\r':
> + return 'r';
> case '\t':
> - return true;
> + return 't';
> default:
> break;
> }
> - return false;
> + return 0;
> }
>
> static char *str_escape(char *oldstr)
> @@ -784,10 +794,15 @@ static char *str_escape(char *oldstr)
> if (!newstr)
> return NULL;
>
> - for (oldptr = oldstr, newptr = newstr; *oldptr; oldptr++, newptr++) {
> - if (char_escape(*oldptr))
> + for (oldptr = oldstr, newptr = newstr; *oldptr; oldptr++) {
> + const int esc = char_escape(*oldptr);
> +
> + if (esc) {
> *(newptr++) = '\\';
> - *newptr = *oldptr;
> + *(newptr++) = esc;
> + } else {
> + *(newptr++) = *oldptr;
> + }
> }
> *newptr = '\0';
> return newstr;
>
Acked-by: Alex Hung <alex.hung at canonical.com>
More information about the fwts-devel
mailing list