Skip to content

Commit a0be38f

Browse files
author
Patrick Nagurny
committed
check for timestamp out of bounds
1 parent 7e1d433 commit a0be38f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/services/db.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,16 @@ DB.prototype.getBlockHashesByTimestamp = function(high, low, callback) {
209209
var self = this;
210210
var hashes = [];
211211

212+
try {
213+
var lowKey = this._encodeBlockIndexKey(low);
214+
var highKey = this._encodeBlockIndexKey(high);
215+
} catch(e) {
216+
return callback(e);
217+
}
218+
212219
var stream = this.store.createReadStream({
213-
gte: this._encodeBlockIndexKey(low),
214-
lte: this._encodeBlockIndexKey(high),
220+
gte: lowKey,
221+
lte: highKey,
215222
reverse: true,
216223
valueEncoding: 'binary',
217224
keyEncoding: 'binary'
@@ -456,6 +463,7 @@ DB.prototype.runAllBlockHandlers = function(block, add, callback) {
456463
};
457464

458465
DB.prototype._encodeBlockIndexKey = function(timestamp) {
466+
$.checkArgument(timestamp >= 0 && timestamp <= 4294967295, 'timestamp out of bounds');
459467
var timestampBuffer = new Buffer(4);
460468
timestampBuffer.writeUInt32BE(timestamp);
461469
return Buffer.concat([DB.PREFIXES.BLOCKS, timestampBuffer]);

test/services/db.unit.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,20 @@ describe('DB Service', function() {
423423

424424
readStream.emit('close');
425425
});
426+
427+
it('should give an error if the timestamp is out of range', function(done) {
428+
var db = new DB(baseConfig);
429+
var readStream = new EventEmitter();
430+
db.store = {
431+
createReadStream: sinon.stub().returns(readStream)
432+
};
433+
434+
db.getBlockHashesByTimestamp(-1, -5, function(err, hashes) {
435+
should.exist(err);
436+
err.message.should.equal('Invalid Argument: timestamp out of bounds');
437+
done();
438+
});
439+
});
426440
});
427441

428442
describe('#getPrevHash', function() {

0 commit comments

Comments
 (0)