Fix Clang Static Analyzer warnings reported by @kallewoof in #12961:
- Fix dead stores. Values were stored but never read.
- Add assertion to guide static analyzers. See #12961 for details.
Fix Clang Static Analyzer warnings reported by @kallewoof in #12961:
Could you elaborate on why it needs the guidance?
524 | @@ -525,6 +525,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request) 525 | // Need to update only after we know CreateNewBlock succeeded 526 | pindexPrev = pindexPrevNew; 527 | } 528 | + assert(pindexPrev && &pblocktemplate->block);
Are you sure the analyzer doesn't realize a reference to an object is never null? Weird.
@kallewoof Agreed – it should understand that. Checking pindexPrev should be sufficient. Updated. Please re-review :-)
utACK 7e95d9772f3e576e514a73fbade932cf5024c474
Clang Analyzer no longer reports the corresponding issues.
utACK 7e95d97
948 | @@ -949,7 +949,7 @@ static void TestOtherProcess(fs::path dirname, std::string lockname, int fd) 949 | case UnlockCommand: 950 | ReleaseDirectoryLocks(); 951 | ch = true; // Always succeeds 952 | - rv = write(fd, &ch, 1);
Should this be fixed by adding an assert(rv == 1); instead, matching the LockCommand case?
546 | @@ -547,7 +547,6 @@ BOOST_AUTO_TEST_CASE(SelectCoins_test) 547 | { 548 | // Reset 549 | out_value = 0; 550 | - target = 0;
Might make sense to reset out_value and out_set at the sample place that target actually gets defined.
524 | @@ -525,6 +525,7 @@ UniValue getblocktemplate(const JSONRPCRequest& request) 525 | // Need to update only after we know CreateNewBlock succeeded 526 | pindexPrev = pindexPrevNew; 527 | } 528 | + assert(pindexPrev);
I believe the reason this is valid is that we've checked IsInitialBlockDownload() is false, and that ensures chainActive->Tip() is not nullptr, and we've just ensured pindexPrev is chainActive->Tip(). We did potentially release cs_main in the meantime, though. I wonder if asserting chainActive->Tip() != nullptr or !IsInitialBlockDownload() would be enough to satisfy to the static analysis; either seems like it would be easier to understand, to me?
@ajtowns On what line do you suggest placing that assertion?
If it satisfied the analysis tool, I'd probably put assert(chainActive->Tip() != nullptr); somewhere around static CBlockIndex* pindexPrev;, I guess.
utACK 7e95d9772f3e576e514a73fbade932cf5024c474
I had a quick go at trying to get the static analyzer to work but didn't strike it lucky.
Updated. Please re-review :-)
89 | @@ -90,9 +90,9 @@ struct WitnessV0KeyHash : public uint160 90 | //! CTxDestination subtype to encode any future Witness version 91 | struct WitnessUnknown 92 | { 93 | - unsigned int version; 94 | - unsigned int length; 95 | - unsigned char program[40]; 96 | + unsigned int version = 0; 97 | + unsigned int length = 0; 98 | + unsigned char program[40] = {};
The warning is only reasonable if an uninitialised WitnessUnknown is compared with something; that seems a bug in its own right, and not something that should be hidden by auto initialising everything to 0/empty?
How about:
+ template<typename T>
+ inline WitnessUnknown(unsigned int _version, T _data)
+ : version(_version), length(_data.size())
+ {
+ assert(length <= 40);
+ std::copy(_data.begin(), _data.end(), program);
+ }
+
+ WitnessUnknown() = delete;
and changing the various uses from:
- WitnessUnknown unk;
- unk.version = version;
- std::copy(data.begin(), data.end(), unk.program);
- unk.length = data.size();
- return unk;
to just
+ return WitnessUnknown(version, data);
As far as I can see the warning is a false-positive though?
EDIT: oops, markdown formatting missed the template<>
Thinking about it a bit more, that doesn't guarantee the first length bytes of program are initialised. std::copy_n(_data, length, program); might arguably be better.
Skipped this change to keep things easy to review. I might post a follow-up PR instead :-)
utACK 159c32d1f111e6bad490bd23ae215462e8ba4374
@kallewoof @Empact Would you mind re-reviewing the latest version? :-)
utACK 159c32d
utACK 159c32d1f111e6bad490bd23ae215462e8ba4374
@MarcoFalke @laanwj Willing to review? :-)
utACK 159c32d1f111e6bad490bd23ae215462e8ba4374
utACK 159c32d1f111e6bad490bd23ae215462e8ba4374