aboutsummaryrefslogtreecommitdiff
path: root/src/script.h
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-06-05 10:10:52 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2014-06-23 10:38:15 +0200
commitfa126effc2a03e22708960344e62fc21259deb23 (patch)
treebb21fb6b131efe0dd5448765c5240ba208d54945 /src/script.h
parent52d4abfdef93cd622b297a092eb6639e678bcb9d (diff)
Avoid undefined behavior using CFlatData in CScript serialization
`&vch[vch.size()]` and even `&vch[0]` on vectors can cause assertion errors with VC in debug mode. This is the problem mentioned in #4239. The deeper problem with this is that we rely on undefined behavior. - Add `begin_ptr` and `end_ptr` functions that get the beginning and end pointer of vector in a reliable way that copes with empty vectors and doesn't reference outside the vector (see https://stackoverflow.com/questions/1339470/how-to-get-the-address-of-the-stdvector-buffer-start-most-elegantly/1339767#1339767). - Add a convenience constructor to CFlatData that wraps a vector. I added `begin_ptr` and `end_ptr` as separate functions as I imagine they will be useful in more places.
Diffstat (limited to 'src/script.h')
-rw-r--r--src/script.h8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/script.h b/src/script.h
index bd6574627a..ea988f0e40 100644
--- a/src/script.h
+++ b/src/script.h
@@ -770,12 +770,12 @@ public:
void Serialize(Stream &s, int nType, int nVersion) const {
std::vector<unsigned char> compr;
if (Compress(compr)) {
- s << CFlatData(&compr[0], &compr[compr.size()]);
+ s << CFlatData(compr);
return;
}
unsigned int nSize = script.size() + nSpecialScripts;
s << VARINT(nSize);
- s << CFlatData(&script[0], &script[script.size()]);
+ s << CFlatData(script);
}
template<typename Stream>
@@ -784,13 +784,13 @@ public:
s >> VARINT(nSize);
if (nSize < nSpecialScripts) {
std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
- s >> REF(CFlatData(&vch[0], &vch[vch.size()]));
+ s >> REF(CFlatData(vch));
Decompress(nSize, vch);
return;
}
nSize -= nSpecialScripts;
script.resize(nSize);
- s >> REF(CFlatData(&script[0], &script[script.size()]));
+ s >> REF(CFlatData(script));
}
};