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,21 +286,24 @@ 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 (f.isHidden() || !f.canRead()) { if (files != null) {
continue; for (File f: files) {
} if (f.isHidden() || !f.canRead()) {
continue;
}
String name = f.getName(); String name = f.getName();
if (!ALLOWED_FILE_NAME.matcher(name).matches()) { if (!ALLOWED_FILE_NAME.matcher(name).matches()) {
continue; continue;
} }
buf.append("<li><a href=\"") buf.append("<li><a href=\"")
.append(name) .append(name)
.append("\">") .append("\">")
.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");