Selaa lähdekoodia

HDFS-14172. Avoid NPE when SectionName#fromString returns null. Contributed by Xiang Li.

Yiqun Lin 6 vuotta sitten
vanhempi
commit
177131793a

+ 5 - 2
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageFormatProtobuf.java

@@ -241,8 +241,11 @@ public final class FSImageFormatProtobuf {
             summary.getCodec(), in);
 
         String n = s.getName();
-
-        switch (SectionName.fromString(n)) {
+        SectionName sectionName = SectionName.fromString(n);
+        if (sectionName == null) {
+          throw new IOException("Unrecognized section " + n);
+        }
+        switch (sectionName) {
         case NS_INFO:
           loadNameSystemSection(in);
           break;

+ 7 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/FSImageLoader.java

@@ -156,7 +156,13 @@ class FSImageLoader {
           LOG.debug("Loading section " + s.getName() + " length: " + s.getLength
               ());
         }
-        switch (FSImageFormatProtobuf.SectionName.fromString(s.getName())) {
+
+        FSImageFormatProtobuf.SectionName sectionName
+            = FSImageFormatProtobuf.SectionName.fromString(s.getName());
+        if (sectionName == null) {
+          throw new IOException("Unrecognized section " + s.getName());
+        }
+        switch (sectionName) {
           case STRING_TABLE:
             stringTable = loadStringTable(is);
             break;

+ 5 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/PBImageTextWriter.java

@@ -594,7 +594,11 @@ abstract class PBImageTextWriter implements Closeable {
         is = FSImageUtil.wrapInputStreamForCompression(conf,
             summary.getCodec(), new BufferedInputStream(new LimitInputStream(
                 fin, section.getLength())));
-        switch (SectionName.fromString(section.getName())) {
+        SectionName sectionName = SectionName.fromString(section.getName());
+        if (sectionName == null) {
+          throw new IOException("Unrecognized section " + section.getName());
+        }
+        switch (sectionName) {
         case STRING_TABLE:
           LOG.info("Loading string table");
           stringTable = FSImageLoader.loadStringTable(is);

+ 5 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/PBImageXmlWriter.java

@@ -326,7 +326,11 @@ public final class PBImageXmlWriter {
             summary.getCodec(), new BufferedInputStream(new LimitInputStream(
                 fin, s.getLength())));
 
-        switch (SectionName.fromString(s.getName())) {
+        SectionName sectionName = SectionName.fromString(s.getName());
+        if (sectionName == null) {
+          throw new IOException("Unrecognized section " + s.getName());
+        }
+        switch (sectionName) {
         case NS_INFO:
           dumpNameSection(is);
           break;