Skip to content

Commit

Permalink
agents: fix exit message format
Browse files Browse the repository at this point in the history
The `message` and `stack` fields should be properly escaped.

PR-URL: #45
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
santigimeno committed Dec 8, 2023
1 parent 367fe4c commit c0ed8e7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions agents/zmq/src/zmq_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const char MSG_6[] = "{"
",\"command\":\"exit\""
",\"exit_code\":%d"
",\"version\":%d"
",\"error\":{\"message\":\"%s\",\"stack\":\"%s\",\"code\":%d}"
",\"error\":{\"message\":%s,\"stack\":%s,\"code\":%d}"
"}";

const char MSG_7[] = "{"
Expand All @@ -144,7 +144,7 @@ const char MSG_8[] = "{"
",\"command\":\"exit\""
",\"exit_code\":%d"
",\"version\":%d"
",\"error\":{\"message\":\"%s\",\"stack\":\"%s\",\"code\":%d}"
",\"error\":{\"message\":%s,\"stack\":%s,\"code\":%d}"
",\"profile\":%s"
"}";

Expand Down Expand Up @@ -1843,15 +1843,18 @@ void ZmqAgent::send_exit() {
last_main_profile_.c_str());
}
} else {
// Use nlohmann::json to properly escape the error message and stack.
nlohmann::json jmsg(std::get<0>(*error));
nlohmann::json jstack(std::get<1>(*error));
if (last_main_profile_.empty()) {
r = snprintf(msg_buf_,
msg_size_,
MSG_6,
agent_id_.c_str(),
exit_code,
version_,
std::get<0>(*error).c_str(),
std::get<1>(*error).c_str(),
jmsg.dump().c_str(),
jstack.dump().c_str(),
500);
} else {
r = snprintf(msg_buf_,
Expand All @@ -1860,8 +1863,8 @@ void ZmqAgent::send_exit() {
agent_id_.c_str(),
exit_code,
version_,
std::get<0>(*error).c_str(),
std::get<1>(*error).c_str(),
jmsg.dump().c_str(),
jstack.dump().c_str(),
500,
last_main_profile_.c_str());
}
Expand Down

0 comments on commit c0ed8e7

Please sign in to comment.