Skip to content

Commit a969b2f

Browse files
committed
Merge bitcoin/bitcoin#25551: refactor: Throw exception on invalid Univalue pushes over silent ignore
fa277cd univalue: Throw exception on invalid pushes over silent ignore (MacroFake) ccccc17 refactor: Default options in walletcreatefundedpsbt to VOBJ instead of VNULL (MacroFake) Pull request description: The return value of the `push*` helpers is never used, but important to determine if the operation was successful. One way to fix this would be to add the "nodiscard" attribute. However, this would make the code (and this diff) overly verbose for no reason. So fix it by removing the never used return value. Also, fail verbosely in case of a programming mistake. ACKs for top commit: furszy: code ACK fa277cd Tree-SHA512: ef212a5bf5ae6bbad20acc4dafa3715521e81544185988d1eab724f440e4864a27e686aff51d5bc51b3017892c2eb8e577bcb8f37e8ddbaa0d8833bb622f2f9c
2 parents 85b601e + fa277cd commit a969b2f

File tree

4 files changed

+50
-49
lines changed

4 files changed

+50
-49
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);

src/wallet/rpc/spend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ RPCHelpMan walletcreatefundedpsbt()
16331633
}, true
16341634
);
16351635

1636-
UniValue options = request.params[3];
1636+
UniValue options{request.params[3].isNull() ? UniValue::VOBJ : request.params[3]};
16371637

16381638
CAmount fee;
16391639
int change_position;

0 commit comments

Comments
 (0)