Relax JSON parsing

Allow comments and trailing commas
This commit is contained in:
SChernykh 2021-08-29 08:34:26 +02:00
parent 759d2ecc82
commit 519b4b548d
3 changed files with 10 additions and 7 deletions

View file

@ -350,7 +350,7 @@ void p2pool::submit_block() const
[height, diff, &submit_data](const char* data, size_t size)
{
rapidjson::Document doc;
if (doc.Parse(data, size).HasParseError() || !doc.IsObject()) {
if (doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(data, size).HasParseError() || !doc.IsObject()) {
LOGERR(0, "submit_block: invalid JSON response from daemon");
return;
}
@ -542,7 +542,7 @@ void p2pool::get_info()
void p2pool::parse_get_info_rpc(const char* data, size_t size)
{
rapidjson::Document doc;
doc.Parse(data, size);
doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(data, size);
if (!doc.IsObject() || !doc.HasMember("result")) {
LOGWARN(1, "get_info RPC response is invalid (\"result\" not found), trying again in 1 second");
@ -599,7 +599,7 @@ void p2pool::get_miner_data()
void p2pool::parse_get_miner_data_rpc(const char* data, size_t size)
{
rapidjson::Document doc;
doc.Parse(data, size);
doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(data, size);
if (!doc.IsObject() || !doc.HasMember("result")) {
LOGWARN(1, "get_miner_data RPC response is invalid, skipping it");
@ -647,7 +647,7 @@ void p2pool::parse_get_miner_data_rpc(const char* data, size_t size)
bool p2pool::parse_block_header(const char* data, size_t size, ChainMain& result)
{
rapidjson::Document doc;
if (doc.Parse(data, size).HasParseError() || !doc.IsObject()) {
if (doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(data, size).HasParseError() || !doc.IsObject()) {
LOGERR(1, "parse_block_header: invalid JSON response from daemon");
return false;
}
@ -682,7 +682,7 @@ bool p2pool::parse_block_header(const char* data, size_t size, ChainMain& result
uint32_t p2pool::parse_block_headers_range(const char* data, size_t size)
{
rapidjson::Document doc;
if (doc.Parse(data, size).HasParseError() || !doc.IsObject()) {
if (doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(data, size).HasParseError() || !doc.IsObject()) {
LOGERR(1, "parse_block_headers_range: invalid JSON response from daemon");
return 0;
}

View file

@ -1493,7 +1493,7 @@ bool SideChain::load_config(const std::string& filename)
rapidjson::Document doc;
rapidjson::IStreamWrapper s(f);
if (doc.ParseStream(s).HasParseError()) {
if (doc.ParseStream<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(s).HasParseError()) {
LOGERR(1, "failed to parse JSON data in " << filename);
return false;
}

View file

@ -143,7 +143,10 @@ void ZMQReader::parse(char* data, size_t size)
using namespace rapidjson;
Document doc;
doc.Parse(value, end - value);
if (doc.Parse<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(value, end - value).HasParseError()) {
LOGWARN(1, "ZeroMQ message failed to parse, skipping it");
return;
}
if (strcmp(data, "json-minimal-txpool_add") == 0) {
if (!doc.IsArray()) {