Merge pull request #3166 from tclinken/6.3-avoid-ub

Eliminate some undefined behavior and const_cast usage
This commit is contained in:
Evan Tschannen 2020-05-20 14:36:30 -07:00 committed by GitHub
commit 06676fc88f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 62 additions and 61 deletions

View File

@ -219,8 +219,9 @@ struct VersionedMutations {
*/
struct DecodeProgress {
DecodeProgress() = default;
DecodeProgress(const LogFile& file, std::vector<std::tuple<Arena, Version, int32_t, StringRef>> values)
: file(file), keyValues(values) {}
template <class U>
DecodeProgress(const LogFile& file, U &&values)
: file(file), keyValues(std::forward<U>(values)) {}
// If there are no more mutations to pull from the file.
// However, we could have unfinished version in the buffer when EOF is true,
@ -228,7 +229,7 @@ struct DecodeProgress {
// should call getUnfinishedBuffer() to get these left data.
bool finished() { return (eof && keyValues.empty()) || (leftover && !keyValues.empty()); }
std::vector<std::tuple<Arena, Version, int32_t, StringRef>>&& getUnfinishedBuffer() { return std::move(keyValues); }
std::vector<std::tuple<Arena, Version, int32_t, StringRef>>&& getUnfinishedBuffer() && { return std::move(keyValues); }
// Returns all mutations of the next version in a batch.
Future<VersionedMutations> getNextBatch() { return getNextBatchImpl(this); }
@ -448,7 +449,7 @@ ACTOR Future<Void> decode_logs(DecodeParams params) {
for (; i < logs.size(); i++) {
if (logs[i].fileSize == 0) continue;
state DecodeProgress progress(logs[i], left);
state DecodeProgress progress(logs[i], std::move(left));
wait(progress.openFile(container));
while (!progress.finished()) {
VersionedMutations vms = wait(progress.getNextBatch());
@ -456,7 +457,7 @@ ACTOR Future<Void> decode_logs(DecodeParams params) {
std::cout << vms.version << " " << m.toString() << "\n";
}
}
left = progress.getUnfinishedBuffer();
left = std::move(progress).getUnfinishedBuffer();
if (!left.empty()) {
TraceEvent("UnfinishedFile").detail("File", logs[i].fileName).detail("Q", left.size());
}

View File

@ -738,7 +738,7 @@ Reference<LocationInfo> DatabaseContext::setCachedLocation( const KeyRangeRef& k
locationCache.insert( KeyRangeRef(begin, end), Reference<LocationInfo>() );
}
locationCache.insert( keys, loc );
return std::move(loc);
return loc;
}
void DatabaseContext::invalidateCache( const KeyRef& key, bool isBackward ) {
@ -2449,7 +2449,7 @@ ACTOR Future< Key > getKeyAndConflictRange(
conflictRange.send( std::make_pair( rep, k.orEqual ? keyAfter( k.getKey() ) : Key(k.getKey(), k.arena()) ) );
else
conflictRange.send( std::make_pair( k.orEqual ? keyAfter( k.getKey() ) : Key(k.getKey(), k.arena()), keyAfter( rep ) ) );
return std::move(rep);
return rep;
} catch( Error&e ) {
conflictRange.send(std::make_pair(Key(), Key()));
throw;

View File

@ -802,36 +802,36 @@ ACTOR Future<int> actorFuzz29( FutureStream<int> inputStream, PromiseStream<int>
std::pair<int,int> actorFuzzTests() {
int testsOK = 0;
testsOK += testFuzzActor( &actorFuzz0, "actorFuzz0", (vector<int>(),390229,596271,574865) );
testsOK += testFuzzActor( &actorFuzz1, "actorFuzz1", (vector<int>(),477566,815578,477566,815578,477566,815578,477566,815578,477566,815578,917160) );
testsOK += testFuzzActor( &actorFuzz2, "actorFuzz2", (vector<int>(),476677,930237) );
testsOK += testFuzzActor( &actorFuzz3, "actorFuzz3", (vector<int>(),1000) );
testsOK += testFuzzActor( &actorFuzz4, "actorFuzz4", (vector<int>(),180600,177605,177605,177605,954508,810052) );
testsOK += testFuzzActor( &actorFuzz5, "actorFuzz5", (vector<int>(),1000) );
testsOK += testFuzzActor( &actorFuzz6, "actorFuzz6", (vector<int>(),320321,266526,762336,463730,320321,266526,762336,463730,320321,266526,762336,463730,320321,266526,762336,463730,320321,266526,762336,463730,945289) );
testsOK += testFuzzActor( &actorFuzz7, "actorFuzz7", (vector<int>(),406152,478841,609181,634881,253861,592023,240597,253861,593023,240597,253861,594023,240597,415949,169335,478331,634881,253861,596023,240597,253861,597023,240597,253861,598023,240597,415949,173335,478331,634881,253861,600023,240597,253861,601023,240597,253861,602023,240597,415949,177335,478331,634881,253861,604023,240597,253861,605023,240597,253861,606023,240597,415949,181335,478331,634881,253861,608023,240597,253861,609023,240597,253861,610023,240597,415949,185335,478331,331905,946924,663973,797073,971923,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,534407,814172,949658) );
testsOK += testFuzzActor( &actorFuzz8, "actorFuzz8", (vector<int>(),285937,696473) );
testsOK += testFuzzActor( &actorFuzz9, "actorFuzz9", (vector<int>(),141463,397424) );
testsOK += testFuzzActor( &actorFuzz10, "actorFuzz10", (vector<int>(),543113,1000) );
testsOK += testFuzzActor( &actorFuzz11, "actorFuzz11", (vector<int>(),1000) );
testsOK += testFuzzActor( &actorFuzz12, "actorFuzz12", (vector<int>(),970588,981887) );
testsOK += testFuzzActor( &actorFuzz13, "actorFuzz13", (vector<int>(),861219) );
testsOK += testFuzzActor( &actorFuzz14, "actorFuzz14", (vector<int>(),527098,527098,527098,628047) );
testsOK += testFuzzActor( &actorFuzz15, "actorFuzz15", (vector<int>(),582389,240216,732317,582389,240216,732317,582389,240216,732317,582389,240216,732317,582389,240216,732317,884781) );
testsOK += testFuzzActor( &actorFuzz16, "actorFuzz16", (vector<int>(),943071,492690,908751,198776,537939) );
testsOK += testFuzzActor( &actorFuzz17, "actorFuzz17", (vector<int>(),249436,416782,249436,416782,249436,416782,299183) );
testsOK += testFuzzActor( &actorFuzz18, "actorFuzz18", (vector<int>(),337649,395297,807261,517901) );
testsOK += testFuzzActor( &actorFuzz19, "actorFuzz19", (vector<int>(),492598,139186,742053,492598,140186,742053,492598,141186,742053,592919) );
testsOK += testFuzzActor( &actorFuzz20, "actorFuzz20", (vector<int>(),760082,1000) );
testsOK += testFuzzActor( &actorFuzz21, "actorFuzz21", (vector<int>(),806394) );
testsOK += testFuzzActor( &actorFuzz22, "actorFuzz22", (vector<int>(),722878,369302,416748) );
testsOK += testFuzzActor( &actorFuzz23, "actorFuzz23", (vector<int>(),562792,231437) );
testsOK += testFuzzActor( &actorFuzz24, "actorFuzz24", (vector<int>(),847672,835175) );
testsOK += testFuzzActor( &actorFuzz25, "actorFuzz25", (vector<int>(),843261,327560,592398) );
testsOK += testFuzzActor( &actorFuzz26, "actorFuzz26", (vector<int>(),520263,306397,944232,366272,700651,146918,191890) );
testsOK += testFuzzActor( &actorFuzz27, "actorFuzz27", (vector<int>(),313322,196907) );
testsOK += testFuzzActor( &actorFuzz28, "actorFuzz28", (vector<int>(),715827,529509,449273,715827,529509,449273,715827,529509,449273,715827,529509,449273,715827,529509,449273,743922) );
testsOK += testFuzzActor( &actorFuzz29, "actorFuzz29", (vector<int>(),821092,901028,617942,821092,902028,617942,821092,903028,617942,821092,904028,617942,821092,905028,617942,560881) );
testsOK += testFuzzActor( &actorFuzz0, "actorFuzz0", {390229,596271,574865});
testsOK += testFuzzActor( &actorFuzz1, "actorFuzz1", {477566,815578,477566,815578,477566,815578,477566,815578,477566,815578,917160});
testsOK += testFuzzActor( &actorFuzz2, "actorFuzz2", {476677,930237});
testsOK += testFuzzActor( &actorFuzz3, "actorFuzz3", {1000});
testsOK += testFuzzActor( &actorFuzz4, "actorFuzz4", {180600,177605,177605,177605,954508,810052});
testsOK += testFuzzActor( &actorFuzz5, "actorFuzz5", {1000});
testsOK += testFuzzActor( &actorFuzz6, "actorFuzz6", {320321,266526,762336,463730,320321,266526,762336,463730,320321,266526,762336,463730,320321,266526,762336,463730,320321,266526,762336,463730,945289});
testsOK += testFuzzActor( &actorFuzz7, "actorFuzz7", {406152,478841,609181,634881,253861,592023,240597,253861,593023,240597,253861,594023,240597,415949,169335,478331,634881,253861,596023,240597,253861,597023,240597,253861,598023,240597,415949,173335,478331,634881,253861,600023,240597,253861,601023,240597,253861,602023,240597,415949,177335,478331,634881,253861,604023,240597,253861,605023,240597,253861,606023,240597,415949,181335,478331,634881,253861,608023,240597,253861,609023,240597,253861,610023,240597,415949,185335,478331,331905,946924,663973,797073,971923,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,295772,923567,559259,559259,559259,325678,679187,534407,814172,949658});
testsOK += testFuzzActor( &actorFuzz8, "actorFuzz8", {285937,696473});
testsOK += testFuzzActor( &actorFuzz9, "actorFuzz9", {141463,397424});
testsOK += testFuzzActor( &actorFuzz10, "actorFuzz10", {543113,1000});
testsOK += testFuzzActor( &actorFuzz11, "actorFuzz11", {1000});
testsOK += testFuzzActor( &actorFuzz12, "actorFuzz12", {970588,981887});
testsOK += testFuzzActor( &actorFuzz13, "actorFuzz13", {861219});
testsOK += testFuzzActor( &actorFuzz14, "actorFuzz14", {527098,527098,527098,628047});
testsOK += testFuzzActor( &actorFuzz15, "actorFuzz15", {582389,240216,732317,582389,240216,732317,582389,240216,732317,582389,240216,732317,582389,240216,732317,884781});
testsOK += testFuzzActor( &actorFuzz16, "actorFuzz16", {943071,492690,908751,198776,537939});
testsOK += testFuzzActor( &actorFuzz17, "actorFuzz17", {249436,416782,249436,416782,249436,416782,299183});
testsOK += testFuzzActor( &actorFuzz18, "actorFuzz18", {337649,395297,807261,517901});
testsOK += testFuzzActor( &actorFuzz19, "actorFuzz19", {492598,139186,742053,492598,140186,742053,492598,141186,742053,592919});
testsOK += testFuzzActor( &actorFuzz20, "actorFuzz20", {760082,1000});
testsOK += testFuzzActor( &actorFuzz21, "actorFuzz21", {806394});
testsOK += testFuzzActor( &actorFuzz22, "actorFuzz22", {722878,369302,416748});
testsOK += testFuzzActor( &actorFuzz23, "actorFuzz23", {562792,231437});
testsOK += testFuzzActor( &actorFuzz24, "actorFuzz24", {847672,835175});
testsOK += testFuzzActor( &actorFuzz25, "actorFuzz25", {843261,327560,592398});
testsOK += testFuzzActor( &actorFuzz26, "actorFuzz26", {520263,306397,944232,366272,700651,146918,191890});
testsOK += testFuzzActor( &actorFuzz27, "actorFuzz27", {313322,196907});
testsOK += testFuzzActor( &actorFuzz28, "actorFuzz28", {715827,529509,449273,715827,529509,449273,715827,529509,449273,715827,529509,449273,715827,529509,449273,743922});
testsOK += testFuzzActor( &actorFuzz29, "actorFuzz29", {821092,901028,617942,821092,902028,617942,821092,903028,617942,821092,904028,617942,821092,905028,617942,560881});
return std::make_pair(testsOK, 30);
}
#endif // WIN32

View File

@ -24,14 +24,6 @@
using std::vector;
inline vector<int>& operator , (vector<int>& v, int a) {
v.push_back(a);
return v;
}
inline vector<int>& operator , (vector<int> const& v, int a) {
return (const_cast<vector<int>&>(v), a);
}
inline void throw_operation_failed() { throw operation_failed(); }
// This is in dsltest.actor.cpp:

View File

@ -80,16 +80,17 @@ Future<Reference<IAsyncFile>> AsyncFileCached::open_impl( std::string filename,
return open_impl(filename, flags, mode, pageCache);
}
Future<Void> AsyncFileCached::read_write_impl( AsyncFileCached* self, void* data, int length, int64_t offset, bool writing ) {
if (writing) {
template <bool writing>
Future<Void> AsyncFileCached::read_write_impl(AsyncFileCached* self,
typename std::conditional_t<writing, const uint8_t*, uint8_t*> data,
int length, int64_t offset) {
if constexpr (writing) {
if (offset + length > self->length)
self->length = offset + length;
}
std::vector<Future<Void>> actors;
uint8_t* cdata = static_cast<uint8_t*>(data);
int offsetInPage = offset % self->pageCache->pageSize;
int64_t pageOffset = offset - offsetInPage;
@ -108,13 +109,16 @@ Future<Void> AsyncFileCached::read_write_impl( AsyncFileCached* self, void* data
int bytesInPage = std::min(self->pageCache->pageSize - offsetInPage, remaining);
auto w = writing
? p->second->write( cdata, bytesInPage, offsetInPage )
: p->second->read( cdata, bytesInPage, offsetInPage );
Future<Void> w;
if constexpr (writing) {
w = p->second->write(data, bytesInPage, offsetInPage);
} else {
w = p->second->read(data, bytesInPage, offsetInPage);
}
if (!w.isReady() || w.isError())
actors.push_back( w );
cdata += bytesInPage;
data += bytesInPage;
pageOffset += self->pageCache->pageSize;
offsetInPage = 0;

View File

@ -28,6 +28,7 @@
#define FLOW_ASYNCFILECACHED_ACTOR_H
#include <boost/intrusive/list.hpp>
#include <type_traits>
#include "flow/flow.h"
#include "fdbrpc/IAsyncFile.h"
@ -166,7 +167,7 @@ public:
length = int(this->length - offset);
ASSERT(length >= 0);
}
auto f = read_write_impl(this, data, length, offset, false);
auto f = read_write_impl<false>(this, static_cast<uint8_t*>(data), length, offset);
if( f.isReady() && !f.isError() ) return length;
++countFileCacheReadsBlocked;
++countCacheReadsBlocked;
@ -180,7 +181,7 @@ public:
wait(self->currentTruncate);
++self->countFileCacheWrites;
++self->countCacheWrites;
Future<Void> f = read_write_impl(self, const_cast<void*>(data), length, offset, true);
Future<Void> f = read_write_impl<true>(self, static_cast<const uint8_t*>(data), length, offset);
if (!f.isReady()) {
++self->countFileCacheWritesBlocked;
++self->countCacheWritesBlocked;
@ -346,7 +347,10 @@ private:
return Void();
}
static Future<Void> read_write_impl( AsyncFileCached* self, void* data, int length, int64_t offset, bool writing );
template <bool writing>
static Future<Void> read_write_impl(AsyncFileCached* self,
typename std::conditional_t<writing, const uint8_t*, uint8_t*> data,
int length, int64_t offset);
void remove_page( AFCPage* page );
};

2
fdbrpc/actorFuzz.py Normal file → Executable file
View File

@ -449,7 +449,7 @@ for actor in actors:
print("std::pair<int,int> actorFuzzTests() {\n\tint testsOK = 0;", file=outputFile)
for actor in actors:
print('\ttestsOK += testFuzzActor( &%s, "%s", (vector<int>(),%s) );' % (actor.name, actor.name, ','.join(str(e) for e in actor.ecx.output)),
print('\ttestsOK += testFuzzActor( &%s, "%s", {%s} );' % (actor.name, actor.name, ','.join(str(e) for e in actor.ecx.output)),
file=outputFile)
print("\treturn std::make_pair(testsOK, %d);\n}" % len(actors), file=outputFile)
print('#endif // WIN32\n', file=outputFile)

View File

@ -728,7 +728,7 @@ StringRef setK(Arena& arena, int i) {
#include "fdbserver/ConflictSet.h"
struct ConflictSet {
ConflictSet() : oldestVersion(0) {}
ConflictSet() : oldestVersion(0), removalKey(makeString(0)) {}
~ConflictSet() {}
SkipList versionHistory;

View File

@ -4899,7 +4899,7 @@ public:
#include "fdbserver/art_impl.h"
RedwoodRecordRef VersionedBTree::dbBegin(StringRef(), 0);
RedwoodRecordRef VersionedBTree::dbBegin(LiteralStringRef(""));
RedwoodRecordRef VersionedBTree::dbEnd(LiteralStringRef("\xff\xff\xff\xff\xff"));
VersionedBTree::Counts VersionedBTree::counts;

View File

@ -73,7 +73,7 @@ class ThreadPool : public IThreadPool, public ReferenceCounted<ThreadPool> {
void operator()() { Thread::dispatch(action); action = NULL; }
~ActionWrapper() { if (action) { action->cancel(); } }
private:
void operator=(ActionWrapper const&);
ActionWrapper &operator=(ActionWrapper const&);
};
public:
ThreadPool() : dontstop(ios), mode(Run) {}

View File

@ -287,7 +287,7 @@ ACTOR static Future<Void> readEntireFile( std::string filename, std::string* des
throw file_too_large();
}
destination->resize(filesize);
wait(success(file->read(const_cast<char*>(destination->c_str()), filesize, 0)));
wait(success(file->read(&destination[0], filesize, 0)));
return Void();
}