From 2d714c247a2f094d12347e27dc76d4cc6f28ebfc Mon Sep 17 00:00:00 2001 From: feijermu Date: Tue, 18 Feb 2020 22:04:19 +0800 Subject: [PATCH] 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. --- .../file/HttpStaticFileServerHandler.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java index 0c108b4ec3..4b5521c86b 100644 --- a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java +++ b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java @@ -286,21 +286,24 @@ public class HttpStaticFileServerHandler extends SimpleChannelInboundHandler") .append("
  • ..
  • \r\n"); - for (File f: dir.listFiles()) { - if (f.isHidden() || !f.canRead()) { - continue; - } + File[] files = dir.listFiles(); + if (files != null) { + for (File f: files) { + if (f.isHidden() || !f.canRead()) { + continue; + } - String name = f.getName(); - if (!ALLOWED_FILE_NAME.matcher(name).matches()) { - continue; - } + String name = f.getName(); + if (!ALLOWED_FILE_NAME.matcher(name).matches()) { + continue; + } - buf.append("
  • ") - .append(name) - .append("
  • \r\n"); + buf.append("
  • ") + .append(name) + .append("
  • \r\n"); + } } buf.append("\r\n");