From 4988153e15f2222b1bc0a61d73c19f4dc440e99e Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 8 Sep 2018 16:37:42 +0200 Subject: [PATCH] Library: prevent implicit bool conversion on iterator-like objects ...it should have been this way all the time. Generic code might otherwise be ill guided to assume a conversion from the Iterator to its value type, while in fact an explicit dereferentiation is necessary --- src/gui/model/expander-revealer.hpp | 1 + src/lib/iter-adapter-ptr-deref.hpp | 12 +++++++-- src/lib/iter-adapter-stl.hpp | 25 ++++++++++++++----- src/lib/iter-adapter.hpp | 30 +++++++++++++++++++---- src/lib/itertools.hpp | 6 ++++- src/lib/sync.hpp | 4 +-- tests/library/iter-tree-explorer-test.cpp | 2 +- 7 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/gui/model/expander-revealer.hpp b/src/gui/model/expander-revealer.hpp index 662918b2d..8811ea96b 100644 --- a/src/gui/model/expander-revealer.hpp +++ b/src/gui/model/expander-revealer.hpp @@ -107,6 +107,7 @@ namespace model { and bool{changeState_}; } + explicit operator bool() const { REQUIRE (canExpand()); diff --git a/src/lib/iter-adapter-ptr-deref.hpp b/src/lib/iter-adapter-ptr-deref.hpp index ca4a5b958..b038cadc0 100644 --- a/src/lib/iter-adapter-ptr-deref.hpp +++ b/src/lib/iter-adapter-ptr-deref.hpp @@ -141,7 +141,11 @@ namespace lib { return *this; } - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } @@ -266,7 +270,11 @@ namespace lib { takeAddress(); } - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } diff --git a/src/lib/iter-adapter-stl.hpp b/src/lib/iter-adapter-stl.hpp index e412973e8..e41e29008 100644 --- a/src/lib/iter-adapter-stl.hpp +++ b/src/lib/iter-adapter-stl.hpp @@ -72,11 +72,15 @@ namespace iter_stl { DistinctIter() : i_(), prev_() { } DistinctIter(IT const& i) : i_(i),prev_() { memorise(); } - pointer operator->() const { return i_; } - reference operator*() const { return *i_;} - bool isValid() const { return i_; } + pointer operator->() const { return i_; } + reference operator*() const { return *i_; } + bool isValid() const { return i_; } - operator bool() const { return i_; } + explicit + operator bool() const + { + return bool{i_}; + } DistinctIter& @@ -472,8 +476,17 @@ namespace iter_stl { IterSnapshot& operator= (IterSnapshot const&) = default; IterSnapshot& operator= (IterSnapshot &&) = default; - operator bool() const { return isValid(); } - size_t size() const { return buffer_.size(); } + explicit + operator bool() const + { + return isValid(); + } + + size_t + size() const + { + return buffer_.size(); + } diff --git a/src/lib/iter-adapter.hpp b/src/lib/iter-adapter.hpp index 8834b166c..1364c36d5 100644 --- a/src/lib/iter-adapter.hpp +++ b/src/lib/iter-adapter.hpp @@ -194,7 +194,11 @@ namespace lib { , pos_() { } - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } /* === lumiera forward iterator concept === */ @@ -352,7 +356,11 @@ namespace lib { : core_() { } - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } /* === lumiera forward iterator concept === */ @@ -491,7 +499,11 @@ namespace lib { , e_(oIter.getEnd()) { } - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } /* === lumiera forward iterator concept === */ @@ -599,7 +611,11 @@ namespace lib { // standard copy operations acceptable - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } @@ -732,7 +748,11 @@ namespace lib { : i_(srcIter) { } - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } diff --git a/src/lib/itertools.hpp b/src/lib/itertools.hpp index e95fdd654..d1fb2a616 100644 --- a/src/lib/itertools.hpp +++ b/src/lib/itertools.hpp @@ -205,7 +205,11 @@ namespace lib { hasData(); } - operator bool() const { return isValid(); } + explicit + operator bool() const + { + return isValid(); + } diff --git a/src/lib/sync.hpp b/src/lib/sync.hpp index add2d7ec6..76c7c034b 100644 --- a/src/lib/sync.hpp +++ b/src/lib/sync.hpp @@ -257,7 +257,7 @@ namespace lib { return *this; } - operator bool() { return 0 != tv_sec; } // allows if (timeout_).... + explicit operator bool() { return 0 != tv_sec; } // allows if (timeout_).... }; @@ -368,7 +368,7 @@ namespace lib { } void setTimeout(ulong relative) {timeout_.setOffset(relative);} - bool isTimedWait() {return (timeout_);} + bool isTimedWait() {return bool{timeout_};} }; typedef Mutex NonrecursiveLock_NoWait; diff --git a/tests/library/iter-tree-explorer-test.cpp b/tests/library/iter-tree-explorer-test.cpp index 79f8e3d95..8dc2cdc72 100644 --- a/tests/library/iter-tree-explorer-test.cpp +++ b/tests/library/iter-tree-explorer-test.cpp @@ -1188,7 +1188,7 @@ namespace test{ bool checkPoint() const { - return src; + return bool{src}; } State&