Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add BOOST_REQUIRE to getters returning optional #14771

Merged
merged 1 commit into from Nov 22, 2018

Conversation

@MarcoFalke
Copy link
Member

@MarcoFalke MarcoFalke commented Nov 20, 2018

Usually the returned value is already checked for equality, but for sanity we might as well require that the getter successfully returned.

@Empact
Copy link
Member

@Empact Empact commented Nov 21, 2018

Do you mean to tag ReadConfigStream or GetValue NODISCARD as well? They could be.

GetKey has some unchecked uses in the codebase.

@MarcoFalke MarcoFalke force-pushed the Mf1811-testNoDiscard branch from fa0daa9 to fa21ca0 Nov 21, 2018
@laanwj
Copy link
Member

@laanwj laanwj commented Nov 21, 2018

utACK, I like the idea of adding NODISCARD annotations to make sure the return codes are not ignored.

@practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Nov 21, 2018

utACK – NODISCARD is great!

@@ -36,7 +37,7 @@ class CCoinsViewTest : public CCoinsView
std::map<COutPoint, Coin> map_;

public:
bool GetCoin(const COutPoint& outpoint, Coin& coin) const override
NODISCARD bool GetCoin(const COutPoint& outpoint, Coin& coin) const override
Copy link
Member

@promag promag Nov 22, 2018

Sorry, but this seems like a random change, why is this necessary?

Copy link
Contributor

@practicalswift practicalswift Nov 22, 2018

I'll let @MarcoFalke answer on this specific case, but generally I think it makes perfect sense to add NODISCARD to functions that return a bool indicating success/failure. I think we should do that by default for new functions at least.

Making discarding opt-in will guard against potentially dangerous unintentional discards :-)

Copy link
Member

@promag promag Nov 22, 2018

There are tons of that?

Copy link
Contributor

@practicalswift practicalswift Nov 22, 2018

@promag Let me re-phrase: I think we should add NODISCARD to functions – especially new functions – where discarding the return value might be a mistake.

Such as bool ParseFoo(const std::string& s, Foo *out);, etc.

Sounds reasonable? :-)

Copy link
Member

@promag promag Nov 22, 2018

@practicalswift I'm not agains that annotation, I just don't understand why it's added to GetCoin.

@Empact
Copy link
Member

@Empact Empact commented Nov 22, 2018

I'm into adding NODISCARD to GetKey and GetValue as well, for coherence between the test and source changes.

Here's the patch for that:

diff --git a/src/dbwrapper.h b/src/dbwrapper.h
index 416f5e839..9460e5ff4 100644
--- a/src/dbwrapper.h
+++ b/src/dbwrapper.h
@@ -5,6 +5,7 @@
 #ifndef BITCOIN_DBWRAPPER_H
 #define BITCOIN_DBWRAPPER_H
 
+#include <attributes.h>
 #include <clientversion.h>
 #include <fs.h>
 #include <serialize.h>
@@ -144,7 +145,8 @@ public:
 
     void Next();
 
-    template<typename K> bool GetKey(K& key) {
+    template<typename K>
+    NODISCARD bool GetKey(K& key) {
         leveldb::Slice slKey = piter->key();
         try {
             CDataStream ssKey(slKey.data(), slKey.data() + slKey.size(), SER_DISK, CLIENT_VERSION);
@@ -155,7 +157,8 @@ public:
         return true;
     }
 
-    template<typename V> bool GetValue(V& value) {
+    template<typename V>
+    NODISCARD bool GetValue(V& value) {
         leveldb::Slice slValue = piter->value();
         try {
             CDataStream ssValue(slValue.data(), slValue.data() + slValue.size(), SER_DISK, CLIENT_VERSION);
diff --git a/src/txdb.cpp b/src/txdb.cpp
index 8447352c5..b91931e56 100644
--- a/src/txdb.cpp
+++ b/src/txdb.cpp
@@ -178,8 +178,9 @@ CCoinsViewCursor *CCoinsViewDB::Cursor() const
     // Cache key of first record
     if (i->pcursor->Valid()) {
         CoinEntry entry(&i->keyTmp.second);
-        i->pcursor->GetKey(entry);
-        i->keyTmp.first = entry.key;
+        if (i->pcursor->GetKey(entry)) {
+            i->keyTmp.first = entry.key;
+        }
     } else {
         i->keyTmp.first = 0; // Make sure Valid() and GetKey() return false
     }

MarcoFalke added a commit to MarcoFalke/bitcoin-core that referenced this issue Nov 22, 2018
…ional

fa21ca0 test: Add BOOST_REQUIRE to getters returning optional (MarcoFalke)

Pull request description:

  Usually the returned value is already checked for equality, but for sanity we might as well require that the getter successfully returned.

Tree-SHA512: 0d613a9a721c61bd7a115ebc681a0890df09b8e5775f176ac18b3a586f2ca57bee0b5b816f5a7c314ff3ac6cbb2a4d9c434f8459e054a7c8a6934a75f0120c2a
@MarcoFalke MarcoFalke merged commit fa21ca0 into bitcoin:master Nov 22, 2018
2 checks passed
@MarcoFalke MarcoFalke deleted the Mf1811-testNoDiscard branch Nov 22, 2018
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this issue Sep 1, 2020
Summary:
bitcoin/bitcoin@fa21ca0

---

Backport of Core [[bitcoin/bitcoin#14771 | PR14771]]

Test Plan:
  ninja check check-functional

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Subscribers: deadalnix, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D7278
5tefan added a commit to 5tefan/dash that referenced this issue Jul 28, 2021
…ional

fa21ca0 test: Add BOOST_REQUIRE to getters returning optional
            (MarcoFalke)

Pull request description:

  Usually the returned value is already checked for equality, but for
sanity we might as well require that the getter successfully returned.
5tefan added a commit to 5tefan/dash that referenced this issue Jul 28, 2021
PastaPastaPasta pushed a commit to dashpay/dash that referenced this issue Jul 29, 2021
…ional (#4297)

* Merge bitcoin#14771: test: Add BOOST_REQUIRE to getters returning optional

fa21ca0 test: Add BOOST_REQUIRE to getters returning optional
            (MarcoFalke)

Pull request description:

  Usually the returned value is already checked for equality, but for
sanity we might as well require that the getter successfully returned.

* Ports [[nodiscard]] portion of bitcoin#14771
Munkybooty added a commit to Munkybooty/dash that referenced this issue Aug 3, 2021
…ional (dashpay#4297)

* Merge bitcoin#14771: test: Add BOOST_REQUIRE to getters returning optional

fa21ca0 test: Add BOOST_REQUIRE to getters returning optional
            (MarcoFalke)

Pull request description:

  Usually the returned value is already checked for equality, but for
sanity we might as well require that the getter successfully returned.

* Ports [[nodiscard]] portion of bitcoin#14771
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants