Skip to content

Commit a5e20c7

Browse files
author
Evgenii Zhemchugov
committed
Store::JsonParser: validate input before parsing
Avoid leaving Store half-filled.
1 parent ce36831 commit a5e20c7

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/Store/Json.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ const char* json_scan(const char* input) {
206206
};
207207
}
208208

209+
bool json_valid(const char* input) {
210+
input = json_scan(input);
211+
input = json_scan_whitespace(input);
212+
return input && !*input;
213+
};
214+
209215
bool json_decode_r(const char*& input, bool& value, adl_tag) {
210216
const char* i = json_scan_whitespace(input);
211217
if (!i) return false;

src/Store/Json.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const char* json_scan_object(const char* input);
100100
const char* json_scan_array(const char* input);
101101
const char* json_scan(const char* input); // generic
102102

103+
bool json_valid(const char* input);
104+
103105

104106
// json_encode_r implements encoding of specific objects. Add overloads to this
105107
// function to support custom classes.

src/Store/Store.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ namespace ToolFramework {
124124
}
125125

126126
bool Store::JsonParser(const char* input) {
127+
if (!json_valid(input)) return false; // invalid JSON string
128+
129+
// We have validated input, so in the following `return false` should never
130+
// happen. We keep it, however, just in case.
127131
bool result = json_decode_object(
128132
input,
129133
[this](const char*& input_, std::string key) -> bool {
@@ -165,8 +169,6 @@ namespace ToolFramework {
165169
);
166170
if (!result) return false;
167171

168-
input = json_scan_whitespace(input);
169-
if (!input || *input) return false; // junk at the end
170172
return true;
171173
};
172174

0 commit comments

Comments
 (0)