Skip to content

Commit 31b4612

Browse files
committed
Truncate too long /Decode map entries (issue 20668)
1 parent c574694 commit 31b4612

4 files changed

Lines changed: 35 additions & 17 deletions

File tree

src/core/colorspace.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ function copyRgbaImage(src, dest, alpha01) {
116116
}
117117
}
118118

119+
function isDefaultDecodeHelper(decode, expectedLen) {
120+
if (!Array.isArray(decode)) {
121+
return true;
122+
}
123+
const decodeLen = decode.length;
124+
125+
if (decodeLen < expectedLen) {
126+
warn("Decode map length is too short.");
127+
return true;
128+
}
129+
if (decodeLen > expectedLen) {
130+
info("Truncating too long decode map.");
131+
decode.length = expectedLen;
132+
}
133+
return false;
134+
}
135+
119136
class ColorSpace {
120137
static #rgbBuf = new Uint8ClampedArray(3);
121138

@@ -185,8 +202,8 @@ class ColorSpace {
185202
/**
186203
* Refer to the static `ColorSpace.isDefaultDecode` method below.
187204
*/
188-
isDefaultDecode(decodeMap, bpc) {
189-
return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
205+
isDefaultDecode(decode, bpc) {
206+
return ColorSpace.isDefaultDecode(decode, this.numComps);
190207
}
191208

192209
/**
@@ -322,11 +339,7 @@ class ColorSpace {
322339
* @param {number} numComps - Number of components the color space has.
323340
*/
324341
static isDefaultDecode(decode, numComps) {
325-
if (!Array.isArray(decode)) {
326-
return true;
327-
}
328-
if (numComps * 2 !== decode.length) {
329-
warn("The decode map is not the correct length");
342+
if (isDefaultDecodeHelper(decode, numComps * 2)) {
330343
return true;
331344
}
332345
for (let i = 0, ii = decode.length; i < ii; i += 2) {
@@ -424,7 +437,7 @@ class PatternCS extends ColorSpace {
424437
this.base = baseCS;
425438
}
426439

427-
isDefaultDecode(decodeMap, bpc) {
440+
isDefaultDecode(decode, bpc) {
428441
unreachable("Should not call PatternCS.isDefaultDecode");
429442
}
430443
}
@@ -489,19 +502,15 @@ class IndexedCS extends ColorSpace {
489502
return this.base.getOutputLength(inputLength * this.base.numComps, alpha01);
490503
}
491504

492-
isDefaultDecode(decodeMap, bpc) {
493-
if (!Array.isArray(decodeMap)) {
494-
return true;
495-
}
496-
if (decodeMap.length !== 2) {
497-
warn("Decode map length is not correct");
505+
isDefaultDecode(decode, bpc) {
506+
if (isDefaultDecodeHelper(decode, 2)) {
498507
return true;
499508
}
500509
if (!Number.isInteger(bpc) || bpc < 1) {
501510
warn("Bits per component is not correct");
502511
return true;
503512
}
504-
return decodeMap[0] === 0 && decodeMap[1] === (1 << bpc) - 1;
513+
return decode[0] === 0 && decode[1] === (1 << bpc) - 1;
505514
}
506515
}
507516

@@ -1282,7 +1291,7 @@ class LabCS extends ColorSpace {
12821291
return ((inputLength * (3 + alpha01)) / 3) | 0;
12831292
}
12841293

1285-
isDefaultDecode(decodeMap, bpc) {
1294+
isDefaultDecode(decode, bpc) {
12861295
// XXX: Decoding is handled with the lab conversion because of the strange
12871296
// ranges that are used.
12881297
return true;

test/pdfs/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,3 @@
873873
!Brotli-Prototype-FileA.pdf
874874
!bug2013793.pdf
875875
!bug2014080.pdf
876-
!issue20246.pdf

test/pdfs/issue20668.pdf.link

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/user-attachments/files/25338956/CE651A-_-HPP1102-specs.pdf

test/test_manifest.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13945,5 +13945,14 @@
1394513945
"link": true,
1394613946
"lastPage": 1,
1394713947
"type": "eq"
13948+
},
13949+
{
13950+
"id": "issue20668",
13951+
"file": "pdfs/issue20668.pdf",
13952+
"md5": "70233922a9354c3708637902a8e22e70",
13953+
"rounds": 1,
13954+
"link": true,
13955+
"lastPage": 1,
13956+
"type": "eq"
1394813957
}
1394913958
]

0 commit comments

Comments
 (0)