[PATCH] lib: fwts_json: fix output so that it is machine parsable (LP: #1902249)
Colin King
colin.king at canonical.com
Fri Nov 20 22:32:21 UTC 2020
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;
--
2.28.0
More information about the fwts-devel
mailing list