Uploaded image for project: 'Undertow'
  1. Undertow
  2. UNDERTOW-2011

NPE from PathResource.getName() for drive root

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 2.2.15.Final
    • None
    • Core
    • None

      If a PathResource represents a drive root such as C:/ the getName() method throws a null pointer exception.

       

      public String getName() {
          return file.getFileName().toString();
      } 

      Javadocs for Path.getFileName() state the method will return "null if this path has zero elements"

      https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#getFileName()

      Therefore, Undertow should not assume that it will get a non-null value back from getFileName().

      I noticed this issue when using the CachedResourceManager which creates a CachedResource object whose name matches that of the PathResource.

       

      java.lang.NullPointerException
              at io.undertow.server.handlers.resource.PathResource.getName(PathResource.java:81)
              at io.undertow.server.handlers.resource.CachedResource.<init>(CachedResource.java:66)
              at io.undertow.server.handlers.resource.CachingResourceManager.getResource(CachingResourceManager.java:122)
              at io.undertow.server.handlers.resource.CachingResourceManager.getResource(CachingResourceManager.java:32)
              at io.undertow.servlet.handlers.ServletPathMatches.getServletHandlerByPath(ServletPathMatches.java:141)
              at io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:147) 

      Therefore, I would recommend instead of returning null for the PathResource name, we return the full Path.toString() representation for a meaningful name.

          public String getName() {
            if( file.getFileName() != null ) {
              return file.getFileName().toString();
            }
            return file.toString();
          } 

      Pull incoming from my cohort, cerberus23 .

       

            flaviarnn Flavia Rainone
            bdw429s Brad Wood (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: