odrefresh: don't stage profile if not exists

The code staged the file even if the open failed. This causes a null
pointer dereference later in JoinFilesAsFDs (only used in the
Compilaiton OS case).

Bug: 186132447
Test: odrefresh --use-compilation-os=10 --force-compile
      # complete successfully with other changes
Change-Id: I8227d863ea56e2a7071816130f6b354ac80d03ce
diff --git a/odrefresh/odrefresh.cc b/odrefresh/odrefresh.cc
index 6fbbfd9..e7eba56 100644
--- a/odrefresh/odrefresh.cc
+++ b/odrefresh/odrefresh.cc
@@ -672,17 +672,18 @@
     args->emplace_back(Concatenate({"--instruction-set=", isa_str}));
   }
 
-  static std::unique_ptr<File> AddDex2OatProfileAndCompilerFilter(
+  static void AddDex2OatProfileAndCompilerFilter(
       /*inout*/ std::vector<std::string>* args,
+      /*inout*/ std::vector<std::unique_ptr<File>>* output_files,
       const std::string& profile_path) {
     std::unique_ptr<File> profile_file(OS::OpenFileForReading(profile_path.c_str()));
     if (profile_file && profile_file->IsOpened()) {
       args->emplace_back(android::base::StringPrintf("--profile-file-fd=%d", profile_file->Fd()));
       args->emplace_back("--compiler-filter=speed-profile");
+      output_files->push_back(std::move(profile_file));
     } else {
       args->emplace_back("--compiler-filter=speed");
     }
-    return profile_file;
   }
 
   static bool AddBootClasspathFds(/*inout*/ std::vector<std::string>& args,
@@ -1041,7 +1042,7 @@
 
     std::vector<std::unique_ptr<File>> readonly_files_raii;
     const std::string boot_profile_file(GetAndroidRoot() + "/etc/boot-image.prof");
-    readonly_files_raii.emplace_back(AddDex2OatProfileAndCompilerFilter(&args, boot_profile_file));
+    AddDex2OatProfileAndCompilerFilter(&args, &readonly_files_raii, boot_profile_file);
 
     // Compile as a single image for fewer files and slightly less memory overhead.
     args.emplace_back("--single-image");
@@ -1182,7 +1183,7 @@
       AddDex2OatInstructionSet(&args, isa);
       const std::string jar_name(android::base::Basename(jar));
       const std::string profile = Concatenate({GetAndroidRoot(), "/framework/", jar_name, ".prof"});
-      readonly_files_raii.emplace_back(AddDex2OatProfileAndCompilerFilter(&args, profile));
+      AddDex2OatProfileAndCompilerFilter(&args, &readonly_files_raii, profile);
 
       const std::string image_location = GetSystemServerImagePath(/*on_system=*/false, jar);
       const std::string install_location = android::base::Dirname(image_location);