Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
fix: evm_revert crash when passed invalid subscriptionIds (fixes #386)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Jul 12, 2019
1 parent 4ad1c27 commit 3cc082d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/statemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,22 +807,31 @@ StateManager.prototype.snapshot = function(callback) {
};

StateManager.prototype.revert = function(snapshotId, callback) {
var self = this;

if (snapshotId === null || snapshotId === undefined) {
callback(new Error("invalid snapshotId"));
return;
}
// Convert from hex.
snapshotId = utils.bufferToInt(snapshotId);
try {
snapshotId = utils.bufferToInt(snapshotId);
} catch (e) {
callback(e);
return;
}

this.logger.log("Reverting to snapshot #" + snapshotId);

if (snapshotId > this.snapshots.length) {
if (snapshotId > this.snapshots.length || snapshotId <= 0) {
// the snapshot doesn't exist now, or it has already been reverted
callback(null, false);
return false;
}

// Convert to zero based.
snapshotId = snapshotId - 1;
var timeAdjustment = this.snapshots[snapshotId].timeAdjustment;
const snapShot = this.snapshots[snapshotId];
var timeAdjustment = snapShot.timeAdjustment;
var self = this;

// Loop through each snapshot with a higher id than the current one.
async.whilst(
Expand Down
25 changes: 25 additions & 0 deletions test/snapshotting.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,29 @@ describe("Checkpointing / Reverting", function() {
let n4 = await instance.methods.n().call();
assert.strictEqual(n4, "43", "n is not 43 after calling `inc` again");
});

it("evm_revert rejects invalid subscriptionId types without crashing", async() => {
const { send } = context;
const ids = [{ foo: "bar" }, true, false];
await Promise.all(
ids.map((id) => assert.rejects(send("evm_revert", id), /invalid type/, "evm_revert did not reject as expected"))
);
});

it("evm_revert rejects null/undefined subscriptionId values", async() => {
const { send } = context;
const ids = [null, undefined];
await Promise.all(
ids.map((id) =>
assert.rejects(send("evm_revert", id), /invalid snapshotId/, "evm_revert did not reject as expected")
)
);
});

it("evm_revert returns false for out-of-range subscriptionId values", async() => {
const { send } = context;
const ids = [-1, Infinity, -Infinity, Buffer.from([0]), 0.5];
const promises = ids.map((id) => send("evm_revert", id).then((r) => assert(r, false)));
await Promise.all(promises);
});
});

0 comments on commit 3cc082d

Please sign in to comment.