[ 
https://issues.apache.org/jira/browse/PDFBOX-6357?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18154732#comment-18154732
 ] 

Tilman Hausherr commented on PDFBOX-6357:
-----------------------------------------

How about this?
{code}
public static PDDocument loadPDF(File file, String password, InputStream 
keyStore, String alias,
            StreamCacheCreateFunction streamCacheCreateFunction) throws 
IOException
    {
        RandomAccessRead raFile = null;
        boolean success = false;
        try
        {
            // RandomAccessRead is not closed here, may be needed for signing
            raFile = new RandomAccessReadBufferedFile(file);
            PDDocument doc = Loader.loadPDF(raFile, password, keyStore, alias, 
streamCacheCreateFunction);
            success = true;
            return doc;
        }
        catch (IOException ioe)
        {
            throw ioe;
        }
        finally
        {
            if (!success)
            {
                IOUtils.closeQuietly(raFile);
            }
        }
    }
{code}

> Loader.loadPDF should close a file it opened if a stream-cache factory throws
> -----------------------------------------------------------------------------
>
>                 Key: PDFBOX-6357
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-6357
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 3.0.8 PDFBox
>            Reporter: Qiuye Wang
>            Priority: Minor
>
> Version reviewed: pdfbox 3.0.8
> The file overload opens a random-access source before parsing. Its failure 
> cleanup handles IOException. A user-supplied stream-cache factory can throw 
> an unchecked exception during document construction, before a PDDocument is 
> returned, leaving the file source without a matching close.
> Apply the same ownership rule when parsing exits with an unchecked exception. 
> This concerns files opened by the loader and a failing custom cache factory.
> https://github.com/apache/pdfbox/blob/9286e47d89d6877005c9d2d0f2fd38793a62519a/pdfbox/src/main/java/org/apache/pdfbox/Loader.java#L351-L366
> https://github.com/apache/pdfbox/blob/9286e47d89d6877005c9d2d0f2fd38793a62519a/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java#L140-L155
> Based on source inspection. This report was drafted with AI assistance.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to