Add dex2oat validity check for output file open

Test: manual with bad output path
Change-Id: I607071b8cdfc93fb9b1a6abc81ca69eca094c295
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 4a66110..2f90ec5 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -2188,12 +2188,16 @@
 
         TimingLogger::ScopedTiming t("dex2oat OatFile copy", timings_);
         std::unique_ptr<File>& in = oat_files_[i];
-        std::unique_ptr<File> out(OS::CreateEmptyFile(oat_unstripped_[i].c_str()));
         int64_t in_length = in->GetLength();
         if (in_length < 0) {
           PLOG(ERROR) << "Failed to get the length of oat file: " << in->GetPath();
           return false;
         }
+        std::unique_ptr<File> out(OS::CreateEmptyFile(oat_unstripped_[i].c_str()));
+        if (out == nullptr) {
+          PLOG(ERROR) << "Failed to open oat file for writing: " << oat_unstripped_[i];
+          return false;
+        }
         if (!out->Copy(in.get(), 0, in_length)) {
           PLOG(ERROR) << "Failed to copy oat file to file: " << out->GetPath();
           return false;