Skip to content

Commit f24f016

Browse files
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.
1 parent 9def0c1 commit f24f016

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

analytics/src/analytics_android.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,28 @@ void SetDefaultEventParameters(
681681
}
682682
}
683683

684+
// Check if the bundle is empty. If so, it means all input parameters were
685+
// invalid or the input map itself was empty. In this case, we should not
686+
// call the native SDK, as passing an empty Bundle might clear existing
687+
// parameters, which is not the intent if all inputs were simply invalid.
688+
jboolean is_bundle_empty = env->CallBooleanMethod(
689+
bundle, util::bundle::GetMethodId(util::bundle::kIsEmpty));
690+
if (util::CheckAndClearJniExceptions(env)) {
691+
LogError(
692+
"SetDefaultEventParameters: Failed to check if Bundle is empty. "
693+
"Skipping native call to avoid potential issues.");
694+
env->DeleteLocalRef(bundle);
695+
return;
696+
}
697+
698+
if (is_bundle_empty) {
699+
LogDebug(
700+
"SetDefaultEventParameters: No valid parameters to set, skipping "
701+
"native call.");
702+
env->DeleteLocalRef(bundle);
703+
return;
704+
}
705+
684706
env->CallVoidMethod(
685707
g_analytics_class_instance,
686708
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)