Skip to content

Commit

Permalink
Paranoia checks added for payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueAndi committed Dec 19, 2023
1 parent 399b584 commit eb0d7a0
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 69 deletions.
4 changes: 2 additions & 2 deletions lib/AsyncHttpClient/src/AsyncHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ AsyncHttpClient::AsyncHttpClient() :

memset(&evt, 0, sizeof(evt));
evt.id = EVENT_ID_DATA;
evt.u.data.data = new (std::nothrow) uint8_t[len];
evt.u.data.data = new(std::nothrow) uint8_t[len];

if (nullptr == evt.u.data.data)
{
LOG_ERROR("Couldn't allocate %u memory.", len);

evt.u.data.size = 0U;
}
else
Expand Down
32 changes: 19 additions & 13 deletions lib/BTCQuotePlugin/src/BTCQuotePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ void BTCQuotePlugin::handleAsyncWebResponse(const HttpResponse& rsp)
const char* payload = static_cast<const char*>(vPayload);
const size_t FILTER_SIZE = 128U;
StaticJsonDocument<FILTER_SIZE> filter;
DeserializationError error;

filter["bpi"]["USD"]["rate_float"] = true;
filter["bpi"]["USD"]["rate"] = true;
Expand All @@ -253,24 +252,31 @@ void BTCQuotePlugin::handleAsyncWebResponse(const HttpResponse& rsp)
{
LOG_ERROR("Less memory for filter available.");
}

error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(filter));

if (DeserializationError::Ok != error.code())
else if ((nullptr == payload) ||
(0U == payloadSize))
{
LOG_ERROR("Invalid JSON message received: %s", error.c_str());
LOG_ERROR("No payload.");
}
else
{
Msg msg;
DeserializationError error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(filter));

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
if (DeserializationError::Ok != error.code())
{
delete jsonDoc;
jsonDoc = nullptr;
LOG_ERROR("Invalid JSON message received: %s", error.c_str());
}
else
{
Msg msg;

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
{
delete jsonDoc;
jsonDoc = nullptr;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/FirePlugin/src/FirePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class FirePlugin : public Plugin
*/
static IPluginMaintenance* create(const String& name, uint16_t uid)
{
return new FirePlugin(name, uid);
return new(std::nothrow) FirePlugin(name, uid);
}

/**
Expand Down
32 changes: 19 additions & 13 deletions lib/GrabViaRestPlugin/src/GrabViaRestPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,30 +640,36 @@ void GrabViaRestPlugin::handleAsyncWebResponse(const HttpResponse& rsp)
size_t payloadSize = 0U;
const void* vPayload = rsp.getPayload(payloadSize);
const char* payload = static_cast<const char*>(vPayload);
DeserializationError error;

if (true == m_filter.overflowed())
{
LOG_ERROR("Less memory for filter available.");
}

error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(m_filter));

if (DeserializationError::Ok != error.code())
else if ((nullptr == payload) ||
(0U == payloadSize))
{
LOG_WARNING("JSON parse error: %s", error.c_str());
LOG_ERROR("No payload.");
}
else
{
Msg msg;
DeserializationError error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(m_filter));

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
if (DeserializationError::Ok != error.code())
{
LOG_WARNING("JSON parse error: %s", error.c_str());
}
else
{
delete jsonDoc;
jsonDoc = nullptr;
Msg msg;

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
{
delete jsonDoc;
jsonDoc = nullptr;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/GruenbeckPlugin/src/GruenbeckPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ void GruenbeckPlugin::handleAsyncWebResponse(const HttpResponse& rsp)
char restCapacity[RELEVANT_DATA_LENGTH + 1];
Msg msg;

if (payloadSize >= (START_INDEX_OF_RELEVANT_DATA + RELEVANT_DATA_LENGTH))
if ((nullptr != payload) &&
(payloadSize >= (START_INDEX_OF_RELEVANT_DATA + RELEVANT_DATA_LENGTH)))
{
memcpy(restCapacity, &payload[START_INDEX_OF_RELEVANT_DATA], RELEVANT_DATA_LENGTH);
restCapacity[RELEVANT_DATA_LENGTH] = '\0';
Expand Down
32 changes: 19 additions & 13 deletions lib/OpenWeatherPlugin/src/OpenWeatherPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,32 +881,38 @@ void OpenWeatherPlugin::handleAsyncWebResponse(const HttpResponse& rsp)
const char* payload = static_cast<const char*>(vPayload);
const size_t FILTER_SIZE = 128U;
StaticJsonDocument<FILTER_SIZE> jsonFilterDoc;
DeserializationError error;

m_source->getFilter(jsonFilterDoc);

if (true == jsonFilterDoc.overflowed())
{
LOG_ERROR("Less memory for filter available.");
}

error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(jsonFilterDoc));

if (DeserializationError::Ok != error.code())
else if ((nullptr == payload) ||
(0U == payloadSize))
{
LOG_WARNING("JSON parse error: %s", error.c_str());
LOG_ERROR("No payload.");
}
else
{
Msg msg;
DeserializationError error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(jsonFilterDoc));

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
if (DeserializationError::Ok != error.code())
{
delete jsonDoc;
jsonDoc = nullptr;
LOG_WARNING("JSON parse error: %s", error.c_str());
}
else
{
Msg msg;

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
{
delete jsonDoc;
jsonDoc = nullptr;
}
}
}
}
Expand Down
32 changes: 19 additions & 13 deletions lib/SunrisePlugin/src/SunrisePlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ void SunrisePlugin::handleAsyncWebResponse(const HttpResponse& rsp)
const char* payload = static_cast<const char*>(vPayload);
const size_t FILTER_SIZE = 128U;
StaticJsonDocument<FILTER_SIZE> filter;
DeserializationError error;

/* Example:
* {
Expand Down Expand Up @@ -494,24 +493,31 @@ void SunrisePlugin::handleAsyncWebResponse(const HttpResponse& rsp)
{
LOG_ERROR("Less memory for filter available.");
}

error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(filter));

if (DeserializationError::Ok != error.code())
else if ((nullptr == payload) ||
(0U == payloadSize))
{
LOG_ERROR("Invalid JSON message received: %s", error.c_str());
LOG_ERROR("No payload.");
}
else
{
Msg msg;
DeserializationError error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(filter));

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
if (DeserializationError::Ok != error.code())
{
delete jsonDoc;
jsonDoc = nullptr;
LOG_ERROR("Invalid JSON message received: %s", error.c_str());
}
else
{
Msg msg;

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
{
delete jsonDoc;
jsonDoc = nullptr;
}
}
}
}
Expand Down
32 changes: 19 additions & 13 deletions lib/VolumioPlugin/src/VolumioPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,6 @@ void VolumioPlugin::handleAsyncWebResponse(const HttpResponse& rsp)
const char* payload = static_cast<const char*>(vPayload);
const size_t FILTER_SIZE = 128U;
StaticJsonDocument<FILTER_SIZE> filter;
DeserializationError error;

filter["artist"] = true;
filter["duration"] = true;
Expand All @@ -584,24 +583,31 @@ void VolumioPlugin::handleAsyncWebResponse(const HttpResponse& rsp)
{
LOG_ERROR("Less memory for filter available.");
}

error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(filter));

if (DeserializationError::Ok != error.code())
else if ((nullptr == payload) ||
(0U == payloadSize))
{
LOG_WARNING("JSON parse error: %s", error.c_str());
LOG_ERROR("No payload.");
}
else
{
Msg msg;
DeserializationError error = deserializeJson(*jsonDoc, payload, payloadSize, DeserializationOption::Filter(filter));

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
if (DeserializationError::Ok != error.code())
{
delete jsonDoc;
jsonDoc = nullptr;
LOG_WARNING("JSON parse error: %s", error.c_str());
}
else
{
Msg msg;

msg.type = MSG_TYPE_RSP;
msg.rsp = jsonDoc;

if (false == this->m_taskProxy.send(msg))
{
delete jsonDoc;
jsonDoc = nullptr;
}
}
}
}
Expand Down

0 comments on commit eb0d7a0

Please sign in to comment.