Fix test filtering when running FuzzTest with the separate Centipede binary.

PiperOrigin-RevId: 789899455
diff --git a/centipede/centipede_interface.cc b/centipede/centipede_interface.cc
index fbf90db..bf158f4 100644
--- a/centipede/centipede_interface.cc
+++ b/centipede/centipede_interface.cc
@@ -487,13 +487,22 @@
            "mode";
     fuzz_tests_to_run = fuzztest_config.fuzz_tests_in_current_shard;
   } else {
-    for (int i = 0; i < fuzztest_config.fuzz_tests.size(); ++i) {
+    // TODO: xinhaoyuan - remove this branch after merging the FuzzTest
+    // configuration into Centipede flags.
+    //
+    // We hide shard information when querying the available tests. So we use
+    // `fuzz_tests_in_current_shard` as the full list and shard it here. We
+    // cannot use `fuzz_tests` because it does not take test filter into
+    // account.
+    for (int i = 0; i < fuzztest_config.fuzz_tests_in_current_shard.size();
+         ++i) {
       if (i % total_test_shards == test_shard_index) {
-        fuzz_tests_to_run.push_back(fuzztest_config.fuzz_tests[i]);
+        fuzz_tests_to_run.push_back(
+            fuzztest_config.fuzz_tests_in_current_shard[i]);
       }
     }
   }
-  LOG(INFO) << "Fuzz tests to run:" << absl::StrJoin(fuzz_tests_to_run, ", ");
+  LOG(INFO) << "Fuzz tests to run: " << absl::StrJoin(fuzz_tests_to_run, ", ");
 
   const bool is_workdir_specified = !env.workdir.empty();
   CHECK(!is_workdir_specified || env.fuzztest_single_test_mode);
diff --git a/e2e_tests/corpus_database_test.cc b/e2e_tests/corpus_database_test.cc
index 6a2ed8d..b176acc 100644
--- a/e2e_tests/corpus_database_test.cc
+++ b/e2e_tests/corpus_database_test.cc
@@ -209,6 +209,24 @@
 .*?=== End of summary of detected crashes ===)re")));
 }
 
+TEST_P(UpdateCorpusDatabaseTest, RunsOnFilteredTests) {
+  TempDir corpus_database;
+
+  RunOptions run_options;
+  run_options.fuzztest_flags = {
+      {"corpus_database", corpus_database.path()},
+      {"fuzz_for", "10s"},
+  };
+  run_options.flags = {{GTEST_FLAG_PREFIX_ "filter", "*StackOverflow*"}};
+  auto [status, std_out, std_err] = RunBinaryMaybeWithCentipede(
+      GetCorpusDatabaseTestingBinaryPath(), run_options);
+
+  EXPECT_THAT(std_err,
+              HasSubstr("Fuzzing FuzzTest.FailsWithStackOverflow for 10s"));
+  EXPECT_THAT(std_err,
+              Not(HasSubstr("Fuzzing FuzzTest.FailsInTwoWays for 10s")));
+}
+
 TEST_P(UpdateCorpusDatabaseTest, StartsNewFuzzTestRunsWithoutExecutionIds) {
   TempDir corpus_database;