File handle leak in Aspose.PDF for Java 19.7

Hi all,
I’m reporting a small bug I found in Aspose.PDF for Java 19.7.

I attempted to use Aspose.PDF to load a corrupted PDF file. It threw the error “com.aspose.pdf.exceptions.InvalidPdfFileFormatException: Startxref not found”, which is good and expected. The bug is that after throwing that exception, Aspose.PDF did not release the file handle on the corrupted PDF. The handle was not released no matter how long I waited; it was still present 10-30 minutes later.

I noticed this bug on a Windows 10 environment and reproduced it on a Windows Server 2016 and Windows Server 2012 R2 environment as well. Reproduction steps:

  1. Have a corrupted PDF file. Example attached. no_xref.pdf (4.1 KB)
  2. Load the PDF with Aspose.PDF for Java 19.7. I used the String constructor to do so, for example:
    Document pdf = new Document(“C:\path\to\no_xref.pdf”);
  3. Aspose.PDF will throw an exception. Catch it, then have your code try to delete the corrupted file. It will fail to do so as Aspose.PDF has not released the handle to it.

I have not tried to reproduce this with other constructors or other loading errors, but there is a possibility that they leak file handles as well. Thanks for your time.

@BenBruno54

Thank you for contacting support.

We are afraid that the exception is not being reproduced with Aspose.PDF for Java 19.7 in Windows 10 environment and the file is opened fine with Adobe Reader as well. So there does not appear any problem with it. Can you please double check the file and then share your kind comments along with the code you are using to delete it.

Moreover, you can also check validity of a PDF document with PdfFileInfo class, as in below code snippet:

PdfFileInfo info = new PdfFileInfo(dataDir + "no_xref.pdf");

if (info.isPdfFile())
{
    System.out.println("Valid PDF file");
}
else
{
    System.out.println("Invalid PDF file");
}

Hmm… Originally I observed this with a different file, but I can’t provide it since it’d contain sensitive data if repaired. I’ll try attaching another file that might be able to reproduce the issue. corrupted.pdf (3.6 KB)

@BenBruno54

We have been able to reproduce the issue while using below code to delete the file. A ticket with ID PDFJAVA-38791 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

File file = new File(dataDir + "corrupted.pdf");        
if(file.delete()){
    System.out.println("File deleted");
}else System.out.println("Delete error");    

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFJAVA-38791) have been fixed in Aspose.PDF for Java 19.8.