Menu

#89 Regression: DefaultFileInfo created in CardServiceException handler causes NPE for non-existent files

v1.0_(example)
open
None
1
2026-03-02
2026-02-26
Anonymous
No

After the latest change in revision 1910(https://sourceforge.net/p/jmrtd/code/1910/), a regression was introduced in the file selection logic.
The following code was added:
} catch (CardServiceException cse) {
fileInfo = new DefaultFileInfo(selectedFID, -1);
fileInfos.put(selectedFID, fileInfo);
return fileInfo;
}
Later, when the code assumes that the selected file really exists and tries to use this fileInfo to read from the card (e.g. via READ BINARY or similar operations), a NullPointerException occurs, since the file was never present and the previous CardServiceException was effectively ignored.
Before this change, a CardServiceException correctly indicated that the file could not be selected, so the caller could handle the error (e.g. by skipping the optional file).
Now the error is hidden by constructing a synthetic DefaultFileInfo with length -1, which misleads the rest of the code into thinking the file exists.

Steps to reproduce:

  1. Use JMRTD 0.8.5
  2. Try to select a file (FID) that is not present on the eMRTD, so that the card returns an error status word and a CardServiceException is normally thrown - getInputStream(fid, PassportService.DEFAULT_MAX_BLOCKSIZE)
  3. Because of the new catch (CardServiceException) code, a DefaultFileInfo(selectedFID, -1) is created and returned instead of propagating the exception.
  4. Call code that relies on this fileInfo to read the file contents(e.g. getLength).
  5. Observe a NullPointerException (or similar runtime error) when the code attempts to access data for this non‑existent file.

Discussion

  • Martijn Oostdijk

    Do you have a (partial) stack trace?

     
    • Maksim

      Maksim - 2026-03-02

      Sure, here you are:

                   java.lang.NullPointerException: Attempt to invoke virtual method 'int org.jmrtd.io.FragmentBuffer.getLength()' on a null object reference
                      at org.jmrtd.DefaultFileSystem$DefaultFileInfo.getFileLength(DefaultFileSystem.java:480)
                      at net.sf.scuba.smartcards.CardFileInputStream.<init>(CardFileInputStream.java:65)
                      at org.jmrtd.PassportService.getInputStream(PassportService.java:656)
      
       

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB