Skip to content

Commit 51f5ce0

Browse files
mkg20001daviddias
authored andcommitted
fix: ipfs/js-ipfs#1292 - Catch invalid CIDs and return the error via callback (#170)
1 parent fd83f3a commit 51f5ce0

File tree

7 files changed

+26
-3
lines changed

7 files changed

+26
-3
lines changed

benchmarks/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-disable no-console */
23
'use strict'
34

45
const series = require('async/series')

benchmarks/put-get.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint max-nested-callbacks: ["error", 8] */
2+
/* eslint-disable no-console */
23
'use strict'
34

45
const Benchmark = require('benchmark')

src/types/message/index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ BitswapMessage.deserialize = (raw, callback) => {
148148
if (decoded.wantlist) {
149149
decoded.wantlist.entries.forEach((entry) => {
150150
// note: entry.block is the CID here
151-
const cid = new CID(entry.block)
151+
let cid
152+
try {
153+
cid = new CID(entry.block)
154+
} catch (err) {
155+
return callback(err)
156+
}
152157
msg.addEntry(cid, entry.priority, entry.cancel)
153158
})
154159
}
@@ -161,7 +166,12 @@ BitswapMessage.deserialize = (raw, callback) => {
161166
if (err) {
162167
return cb(err)
163168
}
164-
const cid = new CID(hash)
169+
let cid
170+
try {
171+
cid = new CID(hash)
172+
} catch (err) {
173+
return callback(err)
174+
}
165175
msg.addBlock(new Block(b, cid))
166176
cb()
167177
})
@@ -189,7 +199,12 @@ BitswapMessage.deserialize = (raw, callback) => {
189199
return cb(err)
190200
}
191201

192-
const cid = new CID(cidVersion, codecName[multicodec.toString('16')], hash)
202+
let cid
203+
try {
204+
cid = new CID(cidVersion, codecName[multicodec.toString('16')], hash)
205+
} catch (err) {
206+
return callback(err)
207+
}
193208

194209
msg.addBlock(new Block(p.data, cid))
195210
cb()

test/benchmarks/get-many.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
/* eslint-disable no-console */
4+
35
const distributionTest = require('../utils/distribution-test')
46
const print = require('./helpers/print-swarm-results')
57

test/benchmarks/helpers/print-swarm-results.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
/* eslint-disable no-console */
4+
35
const stats = require('stats-lite')
46

57
module.exports = (suite, emitter) => {

test/network/gen-bitswap-network.node.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint max-nested-callbacks: ["error", 8] */
22
/* eslint-env mocha */
3+
/* eslint-disable no-console */
34
'use strict'
45

56
const chai = require('chai')

test/swarms.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
/* eslint-env mocha */
4+
/* eslint-disable no-console */
45

56
const stats = require('stats-lite')
67
const distributionTest = require('./utils/distribution-test')

0 commit comments

Comments
 (0)