Skip to content

Commit 802931a

Browse files
authored
Merge pull request #5032 from randombit/jack/clang-tidy-implicit-bool-conversion
Fix some readability-implict-bool-conversion warnings from clang-tidy
2 parents 7dcd490 + a58f968 commit 802931a

41 files changed

Lines changed: 203 additions & 154 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/fuzzer/tls_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void fuzz(std::span<const uint8_t> in) {
171171
auto creds = std::make_shared<Fuzzer_TLS_Server_Creds>();
172172
auto callbacks = std::make_shared<Fuzzer_TLS_Server_Callbacks>();
173173

174-
const bool is_datagram = in[0] & 1;
174+
const bool is_datagram = (in[0] & 1) == 1;
175175

176176
Botan::TLS::Server server(callbacks, session_manager, creds, policy, fuzzer_rng_as_shared(), is_datagram);
177177

src/lib/asn1/ber_dec.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@ BER_Decoder& BER_Decoder::decode(BigInt& out, ASN1_Type type_tag, ASN1_Class cla
469469
if(negative) {
470470
secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
471471
for(size_t i = obj.length(); i > 0; --i) {
472-
if(vec[i - 1]--) {
472+
const bool gt0 = (vec[i - 1] > 0);
473+
vec[i - 1] -= 1;
474+
if(gt0) {
473475
break;
474476
}
475477
}

src/lib/filters/pipe.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ void Pipe::find_endpoints(Filter* f) {
194194
* Remove the SecureQueues attached to the Filter
195195
*/
196196
void Pipe::clear_endpoints(Filter* f) {
197-
if(!f) {
197+
if(f == nullptr) {
198198
return;
199199
}
200200
for(size_t j = 0; j != f->total_ports(); ++j) {
201-
if(f->m_next[j] && dynamic_cast<SecureQueue*>(f->m_next[j])) {
201+
if(f->m_next[j] != nullptr && dynamic_cast<SecureQueue*>(f->m_next[j]) != nullptr) {
202202
f->m_next[j] = nullptr;
203203
}
204204
clear_endpoints(f->m_next[j]);
@@ -233,10 +233,10 @@ void Pipe::prepend_filter(Filter* filter) {
233233
* Append a Filter to the Pipe
234234
*/
235235
void Pipe::do_append(Filter* filter) {
236-
if(!filter) {
236+
if(filter == nullptr) {
237237
return;
238238
}
239-
if(dynamic_cast<SecureQueue*>(filter)) {
239+
if(dynamic_cast<SecureQueue*>(filter) != nullptr) {
240240
throw Invalid_Argument("Pipe::append: SecureQueue cannot be used");
241241
}
242242
if(filter->m_owned) {
@@ -249,7 +249,7 @@ void Pipe::do_append(Filter* filter) {
249249

250250
filter->m_owned = true;
251251

252-
if(!m_pipe) {
252+
if(m_pipe == nullptr) {
253253
m_pipe = filter;
254254
} else {
255255
m_pipe->attach(filter);
@@ -263,10 +263,10 @@ void Pipe::do_prepend(Filter* filter) {
263263
if(m_inside_msg) {
264264
throw Invalid_State("Cannot prepend to a Pipe while it is processing");
265265
}
266-
if(!filter) {
266+
if(filter == nullptr) {
267267
return;
268268
}
269-
if(dynamic_cast<SecureQueue*>(filter)) {
269+
if(dynamic_cast<SecureQueue*>(filter) != nullptr) {
270270
throw Invalid_Argument("Pipe::prepend: SecureQueue cannot be used");
271271
}
272272
if(filter->m_owned) {
@@ -275,7 +275,7 @@ void Pipe::do_prepend(Filter* filter) {
275275

276276
filter->m_owned = true;
277277

278-
if(m_pipe) {
278+
if(m_pipe != nullptr) {
279279
filter->attach(m_pipe);
280280
}
281281
m_pipe = filter;
@@ -289,7 +289,7 @@ void Pipe::pop() {
289289
throw Invalid_State("Cannot pop off a Pipe while it is processing");
290290
}
291291

292-
if(!m_pipe) {
292+
if(m_pipe == nullptr) {
293293
return;
294294
}
295295

src/lib/math/numbertheory/numthry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ BigInt gcd(const BigInt& a, const BigInt& b) {
224224
auto both_odd = CT::Mask<word>::expand_bool(u.is_odd()) & CT::Mask<word>::expand_bool(v.is_odd());
225225

226226
// Subtract the smaller from the larger if both are odd
227-
auto u_gt_v = CT::Mask<word>::expand(bigint_cmp(u._data(), u.size(), v._data(), v.size()) > 0);
227+
auto u_gt_v = CT::Mask<word>::expand_bool(bigint_cmp(u._data(), u.size(), v._data(), v.size()) > 0);
228228
bigint_sub_abs(tmp.mutable_data(), u._data(), v._data(), sz, ws.data());
229229
u.ct_cond_assign((u_gt_v & both_odd).as_bool(), tmp);
230230
v.ct_cond_assign((~u_gt_v & both_odd).as_bool(), tmp);

src/lib/misc/roughtime/roughtime.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Response Response::from_bits(const std::vector<uint8_t>& response, const Nonce&
193193
const size_t size = path.size();
194194
const size_t levels = size / 64;
195195

196-
if(size % 64) {
196+
if(size % 64 != 0) {
197197
throw Roughtime_Error("Merkle tree path size must be multiple of 64 bytes");
198198
}
199199
if(indx >= (1U << levels)) {
@@ -204,7 +204,7 @@ Response Response::from_bits(const std::vector<uint8_t>& response, const Nonce&
204204
auto hash = hashLeaf(nonce.get_nonce());
205205
auto index = indx;
206206
for(std::size_t level = 0; level < levels; ++level) {
207-
hashNode(hash, slicer.take<64>(), index & 1);
207+
hashNode(hash, slicer.take<64>(), index % 2 == 1);
208208
index >>= 1;
209209
}
210210

@@ -291,9 +291,9 @@ Chain::Chain(std::string_view str) {
291291

292292
std::vector<Response> Chain::responses() const {
293293
std::vector<Response> responses;
294-
for(unsigned i = 0; i < m_links.size(); ++i) {
294+
for(size_t i = 0; i < m_links.size(); ++i) {
295295
const auto& l = m_links[i];
296-
const auto nonce = i ? nonce_from_blind(m_links[i - 1].response(), l.nonce_or_blind()) : l.nonce_or_blind();
296+
const auto nonce = i > 0 ? nonce_from_blind(m_links[i - 1].response(), l.nonce_or_blind()) : l.nonce_or_blind();
297297
const auto response = Response::from_bits(l.response(), nonce);
298298
if(!response.validate(l.public_key())) {
299299
throw Roughtime_Error("Invalid signature or public key");
@@ -365,7 +365,7 @@ std::vector<uint8_t> online_request(std::string_view uri, const Nonce& nonce, st
365365
//add one additional byte to be able to differentiate if datagram got truncated
366366
const auto n = socket->read(buffer.data(), buffer.size());
367367

368-
if(!n || std::chrono::system_clock::now() - start_time > timeout) {
368+
if(n == 0 || std::chrono::system_clock::now() - start_time > timeout) {
369369
throw System_Error("Timeout waiting for response");
370370
}
371371

src/lib/modes/aead/ccm/ccm.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ void CCM_Mode::encode_length(uint64_t len, uint8_t out[]) {
132132

133133
void CCM_Mode::inc(secure_vector<uint8_t>& C) {
134134
for(size_t i = 0; i != C.size(); ++i) {
135-
if(++C[C.size() - i - 1]) {
135+
uint8_t& b = C[C.size() - i - 1];
136+
b += 1;
137+
if(b > 0) {
136138
break;
137139
}
138140
}

src/lib/pubkey/curve448/ed448/ed448_internal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,8 @@ Ed448Point Ed448Point::scalar_mul(const Scalar448& s) const {
205205

206206
bool Ed448Point::operator==(const Ed448Point& other) const {
207207
// Note that the operator== of of Gf448Elem is constant time
208-
const auto mask_x = CT::Mask<uint8_t>::expand(x() == other.x());
209-
const auto mask_y = CT::Mask<uint8_t>::expand(y() == other.y());
208+
const auto mask_x = CT::Mask<uint8_t>::expand_bool(x() == other.x());
209+
const auto mask_y = CT::Mask<uint8_t>::expand_bool(y() == other.y());
210210

211211
return (mask_x & mask_y).as_bool();
212212
}

src/lib/pubkey/dilithium/dilithium_common/dilithium_algos.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ DilithiumPolyVec make_hint(const DilithiumPolyVec& z, const DilithiumPolyVec& r,
861861

862862
for(size_t i = 0; i < r.size(); ++i) {
863863
for(size_t j = 0; j < r[i].size(); ++j) {
864-
hint[i][j] = make_hint(z[i][j], r[i][j]).as_bool();
864+
hint[i][j] = static_cast<int>(make_hint(z[i][j], r[i][j]).as_bool());
865865
}
866866
}
867867

src/lib/pubkey/ec_group/legacy_ec_point/ec_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ EC_Point EC_Point::mul(const BigInt& scalar) const {
502502
EC_Point R[2] = {this->zero(), *this};
503503

504504
for(size_t i = scalar_bits; i > 0; i--) {
505-
const size_t b = scalar.get_bit(i - 1);
505+
const size_t b = scalar.get_bit(i - 1) ? 1 : 0;
506506
R[b ^ 1].add(R[b], ws);
507507
R[b].mult2(ws);
508508
}

src/lib/pubkey/ecies/ecies.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ PK_Key_Agreement create_key_agreement(const PK_Key_Agreement_Key& private_key,
117117
throw Invalid_Argument("ECIES: cofactor, old cofactor and check mode are only supported for ECDH_PrivateKey");
118118
}
119119

120-
if(ecdh_key && (for_encryption || !ecies_params.cofactor_mode())) {
120+
if(ecdh_key != nullptr && (for_encryption || !ecies_params.cofactor_mode())) {
121121
// ECDH_KA_Operation uses cofactor mode: use own key agreement method if cofactor should not be used.
122122
return PK_Key_Agreement(ECIES_PrivateKey(*ecdh_key), rng, "Raw");
123123
}
@@ -427,7 +427,7 @@ secure_vector<uint8_t> ECIES_Decryptor::do_decrypt(uint8_t& valid_mask, const ui
427427
const secure_vector<uint8_t> calculated_mac = m_mac->final();
428428
valid_mask = CT::is_equal(mac_data.data(), calculated_mac.data(), mac_data.size()).value();
429429

430-
if(valid_mask) {
430+
if(valid_mask == 0xFF) {
431431
// decrypt data
432432

433433
m_cipher->set_key(SymmetricKey(secret_key.begin(), m_params.dem_keylen()));

0 commit comments

Comments
 (0)