Add a null check to method HttpStaticFileServerHandler.sendListing (#10040)

Motivation:

java.io.File.listFiles() may return null and cause a unexpected NPE.

Modification:

Extract a local variable from the return value of File.listFiles() and do a null check.

Result:

Fix the potential NPE.
This commit is contained in:
feijermu 2020-02-18 22:04:19 +08:00 committed by GitHub
parent dcd62c2598
commit 125cd9552d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -286,7 +286,9 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<Ful
.append("<ul>") .append("<ul>")
.append("<li><a href=\"../\">..</a></li>\r\n"); .append("<li><a href=\"../\">..</a></li>\r\n");
for (File f: dir.listFiles()) { File[] files = dir.listFiles();
if (files != null) {
for (File f: files) {
if (f.isHidden() || !f.canRead()) { if (f.isHidden() || !f.canRead()) {
continue; continue;
} }
@ -302,6 +304,7 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler<Ful
.append(name) .append(name)
.append("</a></li>\r\n"); .append("</a></li>\r\n");
} }
}
buf.append("</ul></body></html>\r\n"); buf.append("</ul></body></html>\r\n");