Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uncaught exception when invalid RpcMessage was received #504

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions libshvchainpack/src/rpcdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void RpcDriver::sendRpcFrame(RpcFrame &&frame)
frame = msg.toToRpcFrame(m_clientProtocolType);
}
auto frame_data = frame.toFrameData();
logRpcData().nospace() << "FRAME DATA WRITE " << frame_data.size() << " bytes of data:\n" << shv::chainpack::utils::hexDump(frame_data);
//logRpcData().nospace() << "FRAME DATA WRITE " << frame_data.size() << " bytes of data:\n" << shv::chainpack::utils::hexDump(frame_data);
writeFrameData(frame_data);
}
catch (const std::exception &e) {
Expand All @@ -59,11 +59,6 @@ void RpcDriver::sendRpcFrame(RpcFrame &&frame)

void RpcDriver::onFrameDataRead(const std::string &frame_data)
{
#ifdef _MSC_VER
logRpcData() << __FUNCSIG__ << "+++++++++++++++++++++++++++++++++";
#else
logRpcData() << __PRETTY_FUNCTION__ << "+++++++++++++++++++++++++++++++++";
#endif
logRpcData().nospace() << "FRAME DATA READ " << frame_data.size() << " bytes of data read:\n" << shv::chainpack::utils::hexDump(frame_data);
try {
auto frame = RpcFrame::fromFrameData(frame_data);
Expand Down
23 changes: 19 additions & 4 deletions libshvchainpack/src/rpcmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,24 @@ void RpcMessage::MetaType::registerMetaType()
//==================================================================
RpcMessage RpcFrame::toRpcMessage(std::string *errmsg) const
{
auto make_rpcmsg = [](const RpcValue &val, std::string *err_msg) {
try {
return RpcMessage(val);
}
catch (const std::exception &e) {
if (err_msg) {
*err_msg = e.what();
}
}
return RpcMessage();
};
switch (protocol) {
case Rpc::ProtocolType::ChainPack: {
auto val = RpcValue::fromChainPack(data, errmsg);
if (!errmsg || (errmsg && errmsg->empty())) {
auto m = meta;
val.setMetaData(std::move(m));
return RpcMessage(val);
return make_rpcmsg(val, errmsg);
}
break;
}
Expand All @@ -67,17 +78,21 @@ RpcMessage RpcFrame::toRpcMessage(std::string *errmsg) const
if (!errmsg || (errmsg && errmsg->empty())) {
auto m = meta;
val.setMetaData(std::move(m));
return RpcMessage(val);
return make_rpcmsg(val, errmsg);
}
break;
}
default: {
constexpr auto msg = "Invalid protocol type";
if (errmsg) {
*errmsg = "Invalid protocol type";
*errmsg = msg;
}
else {
throw std::runtime_error(msg);
}
return {};
}
}

return {};
}

Expand Down
4 changes: 3 additions & 1 deletion libshviotqt/src/rpc/serialportsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define logSerialPortSocketM() nCMessage("SerialPortSocket")
#define logSerialPortSocketW() nCWarning("SerialPortSocket")

#define logRpcData() nCMessage("RpcData")

using namespace std;

namespace shv::iotqt::rpc {
Expand All @@ -38,7 +40,7 @@ SerialFrameReader::SerialFrameReader(CrcCheck crc)

QList<int> SerialFrameReader::addData(std::string_view data)
{
shvDebug() << "===> received:" << data.size() << "bytes:" << chainpack::utils::hexArray(data.data(), data.size());
logRpcData().nospace() << "FRAME DATA READ " << data.size() << " bytes of data read:\n" << shv::chainpack::utils::hexDump(data);
QList<int> response_request_ids;
auto check_response_id = [this, &response_request_ids]() {
std::istringstream in(m_readBuffer);
Expand Down
4 changes: 3 additions & 1 deletion libshviotqt/src/rpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <QWebSocket>
#endif

#define logRpcData() nCMessage("RpcData")

namespace shv::iotqt::rpc {

//======================================================
Expand Down Expand Up @@ -102,10 +104,10 @@ int FrameReader::tryToReadMeta(std::istringstream &in)
//======================================================
QList<int> StreamFrameReader::addData(std::string_view data)
{
logRpcData().nospace() << "FRAME DATA READ " << data.size() << " bytes of data read:\n" << shv::chainpack::utils::hexDump(data);
using namespace chainpack;
QList<int> response_request_ids;
m_readBuffer += data;
shvDebug() << "received:\n" << utils::hexDump(data);
while (true) {
std::istringstream in(m_readBuffer);
int err_code;
Expand Down