Skip to content

Commit 9681c18

Browse files
Fix: Prevent SetDefaultEventParameters from clearing params with all-… (#1747)
* Fix: Prevent SetDefaultEventParameters from clearing params with all-invalid input Addresses code review feedback regarding the behavior of SetDefaultEventParameters when all provided parameters are of invalid types. Previously, if all parameters in the C++ map were invalid, an empty NSDictionary/Bundle would be passed to the native iOS/Android setDefaultEventParameters method. According to documentation, passing nil/null (for iOS/Android respectively) clears all default parameters. While passing an empty dictionary/bundle is a valid operation, it could lead to unintentionally clearing parameters if that was the state before, or it might be a no-op if parameters were already set. This change modifies the iOS and Android implementations to explicitly check if the processed native parameter collection (NSDictionary for iOS, Bundle for Android) is empty after filtering for valid types. If it is empty, the call to the native SDK's setDefaultEventParameters method is skipped. This ensures that providing a C++ map containing exclusively invalid parameters (or an empty map) to SetDefaultEventParameters will be a true no-op from the C++ SDK's perspective, preventing any accidental modification or clearing of existing default parameters on the native side. * Fix: Prevent SetDefaultEventParameters from clearing params with all-invalid input Addresses code review feedback regarding the behavior of SetDefaultEventParameters when all provided parameters are of invalid types. Previously, if all parameters in the C++ map were invalid, an empty NSDictionary/Bundle would be passed to the native iOS/Android setDefaultEventParameters method. According to documentation, passing nil/null (for iOS/Android respectively) clears all default parameters. While passing an empty dictionary/bundle is a valid operation, it could lead to unintentionally clearing parameters if that was the state before, or it might be a no-op if parameters were already set. This change modifies the iOS and Android implementations to explicitly check if the processed native parameter collection (NSDictionary for iOS, Bundle for Android) is effectively empty after filtering for valid types. If it is, the call to the native SDK's setDefaultEventParameters method is skipped. For iOS, this is done by checking `[ns_default_parameters count] == 0`. For Android, this is done by tracking if any parameter was successfully added to the Bundle using a boolean flag, as a direct `bundle.isEmpty()` check before the native call could miss JNI exceptions during bundle population. This ensures that providing a C++ map containing exclusively invalid parameters (or an empty map) to SetDefaultEventParameters will be a true no-op from the C++ SDK's perspective, preventing any accidental modification or clearing of existing default parameters on the native side. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 9def0c1 commit 9681c18

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

analytics/src/analytics_android.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ void SetDefaultEventParameters(
627627
return;
628628
}
629629

630+
bool any_parameter_added = false;
630631
for (const auto& pair : default_parameters) {
631632
const Variant& value = pair.second;
632633
const char* key_cstr = pair.first.c_str();
@@ -648,14 +649,18 @@ void SetDefaultEventParameters(
648649
LogError(
649650
"SetDefaultEventParameters: Failed to put null string for key: %s",
650651
key_cstr);
652+
} else {
653+
any_parameter_added = true;
651654
}
652655
env->DeleteLocalRef(key_jstring);
653656
} else if (value.is_string() || value.is_int64() || value.is_double() ||
654657
value.is_bool()) {
655658
// AddVariantToBundle handles these types and their JNI conversions.
656659
// It also logs if an individual AddToBundle within it fails or if a type
657660
// is unsupported by it.
658-
if (!AddVariantToBundle(env, bundle, key_cstr, value)) {
661+
if (AddVariantToBundle(env, bundle, key_cstr, value)) {
662+
any_parameter_added = true;
663+
} else {
659664
// This specific log gives context that the failure happened during
660665
// SetDefaultEventParameters for a type that was expected to be
661666
// supported by AddVariantToBundle.
@@ -681,6 +686,14 @@ void SetDefaultEventParameters(
681686
}
682687
}
683688

689+
if (!any_parameter_added) {
690+
LogDebug(
691+
"SetDefaultEventParameters: No valid parameters were processed, "
692+
"skipping native call to avoid clearing existing parameters.");
693+
env->DeleteLocalRef(bundle);
694+
return;
695+
}
696+
684697
env->CallVoidMethod(
685698
g_analytics_class_instance,
686699
analytics::GetMethodId(analytics::kSetDefaultEventParameters), bundle);

analytics/src/analytics_ios.mm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,15 @@ void SetDefaultEventParameters(const std::map<std::string, Variant>& default_par
403403
pair.first.c_str(), Variant::TypeName(value.type()));
404404
}
405405
}
406+
// If ns_default_parameters is empty at this point, it means all input
407+
// parameters were invalid or the input map itself was empty.
408+
// In this case, we should not call the native SDK, as passing an empty
409+
// NSDictionary might clear existing parameters, which is not the intent
410+
// if all inputs were simply invalid.
411+
if ([ns_default_parameters count] == 0) {
412+
LogDebug("SetDefaultEventParameters: No valid parameters to set, skipping native call.");
413+
return;
414+
}
406415
[FIRAnalytics setDefaultEventParameters:ns_default_parameters];
407416
}
408417

0 commit comments

Comments
 (0)