AutomationPeer.GetRuntimeId assumes Object.GetHashCode() is unique
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
### Description
AutomationPeer.GetRuntimeId returns an array containing the process id and the result of GetHashCode for the AutomationPeer object. Since it doesn't use UiaAppendRuntimeId (which technically is relying on undocumented behavior already), those values must uniquely identify the UIAutomation element. None of the subclasses override GetHashCode or GetRuntimeId, so Object.GetHashCode is used. The [MSDN documentation for Object.GetHashCode](https://learn.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=net-8.0#system-object-gethashcode) says this:
> Two objects that are equal return hash codes that are equal. However, the reverse is not true: equal hash codes do not imply object equality, because different (unequal) objects can have identical hash codes. Furthermore, .NET does not guarantee the default implementation of the [GetHashCode](https://learn.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=net-8.0) method, and the value this method returns may differ between .NET implementations, such as different versions of .NET Framework and .NET Core, and platforms, such as 32-bit and 64-bit platforms. For these reasons, do not use the default implementation of this method as a unique object identifier for hashing purposes.
### Reproduction Steps
Object hash code collisions are likely rare, so this would be difficult to reproduce.
### Expected behavior
GetRuntimeId should return a value that is guaranteed to uniquely identify the element.
### Actual behavior
GetRuntimeId may not uniquely identify the element because it depends on the result of Object.GetHashCode to uniquely identify the object.
### Regression?
_No response_
### Known Workarounds
_No response_
### Impact
May cause rare and difficult-to-reproduce errors in Assistive Technologies when they interact with WPF applications. UI Automation internally relies on GetRuntimeId to be unique across the desktop (Wine tests have examples of this), and Assistive Technologies may also make this assumption.
### Configuration
_No response_
### Other information
My suggestion would be to add a static field with a monotonic counter used to generate runtime id's in AutomationPeer's constructor.
Contributor guide
Assessment
This issue has not been assessed yet.