Skip to content

Commit

Permalink
Add segfault tests (#514)
Browse files Browse the repository at this point in the history
* restore (but skip) segfault test from abstract-leveldown

* prevent segfault when calling iterator() on non-open db
  • Loading branch information
vweevers committed Oct 21, 2018
1 parent d979b30 commit 392394d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions leveldown.js
Expand Up @@ -88,6 +88,11 @@ LevelDOWN.prototype.getProperty = function (property) {
}

LevelDOWN.prototype._iterator = function (options) {
if (this.status !== 'open') {
// Prevent segfault
throw new Error('cannot call iterator() before open()')
}

return new Iterator(this, options)
}

Expand Down
43 changes: 43 additions & 0 deletions test/segfault-test.js
@@ -0,0 +1,43 @@
const test = require('tape')
const testCommon = require('./common')

// Open issue: https://github.com/Level/leveldown/issues/157
test.skip('close() does not segfault if there is a pending write', function (t) {
t.plan(3)

const db = testCommon.factory()

db.open(function (err) {
t.ifError(err, 'no open error')

// The "sync" option seems to be a reliable way to trigger a segfault,
// but is not necessarily the cause of that segfault. More likely, it
// exposes a race condition that's already there.
db.put('foo', 'bar', { sync: true }, function (err) {
// We never get here, due to segfault.
t.ifError(err, 'no put error')
})

db.close(function (err) {
// We never get here, due to segfault.
t.ifError(err, 'no close error')
})
})
})

// See https://github.com/Level/leveldown/issues/134
test('iterator() does not segfault if db is not open', function (t) {
t.plan(2)

const db = testCommon.factory()

try {
db.iterator()
} catch (err) {
t.is(err.message, 'cannot call iterator() before open()')
}

db.close(function (err) {
t.ifError(err, 'no close error')
})
})

0 comments on commit 392394d

Please sign in to comment.