* [LTP] [PATCH 1/1] docparse: Escape backslash, tab and double quote in JSON
@ 2021-05-03 8:57 Petr Vorel
2021-05-03 11:51 ` Cyril Hrubis
0 siblings, 1 reply; 3+ messages in thread
From: Petr Vorel @ 2021-05-03 8:57 UTC (permalink / raw)
To: ltp
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Hi,
not sure if I should escape more, e.g.:
\b Backspace (ascii code 08)
\f Form feed (ascii code 0C)
\n New line
\r Carriage return
Tested on Richard's BPF patchset [1], which got affected on " + previous
problems on CAN tests, which contained \t in testcases/network/can/filter-tests/can_filter.c
(i.e. with reverted 1cdf8ce8b).
Kind regards,
Petr
[1] https://patchwork.ozlabs.org/project/ltp/list/?series=240772&state=*
[2] https://patchwork.ozlabs.org/project/ltp/patch/20210426120107.6632-2-rpalethorpe@suse.com/
docparse/data_storage.h | 36 ++++++++++++++++++++++++++++++++++--
1 file changed, 34 insertions(+), 2 deletions(-)
diff --git a/docparse/data_storage.h b/docparse/data_storage.h
index ef420c08f..08cdc009d 100644
--- a/docparse/data_storage.h
+++ b/docparse/data_storage.h
@@ -256,14 +256,46 @@ static inline void data_fprintf(FILE *f, unsigned int padd, const char *fmt, ...
va_end(va);
}
-static inline void data_to_json_(struct data_node *self, FILE *f, unsigned int padd, int do_padd)
+static inline void json_escape_(char str[], char search, char replace[])
+{
+ char *tmp;
+ char *p = str;
+ size_t i, str_len, tmp_len, replace_len, shift;
+
+ while ((tmp = strchr(p, search))) {
+ str_len = strlen(str);
+ tmp_len = strlen(tmp);
+ replace_len = strlen(replace);
+ shift = str_len - tmp_len;
+
+ memmove(str + shift + replace_len - 1, str + shift, tmp_len);
+
+ for (i = 0; i < replace_len; i++)
+ str[shift++] = replace[i];
+
+ str[str_len + replace_len - 1] = '\0';
+ p = tmp + replace_len;
+ }
+}
+
+static inline const char *json_escape(char *input)
+{
+ json_escape_(input, '\\', "\\\\");
+ json_escape_(input, '"', "\\\"");
+ json_escape_(input, '\t', "\\t");
+
+ return input;
+}
+
+static inline void data_to_json_(struct data_node *self, FILE *f, unsigned int
+ padd, int do_padd)
{
unsigned int i;
switch (self->type) {
case DATA_STRING:
padd = do_padd ? padd : 0;
- data_fprintf(f, padd, "\"%s\"", self->string.val);
+ data_fprintf(f, padd, "\"%s\"", json_escape((self->string.val)));
break;
case DATA_HASH:
for (i = 0; i < self->hash.elems_used; i++) {
--
2.31.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [LTP] [PATCH 1/1] docparse: Escape backslash, tab and double quote in JSON
2021-05-03 8:57 [LTP] [PATCH 1/1] docparse: Escape backslash, tab and double quote in JSON Petr Vorel
@ 2021-05-03 11:51 ` Cyril Hrubis
2021-05-03 12:38 ` Petr Vorel
0 siblings, 1 reply; 3+ messages in thread
From: Cyril Hrubis @ 2021-05-03 11:51 UTC (permalink / raw)
To: ltp
Hi!
> not sure if I should escape more, e.g.:
> \b Backspace (ascii code 08)
> \f Form feed (ascii code 0C)
> \n New line
> \r Carriage return
>
> Tested on Richard's BPF patchset [1], which got affected on " + previous
> problems on CAN tests, which contained \t in testcases/network/can/filter-tests/can_filter.c
> (i.e. with reverted 1cdf8ce8b).
>
> Kind regards,
> Petr
>
> [1] https://patchwork.ozlabs.org/project/ltp/list/?series=240772&state=*
> [2] https://patchwork.ozlabs.org/project/ltp/patch/20210426120107.6632-2-rpalethorpe@suse.com/
>
> docparse/data_storage.h | 36 ++++++++++++++++++++++++++++++++++--
> 1 file changed, 34 insertions(+), 2 deletions(-)
>
> diff --git a/docparse/data_storage.h b/docparse/data_storage.h
> index ef420c08f..08cdc009d 100644
> --- a/docparse/data_storage.h
> +++ b/docparse/data_storage.h
> @@ -256,14 +256,46 @@ static inline void data_fprintf(FILE *f, unsigned int padd, const char *fmt, ...
> va_end(va);
> }
>
> -static inline void data_to_json_(struct data_node *self, FILE *f, unsigned int padd, int do_padd)
> +static inline void json_escape_(char str[], char search, char replace[])
> +{
> + char *tmp;
> + char *p = str;
> + size_t i, str_len, tmp_len, replace_len, shift;
> +
> + while ((tmp = strchr(p, search))) {
> + str_len = strlen(str);
> + tmp_len = strlen(tmp);
> + replace_len = strlen(replace);
> + shift = str_len - tmp_len;
> +
> + memmove(str + shift + replace_len - 1, str + shift, tmp_len);
> +
> + for (i = 0; i < replace_len; i++)
> + str[shift++] = replace[i];
> +
> + str[str_len + replace_len - 1] = '\0';
> + p = tmp + replace_len;
> + }
> +}
> +
> +static inline const char *json_escape(char *input)
> +{
> + json_escape_(input, '\\', "\\\\");
> + json_escape_(input, '"', "\\\"");
> + json_escape_(input, '\t', "\\t");
> +
> + return input;
> +}
I guess that it would be easier to write a function to print json
string, e.g.
static inline void data_fprintf_esc_(FILE *f, unsigned int padd, const char *str)
{
while (padd-- > 0)
fputc(' ', f);
fputc('"', f);
while (*str) {
switch (*str) {
case '\\':
fputs("\\\\", f);
break;
case '"':
fputs("\\\"", f);
break;
case '\t':
fputs("\\t", f);
break;
default:
putc(*str, f);
break;
}
str++;
}
fputc('"', f);
}
> +static inline void data_to_json_(struct data_node *self, FILE *f, unsigned int
> + padd, int do_padd)
> {
> unsigned int i;
>
> switch (self->type) {
> case DATA_STRING:
> padd = do_padd ? padd : 0;
> - data_fprintf(f, padd, "\"%s\"", self->string.val);
> + data_fprintf(f, padd, "\"%s\"", json_escape((self->string.val)));
> break;
> case DATA_HASH:
> for (i = 0; i < self->hash.elems_used; i++) {
> --
> 2.31.1
>
--
Cyril Hrubis
chrubis@suse.cz
^ permalink raw reply [flat|nested] 3+ messages in thread
* [LTP] [PATCH 1/1] docparse: Escape backslash, tab and double quote in JSON
2021-05-03 11:51 ` Cyril Hrubis
@ 2021-05-03 12:38 ` Petr Vorel
0 siblings, 0 replies; 3+ messages in thread
From: Petr Vorel @ 2021-05-03 12:38 UTC (permalink / raw)
To: ltp
Hi Cyril,
> I guess that it would be easier to write a function to print json
> string, e.g.
> static inline void data_fprintf_esc_(FILE *f, unsigned int padd, const char *str)
> {
> while (padd-- > 0)
> fputc(' ', f);
> fputc('"', f);
> while (*str) {
> switch (*str) {
> case '\\':
> fputs("\\\\", f);
> break;
> case '"':
> fputs("\\\"", f);
> break;
> case '\t':
> fputs("\\t", f);
> break;
> default:
> putc(*str, f);
> break;
> }
> str++;
> }
> fputc('"', f);
> }
That's indeed much better, thanks! I'll send v2 with you as author.
Kind regards,
Petr
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-05-03 12:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-05-03 8:57 [LTP] [PATCH 1/1] docparse: Escape backslash, tab and double quote in JSON Petr Vorel
2021-05-03 11:51 ` Cyril Hrubis
2021-05-03 12:38 ` Petr Vorel
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.