Skip to content

Commit 5d0434d

Browse files
committed
Fix OOM bug: UTXO entries with invalid script length
1 parent 4bf631e commit 5d0434d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/compressor.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,14 @@ class CScriptCompressor
8686
return;
8787
}
8888
nSize -= nSpecialScripts;
89-
script.resize(nSize);
90-
s >> REF(CFlatData(script));
89+
if (nSize > MAX_SCRIPT_SIZE) {
90+
// Overly long script, replace with a short invalid one
91+
script << OP_RETURN;
92+
s.ignore(nSize);
93+
} else {
94+
script.resize(nSize);
95+
s >> REF(CFlatData(script));
96+
}
9197
}
9298
};
9399

src/streams.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,20 @@ class CAutoFile
406406
return (*this);
407407
}
408408

409+
CAutoFile& ignore(size_t nSize)
410+
{
411+
if (!file)
412+
throw std::ios_base::failure("CAutoFile::ignore: file handle is NULL");
413+
unsigned char data[4096];
414+
while (nSize > 0) {
415+
size_t nNow = std::min<size_t>(nSize, sizeof(data));
416+
if (fread(data, 1, nNow, file) != nNow)
417+
throw std::ios_base::failure(feof(file) ? "CAutoFile::ignore: end of file" : "CAutoFile::read: fread failed");
418+
nSize -= nNow;
419+
}
420+
return (*this);
421+
}
422+
409423
CAutoFile& write(const char* pch, size_t nSize)
410424
{
411425
if (!file)

0 commit comments

Comments
 (0)