Add check that TypeIndex is valid in StringByTypeIdx.

StringByTypeIdx should fail gracefully if given a bad TypeIndex. This
adds a check that the TypeIndex is valid before getting its TypeId.

This fixes a regression that removed this check when it was refactored
in this CL: https://android-review.googlesource.com/#/c/243493/

Bug: 37287051
Test: mm -j31 test-art-host-gtest-dex_file_test
Change-Id: Ib68cb8135011f5f30335251583e181b089982754
diff --git a/runtime/dex_file-inl.h b/runtime/dex_file-inl.h
index e884e39..41db4d8 100644
--- a/runtime/dex_file-inl.h
+++ b/runtime/dex_file-inl.h
@@ -59,11 +59,17 @@
 }
 
 inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const {
+  if (!idx.IsValid()) {
+    return nullptr;
+  }
   const TypeId& type_id = GetTypeId(idx);
   return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length);
 }
 
 inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx) const {
+  if (!idx.IsValid()) {
+    return nullptr;
+  }
   const TypeId& type_id = GetTypeId(idx);
   return StringDataByIdx(type_id.descriptor_idx_);
 }
diff --git a/runtime/dex_file_test.cc b/runtime/dex_file_test.cc
index 3f7461c..f811287 100644
--- a/runtime/dex_file_test.cc
+++ b/runtime/dex_file_test.cc
@@ -591,4 +591,11 @@
   EXPECT_EQ(raw, nullptr);
 }
 
+TEST_F(DexFileTest, GetStringWithNoIndex) {
+  ScratchFile tmp;
+  std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str()));
+  dex::TypeIndex idx;
+  EXPECT_EQ(raw->StringByTypeIdx(idx), nullptr);
+}
+
 }  // namespace art