Skip to content

Commit eb0b56b

Browse files
committedAug 8, 2014
Simplify serialize.h's exception handling
Remove the 'state' and 'exceptmask' from serialize.h's stream implementations, as well as related methods. As exceptmask always included 'failbit', and setstate was always called with bits = failbit, all it did was immediately raise an exception. Get rid of those variables, and replace the setstate with direct exception throwing (which also removes some dead code). As a result, good() is never reached after a failure (there are only 2 calls, one of which is in tests), and can just be replaced by !eof(). fail(), clear(n) and exceptions() are just never called. Delete them.
1 parent de89257 commit eb0b56b

File tree

3 files changed

+8
-59
lines changed

3 files changed

+8
-59
lines changed
 

‎src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3228,7 +3228,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
32283228
}
32293229
}
32303230
uint64_t nRewind = blkdat.GetPos();
3231-
while (blkdat.good() && !blkdat.eof()) {
3231+
while (!blkdat.eof()) {
32323232
boost::this_thread::interruption_point();
32333233

32343234
blkdat.SetPos(nRewind);

‎src/serialize.h

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,6 @@ class CDataStream
870870
typedef CSerializeData vector_type;
871871
vector_type vch;
872872
unsigned int nReadPos;
873-
short state;
874-
short exceptmask;
875873
public:
876874
int nType;
877875
int nVersion;
@@ -923,8 +921,6 @@ class CDataStream
923921
nReadPos = 0;
924922
nType = nTypeIn;
925923
nVersion = nVersionIn;
926-
state = 0;
927-
exceptmask = std::ios::badbit | std::ios::failbit;
928924
}
929925

930926
CDataStream& operator+=(const CDataStream& b)
@@ -1047,19 +1043,7 @@ class CDataStream
10471043
//
10481044
// Stream subset
10491045
//
1050-
void setstate(short bits, const char* psz)
1051-
{
1052-
state |= bits;
1053-
if (state & exceptmask)
1054-
throw std::ios_base::failure(psz);
1055-
}
1056-
10571046
bool eof() const { return size() == 0; }
1058-
bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
1059-
bool good() const { return !eof() && (state == 0); }
1060-
void clear(short n) { state = n; } // name conflict with vector clear()
1061-
short exceptions() { return exceptmask; }
1062-
short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CDataStream"); return prev; }
10631047
CDataStream* rdbuf() { return this; }
10641048
int in_avail() { return size(); }
10651049

@@ -1079,9 +1063,7 @@ class CDataStream
10791063
{
10801064
if (nReadPosNext > vch.size())
10811065
{
1082-
setstate(std::ios::failbit, "CDataStream::read() : end of data");
1083-
memset(pch, 0, nSize);
1084-
nSize = vch.size() - nReadPos;
1066+
throw std::ios_base::failure("CDataStream::read() : end of data");
10851067
}
10861068
memcpy(pch, &vch[nReadPos], nSize);
10871069
nReadPos = 0;
@@ -1101,7 +1083,7 @@ class CDataStream
11011083
if (nReadPosNext >= vch.size())
11021084
{
11031085
if (nReadPosNext > vch.size())
1104-
setstate(std::ios::failbit, "CDataStream::ignore() : end of data");
1086+
throw std::ios_base::failure("CDataStream::ignore() : end of data");
11051087
nReadPos = 0;
11061088
vch.clear();
11071089
return (*this);
@@ -1174,8 +1156,6 @@ class CAutoFile
11741156
{
11751157
protected:
11761158
FILE* file;
1177-
short state;
1178-
short exceptmask;
11791159
public:
11801160
int nType;
11811161
int nVersion;
@@ -1185,8 +1165,6 @@ class CAutoFile
11851165
file = filenew;
11861166
nType = nTypeIn;
11871167
nVersion = nVersionIn;
1188-
state = 0;
1189-
exceptmask = std::ios::badbit | std::ios::failbit;
11901168
}
11911169

11921170
~CAutoFile()
@@ -1213,19 +1191,6 @@ class CAutoFile
12131191
//
12141192
// Stream subset
12151193
//
1216-
void setstate(short bits, const char* psz)
1217-
{
1218-
state |= bits;
1219-
if (state & exceptmask)
1220-
throw std::ios_base::failure(psz);
1221-
}
1222-
1223-
bool fail() const { return state & (std::ios::badbit | std::ios::failbit); }
1224-
bool good() const { return state == 0; }
1225-
void clear(short n = 0) { state = n; }
1226-
short exceptions() { return exceptmask; }
1227-
short exceptions(short mask) { short prev = exceptmask; exceptmask = mask; setstate(0, "CAutoFile"); return prev; }
1228-
12291194
void SetType(int n) { nType = n; }
12301195
int GetType() { return nType; }
12311196
void SetVersion(int n) { nVersion = n; }
@@ -1238,7 +1203,7 @@ class CAutoFile
12381203
if (!file)
12391204
throw std::ios_base::failure("CAutoFile::read : file handle is NULL");
12401205
if (fread(pch, 1, nSize, file) != nSize)
1241-
setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
1206+
throw std::ios_base::failure(feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed");
12421207
return (*this);
12431208
}
12441209

@@ -1247,7 +1212,7 @@ class CAutoFile
12471212
if (!file)
12481213
throw std::ios_base::failure("CAutoFile::write : file handle is NULL");
12491214
if (fwrite(pch, 1, nSize, file) != nSize)
1250-
setstate(std::ios::failbit, "CAutoFile::write : write failed");
1215+
throw std::ios_base::failure("CAutoFile::write : write failed");
12511216
return (*this);
12521217
}
12531218

@@ -1292,16 +1257,7 @@ class CBufferedFile
12921257
uint64_t nRewind; // how many bytes we guarantee to rewind
12931258
std::vector<char> vchBuf; // the buffer
12941259

1295-
short state;
1296-
short exceptmask;
1297-
12981260
protected:
1299-
void setstate(short bits, const char *psz) {
1300-
state |= bits;
1301-
if (state & exceptmask)
1302-
throw std::ios_base::failure(psz);
1303-
}
1304-
13051261
// read data from the source to fill the buffer
13061262
bool Fill() {
13071263
unsigned int pos = nSrcPos % vchBuf.size();
@@ -1313,8 +1269,7 @@ class CBufferedFile
13131269
return false;
13141270
size_t read = fread((void*)&vchBuf[pos], 1, readNow, src);
13151271
if (read == 0) {
1316-
setstate(std::ios_base::failbit, feof(src) ? "CBufferedFile::Fill : end of file" : "CBufferedFile::Fill : fread failed");
1317-
return false;
1272+
throw std::ios_base::failure(feof(src) ? "CBufferedFile::Fill : end of file" : "CBufferedFile::Fill : fread failed");
13181273
} else {
13191274
nSrcPos += read;
13201275
return true;
@@ -1327,12 +1282,7 @@ class CBufferedFile
13271282

13281283
CBufferedFile(FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn) :
13291284
src(fileIn), nSrcPos(0), nReadPos(0), nReadLimit((uint64_t)(-1)), nRewind(nRewindIn), vchBuf(nBufSize, 0),
1330-
state(0), exceptmask(std::ios_base::badbit | std::ios_base::failbit), nType(nTypeIn), nVersion(nVersionIn) {
1331-
}
1332-
1333-
// check whether no error occurred
1334-
bool good() const {
1335-
return state == 0;
1285+
nType(nTypeIn), nVersion(nVersionIn) {
13361286
}
13371287

13381288
// check whether we're at the end of the source file
@@ -1391,7 +1341,6 @@ class CBufferedFile
13911341
nLongPos = ftell(src);
13921342
nSrcPos = nLongPos;
13931343
nReadPos = nLongPos;
1394-
state = 0;
13951344
return true;
13961345
}
13971346

‎src/test/alert_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ struct ReadAlerts
8383
std::vector<unsigned char> vch(alert_tests::alertTests, alert_tests::alertTests + sizeof(alert_tests::alertTests));
8484
CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
8585
try {
86-
while (stream.good())
86+
while (!stream.eof())
8787
{
8888
CAlert alert;
8989
stream >> alert;

0 commit comments

Comments
 (0)
This conversation has been locked and limited to collaborators.