From 26cffd03b0f54a37b22c6a4ecee50fef66aceab5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 11 Apr 2020 15:49:25 +0200 Subject: [PATCH] [src/core/jpg.js] Remove some redundant marker validation during the MCU parsing in the `decodeScan` function Some of the code in `src/core/jpg.js` is fairly old, and has with time become unnecessary when the surrounding code has been updated to handle various types of JPEG corruption. In particular the `if (!marker || marker <= 0xff00) { ... }` branch is now dead code, since: - The `!marker` case can no longer happen, since we would already have broken out of the loop thanks to the `!fileMarker` branch a handful of lines above. - The `marker <= 0xff00` case can also no longer happen, since the `findNextFileMarker` function validate markers much more thoroughly (by checking `marker >= 0xffc0 && marker <= 0xfffe`). Hence we'd again have broken out of the loop via the `!fileMarker` branch above when no valid marker was found. --- src/core/jpg.js | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/core/jpg.js b/src/core/jpg.js index 3433dadce..53a2c1498 100644 --- a/src/core/jpg.js +++ b/src/core/jpg.js @@ -429,23 +429,17 @@ var JpegImage = (function JpegImageClosure() { bitsCount = 0; fileMarker = findNextFileMarker(data, offset); if (!fileMarker) { - // Reached the end of the image data without finding an EOI marker. - break; - } else if (fileMarker.invalid) { + break; // Reached the end of the image data without finding any marker. + } + if (fileMarker.invalid) { // Some bad images seem to pad Scan blocks with e.g. zero bytes, skip // past those to attempt to find a valid marker (fixes issue4090.pdf). warn( - "decodeScan - unexpected MCU data, current marker is: " + - fileMarker.invalid + `decodeScan - unexpected MCU data, current marker is: ${fileMarker.invalid}` ); offset = fileMarker.offset; } - var marker = fileMarker && fileMarker.marker; - if (!marker || marker <= 0xff00) { - throw new JpegError("decodeScan - a valid marker was not found."); - } - - if (marker >= 0xffd0 && marker <= 0xffd7) { + if (fileMarker.marker >= 0xffd0 && fileMarker.marker <= 0xffd7) { // RSTx offset += 2; } else { @@ -458,8 +452,7 @@ var JpegImage = (function JpegImageClosure() { // attempt to find the next valid marker (fixes issue8182.pdf). if (fileMarker && fileMarker.invalid) { warn( - "decodeScan - unexpected Scan data, current marker is: " + - fileMarker.invalid + `decodeScan - unexpected Scan data, current marker is: ${fileMarker.invalid}` ); offset = fileMarker.offset; }