You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the PDF file contains an image which cannot be decoded, rendering fails trying to dereference a null object.
Unfortunately, I cannot attach the PDF file due to NDA but I did some debugging and here are my
findings:
When PartialEvaluator#buildPaintImageXObject() fails to decode an image it sends null as imgData to the main thread this._sendImgData(objId, /* imgData = */ null, cacheGlobally);.
The WorkerTransport object receives null in the imageData parameter in the "obj" handler and
calls this.objs.resolve(id, imageData); where imageData == null in the case "Image"
branch.
The PDFObjects#resolve() method replaces INITIAL_DATA in obj.data with null.
The PDFObjects#*[Symbol.iterator]() method filters out only data === INITIAL_DATA but not data == null.
The case "CopyLocalImage" branch in the "commonobj" handler iterates over pageProxy.objs:
for(const[,data]ofpageProxy.objs){if(data.ref!==imageRef){// `data` can be `null` here.continue;}
...
}
and because data can be null the code crashes trying to access the ref property with an error
message in the console:
A simple fix data.ref !== imageRef -> data?.ref !== imageRef makes the PDF renderable again but
probably it just fixes the symptom, not the cause of the issue.
The text was updated successfully, but these errors were encountered:
plantago
changed the title
Rendering crashes if PDF contains shared images which cannot be decoded
Rendering crashes if PDF contains images which cannot be decoded
May 2, 2024
Can the data property be null? Yes, it can. Should the data be tested for null before accessing property ref? I think the answer is obvious.
E.g. the PDFObjects.clear() method checks data for the null value:
clear(){for(constobjIdinthis.#objs){const{ data }=this.#objs[objId];data?.bitmap?.close();// Release any `ImageBitmap` data.}this.#objs=Object.create(null);}
If pdf.js were written in TypeScript and the PDFObjects.#objs were properly defined as a nullable type then this code wouldn't even compile:
for(const[,data]ofpageProxy.objs){if(data.ref!==imageRef){// TypeScript would show an error here: 'data' is possibly 'null'.ts(18047)continue;}
...
}
Attach (recommended) or Link to PDF file here: I cannot provide the PDF file due to NDA.
Configuration:
If the PDF file contains an image which cannot be decoded, rendering fails trying to dereference a
null
object.Unfortunately, I cannot attach the PDF file due to NDA but I did some debugging and here are my
findings:
When
PartialEvaluator#buildPaintImageXObject()
fails to decode an image it sendsnull
asimgData
to the main threadthis._sendImgData(objId, /* imgData = */ null, cacheGlobally);
.The
WorkerTransport
object receivesnull
in theimageData
parameter in the"obj"
handler andcalls
this.objs.resolve(id, imageData);
whereimageData == null
in thecase "Image"
branch.
The
PDFObjects#resolve()
method replacesINITIAL_DATA
inobj.data
withnull
.The
PDFObjects#*[Symbol.iterator]()
method filters out onlydata === INITIAL_DATA
but notdata == null
.The
case "CopyLocalImage"
branch in the"commonobj"
handler iterates overpageProxy.objs
:and because
data
can benull
the code crashes trying to access theref
property with an errormessage in the console:
A simple fix
data.ref !== imageRef
->data?.ref !== imageRef
makes the PDF renderable again butprobably it just fixes the symptom, not the cause of the issue.
The text was updated successfully, but these errors were encountered: