diff --git a/lib/block.js b/lib/block.js index 81248ff..52d4e73 100644 --- a/lib/block.js +++ b/lib/block.js @@ -9,6 +9,7 @@ var BufferWriter = require('./encoding/bufferwriter'); var Hash = require('./crypto/hash'); var JSUtil = require('./util/js'); var Transaction = require('./transaction'); +var $ = require('./util/preconditions'); /** * Instantiate a Block from a Buffer, JSON object, or Object with @@ -96,6 +97,7 @@ Block.fromJSON = function fromJSON(json) { */ Block._fromBufferReader = function _fromBufferReader(br) { var info = {}; + $.checkState(!br.finished(), 'No block data received'); info.header = BlockHeader.fromBufferReader(br); var transactions = br.readVarintNum(); info.transactions = []; diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index 89f4645..84509dd 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -28,6 +28,8 @@ BufferReader.prototype.eof = function() { return this.pos >= this.buf.length; }; +BufferReader.prototype.finished = BufferReader.prototype.eof; + BufferReader.prototype.read = function(len) { $.checkArgument(!_.isUndefined(len), 'Must specify a length'); var buf = this.buf.slice(this.pos, this.pos + len); diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 45ed523..1f0b8ae 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -189,6 +189,7 @@ Transaction.prototype.fromBuffer = function(buffer) { }; Transaction.prototype.fromBufferReader = function(reader) { + $.checkArgument(!reader.finished(), 'No transaction data received'); var i, sizeTxIns, sizeTxOuts; this.version = reader.readUInt32LE();