7127235: (fs) NPE in Files.walkFileTree if cached attributes are GC'ed

Alan Bateman Alan.Bateman at oracle.com
Thu Jan 5 07:04:50 PST 2012


This is a faux pas in the implementation of Files.walkFileTree. Where 
the attributes of the files in a directory are read as part of the 
iteration then they are cached, via a weak reference, to avoid 
re-fetching them. Unfortunately this code doesn't check if the reference 
has been cleared which can be lead to a NPE as per the bug report. The 
fix is to trivially check if the reference has been cleared. This one is 
really hard to reproduce and I don't have a reliable test case.

-Alan

diff --git a/src/share/classes/java/nio/file/FileTreeWalker.java 
b/src/share/classes/java/nio/file/FileTreeWalker.java
--- a/src/share/classes/java/nio/file/FileTreeWalker.java
+++ b/src/share/classes/java/nio/file/FileTreeWalker.java
@@ -92,7 +92,7 @@ class FileTreeWalker {
              (System.getSecurityManager() == null))
          {
              BasicFileAttributes cached = 
((BasicFileAttributesHolder)file).get();
-            if (!followLinks || !cached.isSymbolicLink())
+            if (cached != null && (!followLinks || 
!cached.isSymbolicLink()))
                  attrs = cached;
          }
          IOException exc = null;



More information about the nio-dev mailing list