From c5da946e76f6df53be97f5bd5e205d5f2afd95af Mon Sep 17 00:00:00 2001 From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com> Date: Tue, 10 May 2016 14:16:17 -0400 Subject: [PATCH] crypto: mark all operator bool as explicit If operator bool is not marked as explicit and you try to do such: a == b where a and b are instances of class with overloaded bool operator, the behavior is not the waited one: a and b are casted to bool BEFORE the comparaison! This could lead into non waited situation. This patch fixes all operator bool() where overloaded. This also implements PublicKey operators == and != as needed in value.h. This last showed a typical case explained upper. Signed-off-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com> --- include/opendht/crypto.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/opendht/crypto.h b/include/opendht/crypto.h index 0edb700a..56c9a463 100644 --- a/include/opendht/crypto.h +++ b/include/opendht/crypto.h @@ -67,7 +67,13 @@ struct PublicKey PublicKey(PublicKey&& o) noexcept : pk(o.pk) { o.pk = nullptr; }; ~PublicKey(); - operator bool() const { return pk; } + explicit operator bool() const { return pk; } + bool operator ==(const PublicKey& o) const { + return pk == o.pk || getId() == o.getId(); + } + bool operator !=(const PublicKey& o) const { + return !(*this == o); + } PublicKey& operator=(PublicKey&& o) noexcept; @@ -114,7 +120,7 @@ struct PrivateKey PrivateKey(const Blob& import, const std::string& password = {}); ~PrivateKey(); - operator bool() const { return key; } + explicit operator bool() const { return key; } PublicKey getPublicKey() const; Blob serialize(const std::string& password = {}) const; @@ -263,7 +269,7 @@ struct Certificate { void msgpack_unpack(msgpack::object o); - operator bool() const { return cert; } + explicit operator bool() const { return cert; } PublicKey getPublicKey() const; /** Same as getPublicKey().getId() */ -- GitLab