IGListKit/Source/IGListDiffKit
Cameron Roth 54605c079a
Some checks failed
IGListKit CI / Run Danger (push) Has been cancelled
IGListKit CI / Unit Test macOS (push) Has been cancelled
IGListKit CI / Unit Test tvOS (push) Has been cancelled
IGListKit CI / Build Examples and UI tests. (push) Has been cancelled
IGListKit CI / CocoaPods Lint (push) Has been cancelled
IGListKit CI / Verify generate_spm_sources_layout.sh is not broken (push) Has been cancelled
IGListKit CI / Verify SPM build by invoking `xcodebuild` on Package.swift (push) Has been cancelled
IGListKit CI / Unit Test iOS (push) Has been cancelled
IGListKit CI / Verify Carthage build XCFramework (push) Has been cancelled
Fix heap corruption crash from concurrent array mutation during diff (#1578)
Summary:
Fixes https://github.com/Instagram/IGListKit/issues/1578

Callers of `IGListDiff`/`IGListDiffPaths` may pass `NSMutableArray` instances backed by collections that are mutated on other threads. Because the diffing algorithm uses `__unsafe_unretained` pointers internally for performance, concurrent mutation can cause use-after-free heap corruption — typically manifesting as:

```
malloc: Incorrect checksum for freed object: probably modified after being freed.
```

inside `std::deque::push_back` during the entry `oldIndexes` stack growth.

This change adds `[oldArray copy]` and `[newArray copy]` at the top of `IGListDiffing()`. For immutable `NSArray` inputs this is a no-op retain with zero overhead. For `NSMutableArray` inputs it creates an immutable snapshot, narrowing the race window from the entire O(n+m) diff to just the `-copy` call.

This is a best-effort mitigation — callers are still responsible for not mutating the source array concurrently since `-[NSMutableArray copy]` itself is not atomic.

Differential Revision: D101205956

fbshipit-source-id: 514ebbef1903c796c10fee23f4efb4dd9b3073bd
2026-04-30 21:33:49 -07:00
..
Internal Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
IGListAssert.h add assert for iOS 18 cell dequeue exceptions 2024-09-17 07:11:55 -07:00
IGListBatchUpdateData.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
IGListBatchUpdateData.mm clean up ig_ios_listkit_fix_net_item_count 2024-12-12 04:02:03 -08:00
IGListCompatibility.h Fix public compilation by making METAUIKitBridge conditional 2026-04-16 12:51:41 -07:00
IGListDiff.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
IGListDiff.mm Fix heap corruption crash from concurrent array mutation during diff (#1578) 2026-04-30 21:33:49 -07:00
IGListDiffable.h Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
IGListDiffKit.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
IGListExperiments.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
IGListIndexPathResult.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
IGListIndexPathResult.m Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
IGListIndexSetResult.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
IGListIndexSetResult.m Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
IGListMacros.h Handle unexpected (out-of-range) cases in switch statements for CompilerWarningLevel.HIGH targets 2025-05-28 09:28:35 -07:00
IGListMoveIndex.h Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
IGListMoveIndex.m Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
IGListMoveIndexPath.h Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
IGListMoveIndexPath.m Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
NSNumber+IGListDiffable.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
NSNumber+IGListDiffable.m Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00
NSString+IGListDiffable.h Fix the SPM build errors caused by removal of unprefixed headers (#1644) 2025-11-17 00:08:32 -08:00
NSString+IGListDiffable.m Standarize the copyright notice in all source files 2023-04-06 02:44:16 -07:00