豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 24d5ee7

Browse files
committed
fix: validate that the index in the .get() methods is an Array
1 parent f7c10b1 commit 24d5ee7

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

src/utils/array.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,9 @@ export function stretch (arrayToStretch, sizeToStretch, dimToStretch) {
844844
*/
845845
export function get (array, index) {
846846
if (!Array.isArray(array)) { throw new Error('Array expected') }
847+
if (!Array.isArray(index)) {
848+
throw new Error('Array expected for index')
849+
}
847850
const size = arraySize(array)
848851
if (index.length !== size.length) { throw new DimensionError(index.length, size.length) }
849852
for (let x = 0; x < index.length; x++) { validateIndex(index[x], size[x]) }

test/unit-tests/type/matrix/DenseMatrix.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,16 @@ describe('DenseMatrix', function () {
440440
assert.throws(function () { m.get(math.index(1, 1)) })
441441
assert.throws(function () { m.get([[1, 1]]) })
442442
})
443+
444+
it('should throw an error when getting a value given an index that is not an array', function () {
445+
assert.throws(function () {
446+
m.get({ length: 1, reduce: () => {} })
447+
}, /Error: Array expected for index/)
448+
449+
assert.throws(function () {
450+
m.get(new Date())
451+
}, /Error: Array expected for index/)
452+
})
443453
})
444454

445455
describe('set', function () {

test/unit-tests/type/matrix/SparseMatrix.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,21 @@ describe('SparseMatrix', function () {
730730
assert.strictEqual(m.get([4, 5]), 13)
731731
assert.strictEqual(m.get([5, 5]), -1)
732732
})
733+
734+
it('should throw an error when getting a value given an index that is not an array', function () {
735+
const m = new SparseMatrix([
736+
[1, 2],
737+
[3, 4]
738+
])
739+
740+
assert.throws(function () {
741+
m.get({ length: 1, reduce: () => {} })
742+
}, /Error: Array expected/)
743+
744+
assert.throws(function () {
745+
m.get(new Date())
746+
}, /Error: Array expected/)
747+
})
733748
})
734749

735750
describe('set', function () {

test/unit-tests/utils/array.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,16 @@ describe('util.array', function () {
678678
assert.throws(function () { get(m, null) })
679679
assert.throws(function () { get(m, [[1, 1]]) })
680680
})
681+
682+
it('should throw an error when getting a value given an index that is not an array', function () {
683+
assert.throws(function () {
684+
get(m, { length: 1, reduce: () => {} })
685+
}, /Error: Array expected for index/)
686+
687+
assert.throws(function () {
688+
get(m, new Date())
689+
}, /Error: Array expected for index/)
690+
})
681691
})
682692

683693
describe('checkBroadcastingRules', function () {

0 commit comments

Comments
 (0)