Skip to content

Commit 8e031d2

Browse files
committed
assert uints that are passed to native bindings
1 parent 65bb318 commit 8e031d2

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

index.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const Batch = require('./lib/batch')
2020
const { manifestHash, defaultSignerManifest, createManifest, isCompat, sign } = require('./lib/verifier')
2121
const { ReadStream, WriteStream, ByteStream } = require('./lib/streams')
2222
const {
23+
ASSERTION,
2324
BAD_ARGUMENT,
2425
SESSION_CLOSED,
2526
SESSION_NOT_WRITABLE,
@@ -825,6 +826,7 @@ module.exports = class Hypercore extends EventEmitter {
825826

826827
async seek (bytes, opts) {
827828
if (this.opened === false) await this.opening
829+
if (!isValidIndex(bytes)) throw ASSERTION('seek is invalid')
828830

829831
const tree = (opts && opts.tree) || this.core.tree
830832
const s = tree.seek(bytes, this.padding)
@@ -847,17 +849,17 @@ module.exports = class Hypercore extends EventEmitter {
847849

848850
async has (start, end = start + 1) {
849851
if (this.opened === false) await this.opening
852+
if (!isValidIndex(start) || !isValidIndex(end)) throw ASSERTION('has range is invalid')
850853

851-
const length = end - start
852-
if (length <= 0) return false
853-
if (length === 1) return this.core.bitfield.get(start)
854+
if (end === start + 1) return this.core.bitfield.get(start)
854855

855856
const i = this.core.bitfield.firstUnset(start)
856857
return i === -1 || i >= end
857858
}
858859

859860
async get (index, opts) {
860861
if (this.opened === false) await this.opening
862+
if (!isValidIndex(index)) throw ASSERTION('block index is invalid')
861863

862864
this.tracer.trace('get', { index })
863865

@@ -891,6 +893,8 @@ module.exports = class Hypercore extends EventEmitter {
891893
end = start + 1
892894
}
893895

896+
if (!isValidIndex(start) || !isValidIndex(end)) throw ASSERTION('clear range is invalid')
897+
894898
const cleared = (opts && opts.diff) ? { blocks: 0 } : null
895899

896900
if (start >= end) return cleared
@@ -1156,3 +1160,7 @@ function ensureEncryption (core, opts) {
11561160
function createCache (cache) {
11571161
return cache === true ? new Xache({ maxSize: 65536, maxAge: 0 }) : (cache || null)
11581162
}
1163+
1164+
function isValidIndex (index) {
1165+
return index === 0 || index > 0
1166+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"fast-fifo": "^1.3.0",
5151
"flat-tree": "^1.9.0",
5252
"hypercore-crypto": "^3.2.1",
53-
"hypercore-errors": "^1.1.1",
53+
"hypercore-errors": "^1.2.0",
5454
"hypercore-id-encoding": "^1.2.0",
5555
"hypertrace": "^1.2.1",
5656
"is-options": "^1.0.1",

0 commit comments

Comments
 (0)