Skip to content

Commit

Permalink
WeatherService: Prevent adding of expired events
Browse files Browse the repository at this point in the history
Before further parsing of the packet, check timestamp and expires field
right after it has been decoded to avoid unnecessary allocs/frees. Also
perform tidying up of the vector before attempting to add any new
elements.
  • Loading branch information
faxe1008 committed Aug 7, 2023
1 parent 4d39173 commit 46c5641
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/ble/weather/WeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ namespace Pinetime {
CleanUpQcbor(&decodeContext);
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}

// Do not add already expired events
uint64_t currentTimestamp = GetCurrentUnixTimestamp();
if (static_cast<uint64_t>(tmpTimestamp + tmpExpires) < currentTimestamp) {
CleanUpQcbor(&decodeContext);
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}

int64_t tmpEventType = 0;
QCBORDecode_GetInt64InMapSZ(&decodeContext, "EventType", &tmpEventType);
if (QCBORDecode_GetError(&decodeContext) != QCBOR_SUCCESS || tmpEventType < 0 ||
Expand All @@ -74,6 +82,8 @@ namespace Pinetime {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}

TidyTimeline();

switch (static_cast<WeatherData::eventtype>(tmpEventType)) {
case WeatherData::eventtype::AirQuality: {
std::unique_ptr<WeatherData::AirQuality> airquality = std::make_unique<WeatherData::AirQuality>();
Expand Down Expand Up @@ -364,8 +374,6 @@ namespace Pinetime {
}

QCBORDecode_ExitMap(&decodeContext);
GetTimelineLength();
TidyTimeline();

if (QCBORDecode_Finish(&decodeContext) != QCBOR_SUCCESS) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
Expand Down

0 comments on commit 46c5641

Please sign in to comment.