Skip to content

test: Add BOOST_REQUIRE to getters returning optional#14771

Merged
maflcko merged 1 commit into
bitcoin:masterfrom
maflcko:Mf1811-testNoDiscard
Nov 22, 2018
Merged

test: Add BOOST_REQUIRE to getters returning optional#14771
maflcko merged 1 commit into
bitcoin:masterfrom
maflcko:Mf1811-testNoDiscard

Conversation

@maflcko

@maflcko maflcko commented Nov 20, 2018

Copy link
Copy Markdown
Member

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

@maflcko maflcko added the Tests label Nov 20, 2018
@Empact

Empact commented Nov 21, 2018

Copy link
Copy Markdown
Contributor

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

GetKey has some unchecked uses in the codebase.

@maflcko maflcko force-pushed the Mf1811-testNoDiscard branch from fa0daa9 to fa21ca0 Compare November 21, 2018 01:00
@laanwj

laanwj commented Nov 21, 2018

Copy link
Copy Markdown
Member

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

@practicalswift

Copy link
Copy Markdown
Contributor

utACK – NODISCARD is great!

Comment thread src/test/coins_tests.cpp

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@practicalswift practicalswift Nov 22, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are tons of that?

@practicalswift practicalswift Nov 22, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@Empact

Empact commented Nov 22, 2018

Copy link
Copy Markdown
Contributor

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
     }

maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request 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
@maflcko maflcko merged commit fa21ca0 into bitcoin:master Nov 22, 2018
@maflcko maflcko deleted the Mf1811-testNoDiscard branch November 22, 2018 16:39
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request 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 pull request 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 pull request Jul 28, 2021
PastaPastaPasta pushed a commit to dashpay/dash that referenced this pull request 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
@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

Development

Successfully merging this pull request may close these issues.

5 participants