Skip to content

Commit 3e81c08

Browse files
authored
fix: Check if all required fields are present in json payload before reading them (#51)
1 parent cfc6d23 commit 3e81c08

File tree

11 files changed

+69
-5
lines changed

11 files changed

+69
-5
lines changed

.transport.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.2
1+
1.1.3

CHANGELOG.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
## [0.5.1](https://github.com/rdkcentral/firebolt-cpp-client/compare/v0.5.0...v0.5.1)
22

33
### Changed
4-
- **Breaking**: In v0.5.0, `AgePolicy` was moved to the `Firebolt` namespace and to the `common_types.h` header file. References must be changed
5-
from `Firebolt::Discovery::AgePolicy` to `Firebolt::AgePolicy`, and the include must be added (`#include <firebolt/common_types.h>`).
4+
- Updated Firebolt C++ Transport dependency to `v1.1.3`
5+
6+
### Fixed
7+
- Prevented a crash when a mandatory field was missing from the JSON payload
68

79
## [0.5.0](https://github.com/rdkcentral/firebolt-cpp-client/compare/v0.4.0...v0.5.0)
810

@@ -18,6 +20,8 @@
1820
- **Breaking**: Aligned types with the latest specification updates:
1921
- `integer` changed to unsigned (`uint32_t`)
2022
- `float` changed to `double`
23+
- **Breaking**: `AgePolicy` was moved to the `Firebolt` namespace and to the `common_types.h` header file. References must be changed
24+
from `Firebolt::Discovery::AgePolicy` to `Firebolt::AgePolicy`, and the include must be added (`#include <firebolt/common_types.h>`).
2125
- Specify the build version and ABI version
2226
- Updated Firebolt C++ Transport dependency to `v1.1.2`
2327

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ else()
8080
FetchContent_Declare(
8181
FireboltTransport
8282
URL "https://github.com/rdkcentral/firebolt-cpp-transport/releases/download/v${FIREBOLT_TRANSPORT_VERSION}/firebolt-cpp-transport-${FIREBOLT_TRANSPORT_VERSION}.tar.gz"
83-
URL_HASH "SHA256=892f08b1c532fc5ac0029d759612dd2dc1b521d43882a0f70c22a02ff6ca05e7"
83+
URL_HASH "SHA256=0a0c9394395a514d3ba6931a6c656886523c6b3f038b0cb991c27c837f0e506e"
8484
DOWNLOAD_EXTRACT_TIMESTAMP true
8585
)
8686
set(ENABLE_TESTS_ORIG ${ENABLE_TESTS})

src/json_types/accessibility.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class ClosedCaptionsSettings : public Firebolt::JSON::NL_Json_Basic<::Firebolt::
2929
public:
3030
void fromJson(const nlohmann::json& json) override
3131
{
32+
if (!checkRequiredFields(json, {"enabled", "preferredLanguages"}))
33+
{
34+
throw std::invalid_argument("Missing required fields in JSON");
35+
}
3236
enabled_ = json["enabled"].get<bool>();
3337
preferredLanguages_ = json["preferredLanguages"].get<std::vector<std::string>>();
3438
}
@@ -47,6 +51,10 @@ class VoiceGuidanceSettings : public Firebolt::JSON::NL_Json_Basic<::Firebolt::A
4751
public:
4852
void fromJson(const nlohmann::json& json) override
4953
{
54+
if (!checkRequiredFields(json, {"enabled", "rate", "navigationHints"}))
55+
{
56+
throw std::invalid_argument("Missing required fields in JSON");
57+
}
5058
enabled_ = json["enabled"].get<bool>();
5159
rate_ = json["rate"].get<double>();
5260
navigationHints_ = json["navigationHints"].get<bool>();

src/json_types/advertising.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class IfaJson : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Advertising::If
3030
public:
3131
void fromJson(const nlohmann::json& json) override
3232
{
33+
if (!checkRequiredFields(json, {"ifa", "ifa_type", "lmt"}))
34+
{
35+
throw std::invalid_argument("Missing required fields in JSON");
36+
}
3337
ifa = json["ifa"].get<std::string>();
3438
ifa_type = json["ifa_type"].get<std::string>();
3539
lmt = json["lmt"].get<std::string>();

src/json_types/device.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class HDRFormat : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Device::HDRFo
4747
public:
4848
void fromJson(const nlohmann::json& json) override
4949
{
50+
if (!checkRequiredFields(json, {"hdr10", "hdr10Plus", "dolbyVision", "hlg"}))
51+
{
52+
throw std::invalid_argument("Missing required fields in JSON");
53+
}
5054
hdrFormat_.hdr10 = json["hdr10"].get<bool>();
5155
hdrFormat_.hdr10Plus = json["hdr10Plus"].get<bool>();
5256
hdrFormat_.dolbyVision = json["dolbyVision"].get<bool>();

src/json_types/display.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ class DisplaySizeJson : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Display
3131
public:
3232
void fromJson(const nlohmann::json& json) override
3333
{
34+
if (!checkRequiredFields(json, {"width", "height"}))
35+
{
36+
throw std::invalid_argument("Missing required fields in JSON");
37+
}
3438
width_ = json["width"].get<uint32_t>();
3539
height_ = json["height"].get<uint32_t>();
3640
}

src/json_types/lifecycle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class StateChange : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Lifecycle::
5555
public:
5656
void fromJson(const nlohmann::json& json) override
5757
{
58+
if (!checkRequiredFields(json, {"oldState", "newState"}))
59+
{
60+
throw std::invalid_argument("Missing required fields in JSON");
61+
}
5862
oldState_ = LifecycleStateEnum.at(json["oldState"]);
5963
newState_ = LifecycleStateEnum.at(json["newState"]);
6064
}

src/json_types/stats.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class MemoryInfo : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Stats::Memor
2929
public:
3030
void fromJson(const nlohmann::json& json) override
3131
{
32+
if (!checkRequiredFields(json,
33+
{"userMemoryUsedKiB", "userMemoryLimitKiB", "gpuMemoryUsedKiB", "gpuMemoryLimitKiB"}))
34+
{
35+
throw std::invalid_argument("Missing required fields in JSON");
36+
}
3237
userMemoryUsed = json["userMemoryUsedKiB"].get<uint32_t>();
3338
userMemoryLimit = json["userMemoryLimitKiB"].get<uint32_t>();
3439
gpuMemoryUsed = json["gpuMemoryUsedKiB"].get<uint32_t>();

src/json_types/texttospeech.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class ListVoicesResponse : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Text
3636
public:
3737
void fromJson(const nlohmann::json& json) override
3838
{
39+
if (!checkRequiredFields(json, {"TTS_Status", "voices"}))
40+
{
41+
throw std::invalid_argument("Missing required fields in JSON");
42+
}
3943
ttsStatus_ = json["TTS_Status"].get<uint32_t>();
4044
voices_.clear();
4145
for (const auto& voice : json["voices"])
@@ -56,7 +60,14 @@ class ListVoicesResponse : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Text
5660
class SpeechIdEvent : public Firebolt::JSON::NL_Json_Basic<::Firebolt::TextToSpeech::SpeechIdEvent>
5761
{
5862
public:
59-
void fromJson(const nlohmann::json& json) override { speechId_ = json["speechid"].get<uint32_t>(); }
63+
void fromJson(const nlohmann::json& json) override
64+
{
65+
if (!checkRequiredFields(json, {"speechid"}))
66+
{
67+
throw std::invalid_argument("Missing required fields in JSON");
68+
}
69+
speechId_ = json["speechid"].get<uint32_t>();
70+
}
6071
::Firebolt::TextToSpeech::SpeechIdEvent value() const override
6172
{
6273
return ::Firebolt::TextToSpeech::SpeechIdEvent{speechId_};
@@ -71,6 +82,10 @@ class SpeechResponse : public Firebolt::JSON::NL_Json_Basic<::Firebolt::TextToSp
7182
public:
7283
void fromJson(const nlohmann::json& json) override
7384
{
85+
if (!checkRequiredFields(json, {"speechid", "TTS_Status", "success"}))
86+
{
87+
throw std::invalid_argument("Missing required fields in JSON");
88+
}
7489
speechId_ = json["speechid"].get<uint32_t>();
7590
ttsStatus_ = json["TTS_Status"].get<uint32_t>();
7691
success_ = json["success"].get<bool>();
@@ -91,6 +106,10 @@ class SpeechStateResponse : public Firebolt::JSON::NL_Json_Basic<::Firebolt::Tex
91106
public:
92107
void fromJson(const nlohmann::json& json) override
93108
{
109+
if (!checkRequiredFields(json, {"speechstate", "TTS_Status", "success"}))
110+
{
111+
throw std::invalid_argument("Missing required fields in JSON");
112+
}
94113
speechState_ = static_cast<SpeechState>(json["speechstate"].get<uint32_t>());
95114
ttsStatus_ = json["TTS_Status"].get<uint32_t>();
96115
success_ = json["success"].get<bool>();
@@ -111,6 +130,10 @@ class TTSStatusResponse : public Firebolt::JSON::NL_Json_Basic<::Firebolt::TextT
111130
public:
112131
void fromJson(const nlohmann::json& json) override
113132
{
133+
if (!checkRequiredFields(json, {"TTS_Status", "success"}))
134+
{
135+
throw std::invalid_argument("Missing required fields in JSON");
136+
}
114137
ttsStatus_ = json["TTS_Status"].get<uint32_t>();
115138
success_ = json["success"].get<bool>();
116139
}

0 commit comments

Comments
 (0)