Skip to content

Commit 58002be

Browse files
committed
more robust input validation
Closes #53. Closes #52.
1 parent 1531ce2 commit 58002be

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ function pixelmatch(img1, img2, output, width, height, options) {
66

77
if (img1.length !== img2.length) throw new Error('Image sizes do not match.');
88

9+
if (!ArrayBuffer.isView(img1) || !ArrayBuffer.isView(img2) || (output && !ArrayBuffer.isView(output)))
10+
throw new Error('Image data: Uint8Array, Uint8ClampedArray or Buffer expected.');
11+
912
const len = width * height;
1013
const a32 = new Uint32Array(img1.buffer, img1.byteOffset, len);
1114
const b32 = new Uint32Array(img2.buffer, img2.byteOffset, len);

test/test.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ diffTest('6a', '6b', '6diff', 0.05, false, 51);
1515
diffTest('6a', '6a', '6empty', 0, false, 0);
1616

1717
test('throws error if image sizes do not match', (t) => {
18-
t.throws(() => {
19-
match([1, 2, 3], [1, 2, 3, 4], null, 2, 1);
20-
}, /Image sizes do not match/);
18+
t.throws(() => match([1, 2, 3], [1, 2, 3, 4], null, 2, 1), /Image sizes do not match/);
19+
t.end();
20+
});
21+
22+
test('throws error if provided wrong image data format', (t) => {
23+
const re = /Image data: Uint8Array, Uint8ClampedArray or Buffer expected/;
24+
const arr = new Uint8Array(4 * 20 * 20);
25+
const bad = new Array(arr.length).fill(0);
26+
t.throws(() => match(bad, arr, null, 20, 20), re);
27+
t.throws(() => match(arr, bad, null, 20, 20), re);
28+
t.throws(() => match(arr, arr, bad, 20, 20), re);
2129
t.end();
2230
});
2331

0 commit comments

Comments
 (0)