Fix handling unknown branches when parsing switch instructions.

The bitcode reader for the switch insruction did not check if the
branch labels were defined. This patch fixes the problem. Includes
test for such a case.

BUG=None
R=stichnot@chromium.org

Review URL: https://codereview.chromium.org/1303003002 .
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp
index 259c62e..7368a2b 100644
--- a/src/PNaClTranslator.cpp
+++ b/src/PNaClTranslator.cpp
@@ -2415,6 +2415,8 @@
     }
     Ice::CfgNode *DefaultLabel =
         isIRGenDisabled ? nullptr : getBranchBasicBlock(Values[2]);
+    if (DefaultLabel == nullptr)
+      return;
     uint64_t NumCasesRaw = Values[3];
     if (NumCasesRaw > std::numeric_limits<uint32_t>::max()) {
       std::string Buffer;
@@ -2428,10 +2430,10 @@
     // Now recognize each of the cases.
     if (!isValidRecordSize(4 + NumCases * 4, "switch"))
       return;
-    Ice::InstSwitch *Switch =
-        isIRGenDisabled
-            ? nullptr
-            : Ice::InstSwitch::create(Func.get(), NumCases, Cond, DefaultLabel);
+    std::unique_ptr<Ice::InstSwitch> Switch(
+        isIRGenDisabled ? nullptr
+                        : Ice::InstSwitch::create(Func.get(), NumCases, Cond,
+                                                  DefaultLabel));
     unsigned ValCaseIndex = 4; // index to beginning of case entry.
     for (uint32_t CaseIndex = 0; CaseIndex < NumCases;
          ++CaseIndex, ValCaseIndex += 4) {
@@ -2448,11 +2450,13 @@
       if (isIRGenDisabled)
         continue;
       Ice::CfgNode *Label = getBranchBasicBlock(Values[ValCaseIndex + 3]);
+      if (Label == nullptr)
+        return;
       Switch->addBranch(CaseIndex, Value.getSExtValue(), Label);
     }
     if (isIRGenDisabled)
       return;
-    CurrentNode->appendInst(Switch);
+    CurrentNode->appendInst(Switch.release());
     return;
   }
   case naclbitc::FUNC_CODE_INST_UNREACHABLE: {
diff --git a/tests_lit/parse_errs/Inputs/bad-switch-case.tbc b/tests_lit/parse_errs/Inputs/bad-switch-case.tbc
new file mode 100644
index 0000000..27b9dd5
--- /dev/null
+++ b/tests_lit/parse_errs/Inputs/bad-switch-case.tbc
@@ -0,0 +1,25 @@
+65535,8,2;
+1,1;
+65535,17,2;
+1,3;
+2;
+7,32;
+21,0,0,1;
+65534;
+8,2,0,0,0;
+65535,19,2;
+5,0;
+65534;
+65535,14,2;
+1,0,83,119,105,116,99,104,86,97,114,105,97,98,108,101;
+65534;
+65535,12,2;
+1,6;
+12,1,1,2,2,1,1,2,3105555534,1,1,8,4;
+11,5;
+11,5;
+11,5;
+11,5;
+10;
+65534;
+65534;
diff --git a/tests_lit/parse_errs/bad-switch-case.test b/tests_lit/parse_errs/bad-switch-case.test
new file mode 100644
index 0000000..b8d090a
--- /dev/null
+++ b/tests_lit/parse_errs/bad-switch-case.test
@@ -0,0 +1,6 @@
+; REQUIRES: no_minimal_build
+; RUN: not %pnacl_sz -bitcode-as-text %p/Inputs/bad-switch-case.tbc \
+; RUN:     -bitcode-format=pnacl -notranslate -build-on-read 2>&1 \
+; RUN:   | FileCheck -check-prefix=BAD-SWITCH-CASE %s
+
+; BAD-SWITCH-CASE: Reference to basic block 3105555534 not found. Must be less than 6