Skip to content

Commit fa277cd

Browse files
author
MacroFake
committed
univalue: Throw exception on invalid pushes over silent ignore
1 parent ccccc17 commit fa277cd

File tree

3 files changed

+49
-48
lines changed

3 files changed

+49
-48
lines changed

src/univalue/include/univalue.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ class UniValue {
8282
bool isArray() const { return (typ == VARR); }
8383
bool isObject() const { return (typ == VOBJ); }
8484

85-
bool push_back(const UniValue& val);
86-
bool push_backV(const std::vector<UniValue>& vec);
85+
void push_back(const UniValue& val);
86+
void push_backV(const std::vector<UniValue>& vec);
8787
template <class It>
88-
bool push_backV(It first, It last);
88+
void push_backV(It first, It last);
8989

9090
void __pushKV(const std::string& key, const UniValue& val);
91-
bool pushKV(const std::string& key, const UniValue& val);
92-
bool pushKVs(const UniValue& obj);
91+
void pushKV(const std::string& key, const UniValue& val);
92+
void pushKVs(const UniValue& obj);
9393

9494
std::string write(unsigned int prettyIndent = 0,
9595
unsigned int indentLevel = 0) const;
@@ -140,11 +140,10 @@ class UniValue {
140140
};
141141

142142
template <class It>
143-
bool UniValue::push_backV(It first, It last)
143+
void UniValue::push_backV(It first, It last)
144144
{
145-
if (typ != VARR) return false;
145+
if (typ != VARR) throw std::runtime_error{"JSON value is not an array as expected"};
146146
values.insert(values.end(), first, last);
147-
return true;
148147
}
149148

150149
enum jtokentype {

src/univalue/lib/univalue.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,53 +108,45 @@ bool UniValue::setObject()
108108
return true;
109109
}
110110

111-
bool UniValue::push_back(const UniValue& val_)
111+
void UniValue::push_back(const UniValue& val_)
112112
{
113-
if (typ != VARR)
114-
return false;
113+
if (typ != VARR) throw std::runtime_error{"JSON value is not an array as expected"};
115114

116115
values.push_back(val_);
117-
return true;
118116
}
119117

120-
bool UniValue::push_backV(const std::vector<UniValue>& vec)
118+
void UniValue::push_backV(const std::vector<UniValue>& vec)
121119
{
122-
if (typ != VARR)
123-
return false;
120+
if (typ != VARR) throw std::runtime_error{"JSON value is not an array as expected"};
124121

125122
values.insert(values.end(), vec.begin(), vec.end());
126-
127-
return true;
128123
}
129124

130125
void UniValue::__pushKV(const std::string& key, const UniValue& val_)
131126
{
127+
if (typ != VOBJ) throw std::runtime_error{"JSON value is not an object as expected"};
128+
132129
keys.push_back(key);
133130
values.push_back(val_);
134131
}
135132

136-
bool UniValue::pushKV(const std::string& key, const UniValue& val_)
133+
void UniValue::pushKV(const std::string& key, const UniValue& val_)
137134
{
138-
if (typ != VOBJ)
139-
return false;
135+
if (typ != VOBJ) throw std::runtime_error{"JSON value is not an object as expected"};
140136

141137
size_t idx;
142138
if (findKey(key, idx))
143139
values[idx] = val_;
144140
else
145141
__pushKV(key, val_);
146-
return true;
147142
}
148143

149-
bool UniValue::pushKVs(const UniValue& obj)
144+
void UniValue::pushKVs(const UniValue& obj)
150145
{
151-
if (typ != VOBJ || obj.typ != VOBJ)
152-
return false;
146+
if (typ != VOBJ || obj.typ != VOBJ) throw std::runtime_error{"JSON value is not an object as expected"};
153147

154148
for (size_t i = 0; i < obj.keys.size(); i++)
155149
__pushKV(obj.keys[i], obj.values.at(i));
156-
157-
return true;
158150
}
159151

160152
void UniValue::getObjMap(std::map<std::string,UniValue>& kv) const

src/univalue/test/object.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ BOOST_AUTO_TEST_CASE(univalue_constructor)
8585
BOOST_CHECK_EQUAL(v9.getValStr(), "zappa");
8686
}
8787

88+
BOOST_AUTO_TEST_CASE(univalue_push_throw)
89+
{
90+
UniValue j;
91+
BOOST_CHECK_THROW(j.push_back(1), std::runtime_error);
92+
BOOST_CHECK_THROW(j.push_backV({1}), std::runtime_error);
93+
BOOST_CHECK_THROW(j.__pushKV("k", 1), std::runtime_error);
94+
BOOST_CHECK_THROW(j.pushKV("k", 1), std::runtime_error);
95+
BOOST_CHECK_THROW(j.pushKVs({}), std::runtime_error);
96+
}
97+
8898
BOOST_AUTO_TEST_CASE(univalue_typecheck)
8999
{
90100
UniValue v1;
@@ -198,13 +208,13 @@ BOOST_AUTO_TEST_CASE(univalue_array)
198208
UniValue arr(UniValue::VARR);
199209

200210
UniValue v((int64_t)1023LL);
201-
BOOST_CHECK(arr.push_back(v));
211+
arr.push_back(v);
202212

203213
std::string vStr("zippy");
204-
BOOST_CHECK(arr.push_back(vStr));
214+
arr.push_back(vStr);
205215

206216
const char *s = "pippy";
207-
BOOST_CHECK(arr.push_back(s));
217+
arr.push_back(s);
208218

209219
std::vector<UniValue> vec;
210220
v.setStr("boing");
@@ -213,13 +223,13 @@ BOOST_AUTO_TEST_CASE(univalue_array)
213223
v.setStr("going");
214224
vec.push_back(v);
215225

216-
BOOST_CHECK(arr.push_backV(vec));
226+
arr.push_backV(vec);
217227

218-
BOOST_CHECK(arr.push_back((uint64_t) 400ULL));
219-
BOOST_CHECK(arr.push_back((int64_t) -400LL));
220-
BOOST_CHECK(arr.push_back((int) -401));
221-
BOOST_CHECK(arr.push_back(-40.1));
222-
BOOST_CHECK(arr.push_back(true));
228+
arr.push_back(uint64_t{400ULL});
229+
arr.push_back(int64_t{-400LL});
230+
arr.push_back(int{-401});
231+
arr.push_back(-40.1);
232+
arr.push_back(true);
223233

224234
BOOST_CHECK_EQUAL(arr.empty(), false);
225235
BOOST_CHECK_EQUAL(arr.size(), 10);
@@ -260,39 +270,39 @@ BOOST_AUTO_TEST_CASE(univalue_object)
260270

261271
strKey = "age";
262272
v.setInt(100);
263-
BOOST_CHECK(obj.pushKV(strKey, v));
273+
obj.pushKV(strKey, v);
264274

265275
strKey = "first";
266276
strVal = "John";
267-
BOOST_CHECK(obj.pushKV(strKey, strVal));
277+
obj.pushKV(strKey, strVal);
268278

269279
strKey = "last";
270-
const char *cVal = "Smith";
271-
BOOST_CHECK(obj.pushKV(strKey, cVal));
280+
const char* cVal = "Smith";
281+
obj.pushKV(strKey, cVal);
272282

273283
strKey = "distance";
274-
BOOST_CHECK(obj.pushKV(strKey, (int64_t) 25));
284+
obj.pushKV(strKey, int64_t{25});
275285

276286
strKey = "time";
277-
BOOST_CHECK(obj.pushKV(strKey, (uint64_t) 3600));
287+
obj.pushKV(strKey, uint64_t{3600});
278288

279289
strKey = "calories";
280-
BOOST_CHECK(obj.pushKV(strKey, (int) 12));
290+
obj.pushKV(strKey, int{12});
281291

282292
strKey = "temperature";
283-
BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));
293+
obj.pushKV(strKey, double{90.012});
284294

285295
strKey = "moon";
286-
BOOST_CHECK(obj.pushKV(strKey, true));
296+
obj.pushKV(strKey, true);
287297

288298
strKey = "spoon";
289-
BOOST_CHECK(obj.pushKV(strKey, false));
299+
obj.pushKV(strKey, false);
290300

291301
UniValue obj2(UniValue::VOBJ);
292-
BOOST_CHECK(obj2.pushKV("cat1", 9000));
293-
BOOST_CHECK(obj2.pushKV("cat2", 12345));
302+
obj2.pushKV("cat1", 9000);
303+
obj2.pushKV("cat2", 12345);
294304

295-
BOOST_CHECK(obj.pushKVs(obj2));
305+
obj.pushKVs(obj2);
296306

297307
BOOST_CHECK_EQUAL(obj.empty(), false);
298308
BOOST_CHECK_EQUAL(obj.size(), 11);

0 commit comments

Comments
 (0)