From 4dea773c5b62f8fd01cbb56c5d7f50093b0ef180 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Sat, 12 Oct 2024 18:24:37 +0200 Subject: [PATCH] Clamp the hival parameter of Indexed color space to the range [0; 255] Since this value is used to allocate an array, it makes sense to avoid to use too much memory. From the specs, this value must be in the range [0; 255] (see section 8.6.6.3). This patch removes the unused property 'highVal'. --- src/core/colorspace.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/colorspace.js b/src/core/colorspace.js index 149cdf9840d23..7da5daa9df850 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -462,7 +462,7 @@ class ColorSpace { case "I": case "Indexed": baseCS = this._parse(cs[1], xref, resources, pdfFunctionFactory); - const hiVal = xref.fetchIfRef(cs[2]) + 1; + const hiVal = Math.max(0, Math.min(xref.fetchIfRef(cs[2]), 255)); const lookup = xref.fetchIfRef(cs[3]); return new IndexedCS(baseCS, hiVal, lookup); case "Separation": @@ -630,9 +630,8 @@ class IndexedCS extends ColorSpace { constructor(base, highVal, lookup) { super("Indexed", 1); this.base = base; - this.highVal = highVal; - const length = base.numComps * highVal; + const length = base.numComps * (highVal + 1); this.lookup = new Uint8Array(length); if (lookup instanceof BaseStream) {