Skip to content

Commit

Permalink
Simple Weather Service : fix out of bounds access while creating Fore…
Browse files Browse the repository at this point in the history
…cast instance.
  • Loading branch information
JF002 committed Dec 10, 2023
1 parent 79da9b0 commit 5836722
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/components/ble/SimpleWeatherService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "SimpleWeatherService.h"
#include <cstring>
#include <nrf_log.h>
#include <array>

using namespace Pinetime::Controllers;

namespace {
Expand All @@ -42,8 +44,10 @@ namespace {
uint64_t timestamp = static_cast<uint64_t>(dataBuffer[2] + (dataBuffer[3] << 8) + (dataBuffer[4] << 16) + (dataBuffer[5] << 24) +
((uint64_t) dataBuffer[6] << 32) + ((uint64_t) dataBuffer[7] << 40) +
((uint64_t) dataBuffer[8] << 48) + ((uint64_t) dataBuffer[9] << 54));
uint8_t nbDays = dataBuffer[10];
std::array<SimpleWeatherService::Forecast::Day, 5> days;

std::array<SimpleWeatherService::Forecast::Day, SimpleWeatherService::MaxNbForecastDays> days;
const uint8_t nbDaysInBuffer = dataBuffer[10];
const uint8_t nbDays = std::min(SimpleWeatherService::MaxNbForecastDays, nbDaysInBuffer);
for (int i = 0; i < nbDays; i++) {
days[i] = SimpleWeatherService::Forecast::Day {dataBuffer[11 + (i * 3)], dataBuffer[12 + (i * 3)], dataBuffer[13 + (i * 3)]};
}
Expand Down
4 changes: 3 additions & 1 deletion src/components/ble/SimpleWeatherService.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace Pinetime {

int OnCommand(struct ble_gatt_access_ctxt* ctxt);

static constexpr uint8_t MaxNbForecastDays = 5;

enum class Icons : uint8_t {
Sun = 0, // ClearSky
CloudsSun = 1, // FewClouds
Expand Down Expand Up @@ -96,7 +98,7 @@ namespace Pinetime {
uint8_t iconId;
};

std::array<Day, 5> days;
std::array<Day, MaxNbForecastDays> days;
};

std::optional<CurrentWeather> Current() const;
Expand Down

0 comments on commit 5836722

Please sign in to comment.