[PATCH] mongoose: Escape JSON strings

38 views
Skip to first unread message

Joshua Watt

unread,
Jul 15, 2021, 10:15:59 AM7/15/21
to swup...@googlegroups.com, Joshua Watt
Adds escaping of JSON strings which prevents errors when there are
quotes in the encapsulated string

Signed-off-by: Joshua Watt <JPEWh...@gmail.com>
---
mongoose/mongoose_interface.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/mongoose/mongoose_interface.c b/mongoose/mongoose_interface.c
index 5dc99f1..b6da93d 100644
--- a/mongoose/mongoose_interface.c
+++ b/mongoose/mongoose_interface.c
@@ -183,6 +183,7 @@ static void *broadcast_progress_thread(void *data)
struct mg_mgr *mgr = (struct mg_mgr *) data;
struct progress_msg msg;
char str[512];
+ char escaped[512];
int ret;

if (fd < 0)
@@ -202,12 +203,14 @@ static void *broadcast_progress_thread(void *data)
if (msg.status != status || msg.status == FAILURE) {
status = msg.status;

+ snescape(escaped, sizeof(escaped), get_status_string(msg.status));
+
snprintf(str, sizeof(str),
"{\r\n"
"\t\"type\": \"status\",\r\n"
"\t\"status\": \"%s\"\r\n"
"}\r\n",
- get_status_string(msg.status));
+ escaped);
broadcast(mgr, str);
}

@@ -230,12 +233,14 @@ static void *broadcast_progress_thread(void *data)
}

if (msg.infolen) {
+ snescape(escaped, sizeof(escaped), msg.info);
+
snprintf(str, sizeof(str),
"{\r\n"
"\t\"type\": \"info\",\r\n"
"\t\"source\": \"%s\"\r\n"
"}\r\n",
- msg.info);
+ escaped);
broadcast(mgr, str);
}

@@ -244,6 +249,8 @@ static void *broadcast_progress_thread(void *data)
step = msg.cur_step;
percent = msg.cur_percent;

+ snescape(escaped, sizeof(escaped), msg.cur_step ? msg.cur_image: "");
+
snprintf(str, sizeof(str),
"{\r\n"
"\t\"type\": \"step\",\r\n"
@@ -254,7 +261,7 @@ static void *broadcast_progress_thread(void *data)
"}\r\n",
msg.nsteps,
msg.cur_step,
- msg.cur_step ? msg.cur_image: "",
+ escaped,
msg.cur_percent);
broadcast(mgr, str);
}
--
2.32.0

Stefano Babic

unread,
Jul 20, 2021, 6:21:34 AM7/20/21
to Joshua Watt, swup...@googlegroups.com
Hi Joshua,
Acked-by: Stefano Babic <sba...@denx.de>

Best regards,
Stefano Babic

--
=====================================================================
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sba...@denx.de
=====================================================================
Reply all
Reply to author
Forward
0 new messages