From 4e75bd6d5d74471cbb1966991e6c6076822a1527 Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Fri, 27 Nov 2026 17:30:39 +0000 Subject: [PATCH 1/2] [Remote Config] Initialize ConfigInfo fields and fix Android EnsureInitialized throttled_end_time (#1997) Fixes https://github.com/firebase/firebase-cpp-sdk/issues/1997. - Default-initialize all fields of ConfigInfo in remote_config.h. - Thread throttled_end_time into JConfigInfoToConfigInfo and populate it in Android EnsureInitializedCallback (and guard against null jinfo and JNI exceptions). - Capture throttle end times on Android FetchAndActivate() via FetchAndActivateCallback, store throttled_end_time_ atomically, and drain JNI task callbacks in Cleanup() before ReleaseClasses()/util::Terminate(). - On iOS, store throttled_end_time_in_sec_ atomically strictly before completing futures, only update on positive NSNumber throttle timestamps, clamp negative/non-finite fetch_time, and prevent overflow when converting seconds to milliseconds. - On Desktop, clamp negative MillisecondsSinceEpoch() to 0. - Add unit tests (metadata_test.cc, remote_config_desktop_test.cc), integration test (integration_test.cc), and Upcoming release note. --- release_build_files/readme.md | 5 + .../integration_test/src/integration_test.cc | 19 ++++ .../src/android/remote_config_android.cc | 99 +++++++++++++------ .../src/android/remote_config_android.h | 13 ++- remote_config/src/desktop/rest.cc | 7 +- .../src/include/firebase/remote_config.h | 8 +- remote_config/src/ios/remote_config_ios.h | 3 +- remote_config/src/ios/remote_config_ios.mm | 81 +++++++++------ remote_config/tests/desktop/metadata_test.cc | 8 ++ .../desktop/remote_config_desktop_test.cc | 28 +++++- 10 files changed, 198 insertions(+), 73 deletions(-) diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 3a17522da4..4a225886ae 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -615,6 +615,11 @@ code. ## Release Notes ### Upcoming - Changes + - Remote Config: Fixed `ConfigInfo::throttled_end_time` being left + uninitialized in `RemoteConfig::EnsureInitialized()` on Android, added + default member initializers to `ConfigInfo`, captured throttle end times + from Android `FetchAndActivate()`, and clamped negative/out-of-range + timestamps across Android, iOS, and Desktop. - Database (Desktop): Added exponential backoff to Realtime Database reconnection attempts to prevent tight reconnect loops. - General (Desktop): Fixed Auth and App Check staying registered with their diff --git a/remote_config/integration_test/src/integration_test.cc b/remote_config/integration_test/src/integration_test.cc index 2ed2ba7f00..ed7fa3a16a 100644 --- a/remote_config/integration_test/src/integration_test.cc +++ b/remote_config/integration_test/src/integration_test.cc @@ -507,6 +507,25 @@ TEST_F(FirebaseRemoteConfigTest, TestFetchSecondsParameter) { FLAKY_TEST_SECTION_END(); } +TEST_F(FirebaseRemoteConfigTest, TestEnsureInitialized) { + ASSERT_NE(rc_, nullptr); + + Future future = rc_->EnsureInitialized(); + EXPECT_TRUE(WaitForCompletion(future, "EnsureInitialized")); + EXPECT_EQ(rc_->EnsureInitializedLastResult().error(), 0); + ASSERT_NE(future.result(), nullptr); + + // Max milliseconds past Unix epoch representable by .NET DateTime + // (9999-12-31T23:59:59.999Z); uninitialized stack values typically exceed + // this bound. + constexpr uint64_t kMaxValidEpochMillis = 253402300799999ULL; + const firebase::remote_config::ConfigInfo& info = *future.result(); + const firebase::remote_config::ConfigInfo current_info = rc_->GetInfo(); + EXPECT_LE(info.fetch_time, kMaxValidEpochMillis); + EXPECT_LE(info.throttled_end_time, kMaxValidEpochMillis); + EXPECT_EQ(info.throttled_end_time, current_info.throttled_end_time); +} + TEST_F(FirebaseRemoteConfigTest, TestSetCustomSignals) { ASSERT_NE(rc_, nullptr); diff --git a/remote_config/src/android/remote_config_android.cc b/remote_config/src/android/remote_config_android.cc index 66cf18c821..37f8230f76 100644 --- a/remote_config/src/android/remote_config_android.cc +++ b/remote_config/src/android/remote_config_android.cc @@ -606,20 +606,31 @@ static RemoteConfigError RemoteConfigErrorFromJavaErrorCode(int error_code) { } static void JConfigInfoToConfigInfo(JNIEnv* env, jobject jinfo, + uint64_t throttled_end_time, ConfigInfo* info) { + FIREBASE_DEV_ASSERT(info != nullptr); + info->throttled_end_time = throttled_end_time; + if (!jinfo) { + return; + } FIREBASE_DEV_ASSERT(env->IsInstanceOf(jinfo, config_info::GetClass())); int64_t fetch_time = env->CallLongMethod( jinfo, config_info::GetMethodId(config_info::kGetFetchTimeMillis)); - // The C++ fetch_time is a uint64_t, so if we are given a negative number, - // use 0 instead, to prevent getting a very large number. - if (fetch_time < 0) { + // The C++ fetch_time is a uint64_t, so if we are given a negative number + // (e.g. -1 before any fetch) or a JNI exception occurs, use 0 instead to + // prevent wrapping to a very large number. + if (util::CheckAndClearJniExceptions(env) || fetch_time < 0) { fetch_time = 0; } - info->fetch_time = fetch_time; - util::CheckAndClearJniExceptions(env); - int64_t status_code = env->CallIntMethod( + info->fetch_time = static_cast(fetch_time); + int32_t status_code = env->CallIntMethod( jinfo, config_info::GetMethodId(config_info::kGetLastFetchStatus)); + if (util::CheckAndClearJniExceptions(env)) { + info->last_fetch_status = kLastFetchStatusFailure; + info->last_fetch_failure_reason = kFetchFailureReasonError; + return; + } switch (status_code) { case -1: // FirebaseRemoteConfig.LAST_FETCH_STATUS_SUCCESS info->last_fetch_status = kLastFetchStatusSuccess; @@ -643,7 +654,6 @@ static void JConfigInfoToConfigInfo(JNIEnv* env, jobject jinfo, info->last_fetch_failure_reason = kFetchFailureReasonInvalid; break; } - util::CheckAndClearJniExceptions(env); } template @@ -722,8 +732,11 @@ RemoteConfigInternal::~RemoteConfigInternal() { // ConfigUpdateListenerRegistration instances to no longer point to the // corresponding internal objects. cleanup_notifier().CleanupAll(); - JNIEnv* env = app_.GetJNIEnv(); - util::CancelCallbacks(env, jni_task_id_.c_str()); + if (!callbacks_cancelled_) { + callbacks_cancelled_ = true; + JNIEnv* env = app_.GetJNIEnv(); + util::CancelCallbacks(env, jni_task_id_.c_str()); + } } bool RemoteConfigInternal::Initialized() const { @@ -731,25 +744,52 @@ bool RemoteConfigInternal::Initialized() const { } void RemoteConfigInternal::Cleanup() { + JNIEnv* env = app_.GetJNIEnv(); + // Drain pending JNI task callbacks before releasing cached JNI classes or + // terminating util, and before acquiring ReferenceCountLock to avoid lock + // inversion with completion callbacks. + if (!callbacks_cancelled_) { + callbacks_cancelled_ = true; + util::CancelCallbacks(env, jni_task_id_.c_str()); + } ReferenceCountLock lock(&initializer_); if (lock.RemoveReference() == 1) { - JNIEnv* env = app_.GetJNIEnv(); ReleaseClasses(env); util::Terminate(env); } } +static void UpdateThrottledEndTimeFromResult( + JNIEnv* env, jobject result, util::FutureResult result_code, + RemoteConfigInternal* rc_internal) { + jclass throttled_class = throttled_exception::GetClass(); + if (result_code != util::kFutureResultSuccess && result && rc_internal && + throttled_class && env->IsInstanceOf(result, throttled_class)) { + int64_t throttle_end_time = static_cast(env->CallLongMethod( + result, throttled_exception::GetMethodId( + throttled_exception::kGetThrottleEndTimeMillis))); + if (!util::CheckAndClearJniExceptions(env) && throttle_end_time > 0) { + rc_internal->set_throttled_end_time(throttle_end_time); + } + } +} + void EnsureInitializedCallback(JNIEnv* env, jobject result, util::FutureResult result_code, const char* status_message, void* callback_data) { + auto* data_handle = + reinterpret_cast*>(callback_data); bool success = (result_code == util::kFutureResultSuccess); + uint64_t throttled_end_time = + data_handle->rc_internal ? data_handle->rc_internal->throttled_end_time() + : 0; ConfigInfo info; if (success && result) { - JConfigInfoToConfigInfo(env, result, &info); + JConfigInfoToConfigInfo(env, result, throttled_end_time, &info); + } else { + info.throttled_end_time = throttled_end_time; } - auto* data_handle = - reinterpret_cast*>(callback_data); data_handle->future_api->CompleteWithResult( data_handle->future_handle, @@ -777,6 +817,15 @@ void BoolResultCallback(JNIEnv* env, jobject result, delete data_handle; } +void FetchAndActivateCallback(JNIEnv* env, jobject result, + util::FutureResult result_code, + const char* status_message, void* callback_data) { + auto* data_handle = reinterpret_cast*>(callback_data); + UpdateThrottledEndTimeFromResult(env, result, result_code, + data_handle->rc_internal); + BoolResultCallback(env, result, result_code, status_message, callback_data); +} + void CompleteVoidCallback(JNIEnv* env, jobject result, util::FutureResult result_code, const char* status_message, void* callback_data) { @@ -790,20 +839,9 @@ void CompleteVoidCallback(JNIEnv* env, jobject result, void FetchCallback(JNIEnv* env, jobject result, util::FutureResult result_code, const char* status_message, void* callback_data) { - bool success = (result_code == util::kFutureResultSuccess); - int64_t throttle_end_time = 0; - if (!success && result) { - if (env->IsInstanceOf(result, throttled_exception::GetClass())) { - throttle_end_time = static_cast(env->CallLongMethod( - result, throttled_exception::GetMethodId( - throttled_exception::kGetThrottleEndTimeMillis))); - } - } - auto data_handle = reinterpret_cast*>(callback_data); - if (throttle_end_time > 0) { - data_handle->rc_internal->set_throttled_end_time(throttle_end_time); - } - + auto* data_handle = reinterpret_cast*>(callback_data); + UpdateThrottledEndTimeFromResult(env, result, result_code, + data_handle->rc_internal); CompleteVoidCallback(env, result, result_code, status_message, callback_data); } @@ -882,7 +920,7 @@ Future RemoteConfigInternal::FetchAndActivate() { "FetchAndActivate native function fails"); } else { auto data_handle = new RCDataHandle(&future_impl_, handle, this); - util::RegisterCallbackOnTask(env, task, BoolResultCallback, + util::RegisterCallbackOnTask(env, task, FetchAndActivateCallback, reinterpret_cast(data_handle), jni_task_id_.c_str()); } @@ -1353,13 +1391,12 @@ std::map RemoteConfigInternal::GetAll() { const ConfigInfo RemoteConfigInternal::GetInfo() const { JNIEnv* env = app_.GetJNIEnv(); ConfigInfo info; - info.throttled_end_time = throttled_end_time_; jobject jinfo = env->CallObjectMethod(internal_obj_, config::GetMethodId(config::kGetInfo)); if (util::CheckAndClearJniExceptions(env)) jinfo = nullptr; - JConfigInfoToConfigInfo(env, jinfo, &info); - env->DeleteLocalRef(jinfo); + JConfigInfoToConfigInfo(env, jinfo, throttled_end_time(), &info); + if (jinfo) env->DeleteLocalRef(jinfo); return info; } diff --git a/remote_config/src/android/remote_config_android.h b/remote_config/src/android/remote_config_android.h index 8f85bc1d6d..442d743182 100644 --- a/remote_config/src/android/remote_config_android.h +++ b/remote_config/src/android/remote_config_android.h @@ -17,6 +17,7 @@ #include +#include #include #include @@ -99,7 +100,13 @@ class RemoteConfigInternal { CleanupNotifier& cleanup_notifier() { return cleanup_; } void set_throttled_end_time(int64_t end_time) { - throttled_end_time_ = end_time; + throttled_end_time_.store( + end_time > 0 ? static_cast(end_time) : 0, + std::memory_order_relaxed); + } + + uint64_t throttled_end_time() const { + return throttled_end_time_.load(std::memory_order_relaxed); } void SaveTmpKeysToDefault(std::vector tmp_default_keys) { @@ -125,10 +132,12 @@ class RemoteConfigInternal { // If a fetch was throttled, this is set to the time when throttling is // finished, in milliseconds since epoch. - int64_t throttled_end_time_ = 0; + std::atomic throttled_end_time_{0}; // String to be used when registering for JNI task callbacks. std::string jni_task_id_; + + bool callbacks_cancelled_ = false; }; } // namespace internal diff --git a/remote_config/src/desktop/rest.cc b/remote_config/src/desktop/rest.cc index 4bc5fd218c..265371508d 100644 --- a/remote_config/src/desktop/rest.cc +++ b/remote_config/src/desktop/rest.cc @@ -200,9 +200,10 @@ void RemoteConfigREST::FetchFailure(FetchFailureReason reason) { } uint64_t RemoteConfigREST::MillisecondsSinceEpoch() { - return std::chrono::duration_cast( - std::chrono::system_clock::now().time_since_epoch()) - .count(); + const auto ms = std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count(); + return ms > 0 ? static_cast(ms) : 0; } } // namespace internal diff --git a/remote_config/src/include/firebase/remote_config.h b/remote_config/src/include/firebase/remote_config.h index 148f21d671..2db87a1163 100644 --- a/remote_config/src/include/firebase/remote_config.h +++ b/remote_config/src/include/firebase/remote_config.h @@ -95,17 +95,17 @@ struct ConfigUpdate { struct ConfigInfo { /// @brief The time (in milliseconds since the epoch) that the last fetch /// operation completed. 0 if no fetch attempt has been made yet. - uint64_t fetch_time; + uint64_t fetch_time = 0; /// @brief The status of the last fetch request. - LastFetchStatus last_fetch_status; + LastFetchStatus last_fetch_status = kLastFetchStatusPending; /// @brief The reason the most recent fetch failed. - FetchFailureReason last_fetch_failure_reason; + FetchFailureReason last_fetch_failure_reason = kFetchFailureReasonInvalid; /// @brief The time (in milliseconds since the epoch) when the refreshing of /// Remote Config data is throttled. - uint64_t throttled_end_time; + uint64_t throttled_end_time = 0; }; /// @brief Describes the source a config value was retrieved from. diff --git a/remote_config/src/ios/remote_config_ios.h b/remote_config/src/ios/remote_config_ios.h index c0fb63f20b..b78b181b99 100644 --- a/remote_config/src/ios/remote_config_ios.h +++ b/remote_config/src/ios/remote_config_ios.h @@ -16,6 +16,7 @@ #define FIREBASE_REMOTE_CONFIG_SRC_IOS_REMOTE_CONFIG_IOS_H_ #include +#include #include #include #include @@ -122,7 +123,7 @@ class RemoteConfigInternal { // Saved default keys. std::vector default_keys_; - int64_t throttled_end_time_in_sec_; + std::atomic throttled_end_time_in_sec_{0}; }; } // namespace internal } // namespace remote_config diff --git a/remote_config/src/ios/remote_config_ios.mm b/remote_config/src/ios/remote_config_ios.mm index 46c7d17f9a..4d2df21da1 100644 --- a/remote_config/src/ios/remote_config_ios.mm +++ b/remote_config/src/ios/remote_config_ios.mm @@ -13,8 +13,10 @@ // limitations under the License. #include +#include #include #include +#include #include "remote_config/src/ios/remote_config_ios.h" #include @@ -142,17 +144,36 @@ static void CheckDoubleConversion(FIRRemoteConfigValue *value, ValueInfo *info) return std::vector(bytes, bytes + size); } +static uint64_t ThrottledEndTimeSecondsToMillis(int64_t throttled_end_time) { + // The C++ throttled_end_time is a uint64_t, so if we are given a negative number, + // use 0 instead, to prevent getting a very large number. + if (throttled_end_time <= 0) { + return 0; + } + const uint64_t max_seconds = + std::numeric_limits::max() / ::firebase::internal::kMillisecondsPerSecond; + const uint64_t throttled_seconds = static_cast(throttled_end_time); + return throttled_seconds > max_seconds + ? std::numeric_limits::max() + : throttled_seconds * ::firebase::internal::kMillisecondsPerSecond; +} + static void GetInfoFromFIRRemoteConfig(FIRRemoteConfig *rc_instance, ConfigInfo *out_info, int64_t throttled_end_time) { FIREBASE_DEV_ASSERT(out_info != nullptr); - out_info->fetch_time = round(rc_instance.lastFetchTime.timeIntervalSince1970 * - ::firebase::internal::kMillisecondsPerSecond); - // The C++ throttled_end_time is a uint64_t, so if we are given a negative number, - // use 0 instead, to prevent getting a very large number. - if (throttled_end_time < 0) { - throttled_end_time = 0; + NSTimeInterval fetch_time_sec = + rc_instance.lastFetchTime ? rc_instance.lastFetchTime.timeIntervalSince1970 : 0.0; + double fetch_time_ms = fetch_time_sec * ::firebase::internal::kMillisecondsPerSecond; + // The C++ fetch_time is a uint64_t, so clamp negative or non-finite values + // (e.g. a device clock set before 1970) to 0 instead of wrapping. + if (!std::isfinite(fetch_time_ms) || fetch_time_ms <= 0.0) { + out_info->fetch_time = 0; + } else if (fetch_time_ms >= static_cast(std::numeric_limits::max())) { + out_info->fetch_time = static_cast(std::numeric_limits::max()); + } else { + out_info->fetch_time = static_cast(std::llround(fetch_time_ms)); } - out_info->throttled_end_time = throttled_end_time * ::firebase::internal::kMillisecondsPerSecond; + out_info->throttled_end_time = ThrottledEndTimeSecondsToMillis(throttled_end_time); switch (rc_instance.lastFetchStatus) { case FIRRemoteConfigFetchStatusNoFetchYet: out_info->last_fetch_status = kLastFetchStatusPending; @@ -226,31 +247,39 @@ static ConfigUpdate ConvertConfigUpdateKeys(NSSet *keys) { void RemoteConfigInternal::Cleanup() {} -template -static int64_t FutureCompleteWithError(NSError *error, ReferenceCountedFutureImpl *future_impl, - THandle handle, TResult result) { - int64_t throttled_end_time = 0; +static void UpdateThrottledEndTimeFromError(NSError *error, + std::atomic *throttled_end_time_in_sec) { if (error.userInfo) { - throttled_end_time = - ((NSNumber *)error.userInfo[FIRRemoteConfigThrottledEndTimeInSecondsKey]).longLongValue; + id throttled_value = error.userInfo[FIRRemoteConfigThrottledEndTimeInSecondsKey]; + if ([throttled_value isKindOfClass:[NSNumber class]]) { + int64_t throttled_end_time = static_cast(throttled_value).longLongValue; + if (throttled_end_time > 0) { + throttled_end_time_in_sec->store(throttled_end_time, std::memory_order_relaxed); + } + } } +} + +template +static void FutureCompleteWithError(NSError *error, ReferenceCountedFutureImpl *future_impl, + THandle handle, TResult result) { // If we got an error code back, return that, with the associated string. future_impl->CompleteWithResult(handle, kFutureStatusFailure, util::NSStringToString(error.localizedDescription).c_str(), result); - return throttled_end_time; } Future RemoteConfigInternal::EnsureInitialized() { const auto handle = future_impl_.SafeAlloc(kRemoteConfigFnEnsureInitialized); [impl() ensureInitializedWithCompletionHandler:^(NSError *error) { ConfigInfo config_info; + int64_t throttled_end_time_sec = throttled_end_time_in_sec_.load(std::memory_order_relaxed); if (error) { - throttled_end_time_in_sec_ = - FutureCompleteWithError, ConfigInfo>(error, &future_impl_, - handle, config_info); + config_info.throttled_end_time = ThrottledEndTimeSecondsToMillis(throttled_end_time_sec); + FutureCompleteWithError, ConfigInfo>(error, &future_impl_, + handle, config_info); } else { - GetInfoFromFIRRemoteConfig(impl(), &config_info, throttled_end_time_in_sec_); + GetInfoFromFIRRemoteConfig(impl(), &config_info, throttled_end_time_sec); future_impl_.CompleteWithResult(handle, kFutureStatusSuccess, "", config_info); } }]; @@ -266,8 +295,7 @@ static int64_t FutureCompleteWithError(NSError *error, ReferenceCountedFutureImp const auto handle = future_impl_.SafeAlloc(kRemoteConfigFnActivate); [impl() activateWithCompletion:^(BOOL changed, NSError *error) { if (error) { - throttled_end_time_in_sec_ = FutureCompleteWithError, bool>( - error, &future_impl_, handle, false); + FutureCompleteWithError, bool>(error, &future_impl_, handle, false); } else { future_impl_.CompleteWithResult(handle, kFutureStatusSuccess, "", changed ? true : false); } @@ -284,8 +312,8 @@ static int64_t FutureCompleteWithError(NSError *error, ReferenceCountedFutureImp [impl() fetchAndActivateWithCompletionHandler:^(FIRRemoteConfigFetchAndActivateStatus status, NSError *error) { if (error) { - throttled_end_time_in_sec_ = FutureCompleteWithError, bool>( - error, &future_impl_, handle, false); + UpdateThrottledEndTimeFromError(error, &throttled_end_time_in_sec_); + FutureCompleteWithError, bool>(error, &future_impl_, handle, false); } else if (status == FIRRemoteConfigFetchAndActivateStatusSuccessUsingPreFetchedData) { future_impl_.CompleteWithResult( @@ -310,11 +338,7 @@ static int64_t FutureCompleteWithError(NSError *error, ReferenceCountedFutureImp fetchWithExpirationDuration:cache_expiration_in_seconds completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) { if (error) { - if (error.userInfo) { - throttled_end_time_in_sec_ = - ((NSNumber *)error.userInfo[FIRRemoteConfigThrottledEndTimeInSecondsKey]) - .longLongValue; - } + UpdateThrottledEndTimeFromError(error, &throttled_end_time_in_sec_); // If we got an error code back, return that, with the associated string. future_impl_.Complete( handle, kFutureStatusFailure, @@ -571,7 +595,8 @@ static Variant FIRValueToVariant(FIRRemoteConfigValue *in_value, ValueInfo *valu const ConfigInfo RemoteConfigInternal::GetInfo() const { ConfigInfo config_info; - GetInfoFromFIRRemoteConfig(impl(), &config_info, throttled_end_time_in_sec_); + GetInfoFromFIRRemoteConfig(impl(), &config_info, + throttled_end_time_in_sec_.load(std::memory_order_relaxed)); return config_info; } diff --git a/remote_config/tests/desktop/metadata_test.cc b/remote_config/tests/desktop/metadata_test.cc index e0c49028b9..4c47556be9 100644 --- a/remote_config/tests/desktop/metadata_test.cc +++ b/remote_config/tests/desktop/metadata_test.cc @@ -51,6 +51,14 @@ TEST(RemoteConfigMetadataTest, Serialization) { EXPECT_EQ(remote_config_metadata, new_remote_config_metadata); } +TEST(RemoteConfigMetadataTest, ConfigInfoDefaultInitialized) { + ConfigInfo info; + EXPECT_EQ(info.fetch_time, 0u); + EXPECT_EQ(info.last_fetch_status, kLastFetchStatusPending); + EXPECT_EQ(info.last_fetch_failure_reason, kFetchFailureReasonInvalid); + EXPECT_EQ(info.throttled_end_time, 0u); +} + TEST(RemoteConfigMetadataTest, GetInfoDefaultValues) { RemoteConfigMetadata m; ExpectEqualConfigInfo(m.info(), ConfigInfo({0, kLastFetchStatusSuccess, diff --git a/remote_config/tests/desktop/remote_config_desktop_test.cc b/remote_config/tests/desktop/remote_config_desktop_test.cc index 910ad938c3..9db8297060 100644 --- a/remote_config/tests/desktop/remote_config_desktop_test.cc +++ b/remote_config/tests/desktop/remote_config_desktop_test.cc @@ -207,7 +207,9 @@ TEST_F(RemoteConfigDesktopTest, GetAndSetConfigSetting) { } TEST_F(RemoteConfigDesktopTest, GetBoolean) { - { EXPECT_FALSE(instance_->GetBoolean("key_bool", nullptr)); } + { + EXPECT_FALSE(instance_->GetBoolean("key_bool", nullptr)); + } { ValueInfo info; EXPECT_FALSE(instance_->GetBoolean("key_bool", &info)); @@ -217,7 +219,9 @@ TEST_F(RemoteConfigDesktopTest, GetBoolean) { } TEST_F(RemoteConfigDesktopTest, GetLong) { - { EXPECT_EQ(instance_->GetLong("key_long", nullptr), 55555); } + { + EXPECT_EQ(instance_->GetLong("key_long", nullptr), 55555); + } { ValueInfo info; EXPECT_EQ(instance_->GetLong("key_long", &info), 55555); @@ -227,7 +231,9 @@ TEST_F(RemoteConfigDesktopTest, GetLong) { } TEST_F(RemoteConfigDesktopTest, GetDouble) { - { EXPECT_EQ(instance_->GetDouble("key_double", nullptr), 100.5); } + { + EXPECT_EQ(instance_->GetDouble("key_double", nullptr), 100.5); + } { ValueInfo info; EXPECT_EQ(instance_->GetDouble("key_double", &info), 100.5); @@ -237,7 +243,9 @@ TEST_F(RemoteConfigDesktopTest, GetDouble) { } TEST_F(RemoteConfigDesktopTest, GetString) { - { EXPECT_EQ(instance_->GetString("key_string", nullptr), "aaa"); } + { + EXPECT_EQ(instance_->GetString("key_string", nullptr), "aaa"); + } { ValueInfo info; EXPECT_EQ(instance_->GetString("key_string", &info), "aaa"); @@ -290,6 +298,18 @@ TEST_F(RemoteConfigDesktopTest, GetInfo) { EXPECT_EQ(info.throttled_end_time, 1498758888); } +TEST_F(RemoteConfigDesktopTest, EnsureInitialized) { + Future future = instance_->EnsureInitialized(); + EXPECT_EQ(future.status(), kFutureStatusComplete); + EXPECT_EQ(future.error(), kFutureStatusSuccess); + ASSERT_NE(future.result(), nullptr); + const ConfigInfo& info = *future.result(); + EXPECT_EQ(info.fetch_time, 1498757224u); + EXPECT_EQ(info.last_fetch_status, kLastFetchStatusPending); + EXPECT_EQ(info.last_fetch_failure_reason, kFetchFailureReasonThrottled); + EXPECT_EQ(info.throttled_end_time, 1498758888u); +} + TEST_F(RemoteConfigDesktopTest, ActivateFetched) { { SetUpInstance(); From ef485a8d797582d2af50d4cd69c69741ea66c0bb Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Fri, 27 Nov 2026 19:16:07 +0000 Subject: [PATCH 2/2] [Remote Config] Address adversarial review findings - Revert 4 unrelated clang-format hunks in remote_config_desktop_test.cc (fixes CI file_format_check). - Poison storage with 0xAA + placement-new default-init in ConfigInfoDefaultInitialized test for mutation resistance. - Guard non-finite/out-of-range NSNumber doubles in iOS UpdateThrottledEndTimeFromError. - Clarify Cleanup() comment ('Cancel' instead of 'Drain') to match util::CancelCallbacks semantics. --- .../src/android/remote_config_android.cc | 2 +- remote_config/src/ios/remote_config_ios.mm | 12 +++++++++--- remote_config/tests/desktop/metadata_test.cc | 15 ++++++++++----- .../tests/desktop/remote_config_desktop_test.cc | 16 ++++------------ 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/remote_config/src/android/remote_config_android.cc b/remote_config/src/android/remote_config_android.cc index 37f8230f76..5e645c4124 100644 --- a/remote_config/src/android/remote_config_android.cc +++ b/remote_config/src/android/remote_config_android.cc @@ -745,7 +745,7 @@ bool RemoteConfigInternal::Initialized() const { void RemoteConfigInternal::Cleanup() { JNIEnv* env = app_.GetJNIEnv(); - // Drain pending JNI task callbacks before releasing cached JNI classes or + // Cancel pending JNI task callbacks before releasing cached JNI classes or // terminating util, and before acquiring ReferenceCountLock to avoid lock // inversion with completion callbacks. if (!callbacks_cancelled_) { diff --git a/remote_config/src/ios/remote_config_ios.mm b/remote_config/src/ios/remote_config_ios.mm index 4d2df21da1..d19332787c 100644 --- a/remote_config/src/ios/remote_config_ios.mm +++ b/remote_config/src/ios/remote_config_ios.mm @@ -252,9 +252,15 @@ static void UpdateThrottledEndTimeFromError(NSError *error, if (error.userInfo) { id throttled_value = error.userInfo[FIRRemoteConfigThrottledEndTimeInSecondsKey]; if ([throttled_value isKindOfClass:[NSNumber class]]) { - int64_t throttled_end_time = static_cast(throttled_value).longLongValue; - if (throttled_end_time > 0) { - throttled_end_time_in_sec->store(throttled_end_time, std::memory_order_relaxed); + double throttled_end_time_sec = static_cast(throttled_value).doubleValue; + if (std::isfinite(throttled_end_time_sec) && throttled_end_time_sec > 0.0) { + int64_t throttled_end_time = + throttled_end_time_sec >= static_cast(std::numeric_limits::max()) + ? std::numeric_limits::max() + : static_cast(throttled_end_time_sec); + if (throttled_end_time > 0) { + throttled_end_time_in_sec->store(throttled_end_time, std::memory_order_relaxed); + } } } } diff --git a/remote_config/tests/desktop/metadata_test.cc b/remote_config/tests/desktop/metadata_test.cc index 4c47556be9..6b32384bbc 100644 --- a/remote_config/tests/desktop/metadata_test.cc +++ b/remote_config/tests/desktop/metadata_test.cc @@ -14,7 +14,9 @@ #include "remote_config/src/desktop/metadata.h" +#include #include +#include #include #include "gtest/gtest.h" @@ -52,11 +54,14 @@ TEST(RemoteConfigMetadataTest, Serialization) { } TEST(RemoteConfigMetadataTest, ConfigInfoDefaultInitialized) { - ConfigInfo info; - EXPECT_EQ(info.fetch_time, 0u); - EXPECT_EQ(info.last_fetch_status, kLastFetchStatusPending); - EXPECT_EQ(info.last_fetch_failure_reason, kFetchFailureReasonInvalid); - EXPECT_EQ(info.throttled_end_time, 0u); + alignas(ConfigInfo) unsigned char storage[sizeof(ConfigInfo)]; + std::memset(storage, 0xAA, sizeof(storage)); + ConfigInfo* info = new (storage) ConfigInfo; + EXPECT_EQ(info->fetch_time, 0u); + EXPECT_EQ(info->last_fetch_status, kLastFetchStatusPending); + EXPECT_EQ(info->last_fetch_failure_reason, kFetchFailureReasonInvalid); + EXPECT_EQ(info->throttled_end_time, 0u); + info->~ConfigInfo(); } TEST(RemoteConfigMetadataTest, GetInfoDefaultValues) { diff --git a/remote_config/tests/desktop/remote_config_desktop_test.cc b/remote_config/tests/desktop/remote_config_desktop_test.cc index 9db8297060..e24da69e96 100644 --- a/remote_config/tests/desktop/remote_config_desktop_test.cc +++ b/remote_config/tests/desktop/remote_config_desktop_test.cc @@ -207,9 +207,7 @@ TEST_F(RemoteConfigDesktopTest, GetAndSetConfigSetting) { } TEST_F(RemoteConfigDesktopTest, GetBoolean) { - { - EXPECT_FALSE(instance_->GetBoolean("key_bool", nullptr)); - } + { EXPECT_FALSE(instance_->GetBoolean("key_bool", nullptr)); } { ValueInfo info; EXPECT_FALSE(instance_->GetBoolean("key_bool", &info)); @@ -219,9 +217,7 @@ TEST_F(RemoteConfigDesktopTest, GetBoolean) { } TEST_F(RemoteConfigDesktopTest, GetLong) { - { - EXPECT_EQ(instance_->GetLong("key_long", nullptr), 55555); - } + { EXPECT_EQ(instance_->GetLong("key_long", nullptr), 55555); } { ValueInfo info; EXPECT_EQ(instance_->GetLong("key_long", &info), 55555); @@ -231,9 +227,7 @@ TEST_F(RemoteConfigDesktopTest, GetLong) { } TEST_F(RemoteConfigDesktopTest, GetDouble) { - { - EXPECT_EQ(instance_->GetDouble("key_double", nullptr), 100.5); - } + { EXPECT_EQ(instance_->GetDouble("key_double", nullptr), 100.5); } { ValueInfo info; EXPECT_EQ(instance_->GetDouble("key_double", &info), 100.5); @@ -243,9 +237,7 @@ TEST_F(RemoteConfigDesktopTest, GetDouble) { } TEST_F(RemoteConfigDesktopTest, GetString) { - { - EXPECT_EQ(instance_->GetString("key_string", nullptr), "aaa"); - } + { EXPECT_EQ(instance_->GetString("key_string", nullptr), "aaa"); } { ValueInfo info; EXPECT_EQ(instance_->GetString("key_string", &info), "aaa");