Skip to content

Commit c341648

Browse files
committed
Bugfix: RPC: Remove final comma for last entry of fixed-size Arrays and Objects in RPCResult
JSON doesn't allow a trailing comma in Arrays/Objects
1 parent 5236b2e commit c341648

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/rpc/util.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ UniValue JSONRPCTransactionError(TransactionError terr, const std::string& err_s
293293
struct Section {
294294
Section(const std::string& left, const std::string& right)
295295
: m_left{left}, m_right{right} {}
296-
const std::string m_left;
296+
std::string m_left;
297297
const std::string m_right;
298298
};
299299

@@ -645,6 +645,10 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
645645
}
646646
if (m_type == Type::ARR) {
647647
sections.PushSection({indent_next + "...", ""});
648+
} else {
649+
CHECK_NONFATAL(!m_inner.empty());
650+
// Remove final comma, which would be invalid JSON
651+
sections.m_sections.back().m_left.pop_back();
648652
}
649653
sections.PushSection({indent + "]" + maybe_separator, ""});
650654
return;
@@ -658,6 +662,10 @@ void RPCResult::ToSections(Sections& sections, const OuterType outer_type, const
658662
if (m_type == Type::OBJ_DYN) {
659663
// If the dictionary keys are dynamic, use three dots for continuation
660664
sections.PushSection({indent_next + "...", ""});
665+
} else {
666+
CHECK_NONFATAL(!m_inner.empty());
667+
// Remove final comma, which would be invalid JSON
668+
sections.m_sections.back().m_left.pop_back();
661669
}
662670
sections.PushSection({indent + "}" + maybe_separator, ""});
663671
return;

0 commit comments

Comments
 (0)