Fix a crash in Unreal Engine games when system language is Chinese #182
+31
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Recently some Unreal Engine games started using the Extended Virtual Addressing feature. When this feature is enabled, Unreal Engine attempts to extract the Entitlement from the executable and checks whether it contains
com.apple.developer.kernel.extended-virtual-addressing
.However, due to an issue in Unreal Engine’s code, it read 8 extra bytes. These extra bytes cause a string decoding failure when the system language is set to Chinese, ultimately leading to a crash.
Explanations
FApplePlatformMemory.cpp
This function calls
FIOSPlatformMisc::IsEntitlementEnabled
.FIOSPlatformMisc.cpp
EntitlementsData()
returns an empty string, but therange
is nonzero, causing stringByReplacingOccurrencesOfString to crash with a Range out of bounds error.IOSPlatformMisc.cpp
It reads 8 extra bytes, which causes a decoding failure and returns an empty string.
Why has it read 8 extra bytes?
The
blob.length
represents the total length ofCS_GenericBlob
, not thedata
array.The actual length of the data array should be blob.length - 8 bytes.
Why does it return an empty string?
Expected data:
<plist>...</plist>
Actual data read:
<plist>...</plist>\xfa\xde\x71\x72
When we pass the data to stringWithFormat, it uses the encoding returned by
CFStringGetSystemEncoding()
to decode it. When the system language is set to Chinese, it will useCFStringEncodingMacChineseSimp
, but the decoding fails, returning an empty string.Why doesn’t the crash happen on iOS?
On iOS,
CFStringGetSystemEncoding()
always returns0
regardless of the system language, so it works fine. On macOS,CFStringGetSystemEncoding()
returns different values depending on the system language.How to Fix
Hook the string replacement function. If the source string is empty, return immediately to prevent a Range out of bounds error.