SIGN IN SIGN UP
facebook / react-native UNCLAIMED

A framework for building native applications using React

0 0 0 C++

Fix undefined behavior in LayoutAnimation sort comparator (#56127)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/56127

`shouldFirstComeBeforeSecondMutation` in `utils.h` uses `return &lhs < &rhs` as a fallback when mutation types don't determine ordering. That's undefined behavior per C++ standard [expr.rel]/4, comparing pointers from different allocations (which happens during `std::stable_sort`'s merge phase).

This wasn't a problem until the LLVM 19.x NDK upgrade changed libc++'s sort implementation. The new insertion sort phase is more aggressive about exploiting comparator correctness, and the UB now manifests as SIGSEGV in the insertion sort phase on Android.

The fix replaces the pointer comparison with a deterministic fallback using `parentTag`, `newChildShadowView.tag`, and `oldChildShadowView.tag`. When all properties are equal, returns `false` to maintain stable ordering.

Also adds `MutationComparatorTest.cpp` with tests covering:
- Strict weak ordering properties (irreflexivity, asymmetry, transitivity)
- Type-based ordering rules (deletes last, removes before inserts, etc.)
- Deterministic fallback by parentTag, newChildTag, oldChildTag
- Equal mutations return false (stability)
- Stress test: `std::stable_sort` on large mutation lists with duplicates

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D95909371

fbshipit-source-id: 059947e172efe7ccf28456234b4aa8a98d357657
P
Peter Abbondanzo committed
1fdd16710900f00648332cce8b3cd63bc6208329
Parent: 7ee5275
Committed by meta-codesync[bot] <215208954+meta-codesync[bot]@users.noreply.github.com> on 3/19/2026, 3:32:13 PM