Skip to content

Conversation

jkasten2
Copy link
Member

@jkasten2 jkasten2 commented May 9, 2025

Description

One Line Summary

When passing some labels to User.addAlias, such as "AL", would cause a crash when starting the app the next time.

Details

This is due to casting the wrong type as initializeFromJson's code used a contains instead of an equals which can result in finding the wrong function.

To correct this switched this to equals("get$property", true) instead and also added a check for it.parameterCount == 0 as we only want to cast based on getters.

Lastly also removed the if (method == null) check to simplify the code, as the when statement can handle this case.

Related

This address issue #2279

Motivation

Valid input values to the SDK should not cause crashes.

Scope

Affects casting logic loading Models from storage on app startup.

Testing

Unit testing

Also added tests for IdentityModel with a number of labels to ensure this code changes covers common label values.

Manual testing

Tested on an Android 14 emulator, added an alias with the label "AL", reproduced crash, and ensured no longer crashes after the fix.

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • If it is hard to explain how any codes changes are related to each other then it most likely needs to be more than one PR
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

When passing some labels to User.addAlias, such as "AL" this could
cause a crash when starting the app the next time. This is due to
casting the wrong type as initializeFromJson's code used a contains
instead of an equals which can result in finding the wrong function.

To correct this switched this to equals("get$property", true) instead
and also added a check for it.parameterCount == 0 as we only want to
cast based on getters.

Also added tests for IdentityModel with a number of labels to ensure
this code changes covers common label values.

Lastly also removed the if (method == null) check to simplify the code,
as the when statement can handle this case.
@jkasten2
Copy link
Member Author

Was thinking about switching the implementation to use getDeclaredMethod to simply and improve this code performance. However since we depend on case-insensitivity this can't be used on it's own. It's possible we could use getDeclaredMethod if we can accurately generate the correct case, however validating this with all fields would take sometime and the bigger one, would make things more fragile.

On the performance front, calling this.javaClass.methods is fairly expensive (from memory when we did some performance testing a while back), this could be cached. However doing this change later would be better, so we don't increase the risk of breaking something and to get this bug fix shipped faster.

@jkasten2 jkasten2 merged commit 77d2769 into main May 21, 2025
2 checks passed
@jkasten2 jkasten2 deleted the fix/crash-on-some-alias-labels branch May 21, 2025 22:07
@jinliu9508 jinliu9508 mentioned this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants