I don't believe it's safe to return a vector by reference created in a function. (#2286)

This commit is contained in:
Garrett Moon 2016-09-26 14:41:58 -07:00 committed by Adlai Holler
parent 29dd3ce6d6
commit d909a8af01

View File

@ -130,16 +130,16 @@ static inline BOOL ASLayoutCanTransitionAsynchronous(ASLayout *layout) {
compareBlock:^BOOL(ASLayout *lhs, ASLayout *rhs) { compareBlock:^BOOL(ASLayout *lhs, ASLayout *rhs) {
return ASObjectIsEqual(lhs.layoutable, rhs.layoutable); return ASObjectIsEqual(lhs.layoutable, rhs.layoutable);
}]; }];
findNodesInLayoutAtIndexes(pendingLayout, insertions, &_insertedSubnodes, &_insertedSubnodePositions); _insertedSubnodePositions = findNodesInLayoutAtIndexes(pendingLayout, insertions, &_insertedSubnodes);
findNodesInLayoutAtIndexesWithFilteredNodes(previousLayout, _removedSubnodePositions = findNodesInLayoutAtIndexesWithFilteredNodes(previousLayout,
deletions, deletions,
_insertedSubnodes, _insertedSubnodes,
&_removedSubnodes, &_removedSubnodes);
&_removedSubnodePositions);
} else { } else {
NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [pendingLayout.sublayouts count])]; NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [pendingLayout.sublayouts count])];
findNodesInLayoutAtIndexes(pendingLayout, indexes, &_insertedSubnodes, &_insertedSubnodePositions); _insertedSubnodePositions = findNodesInLayoutAtIndexes(pendingLayout, indexes, &_insertedSubnodes);
_removedSubnodes = nil; _removedSubnodes = nil;
_removedSubnodePositions.clear();
} }
_calculatedSubnodeOperations = YES; _calculatedSubnodeOperations = YES;
} }
@ -195,26 +195,25 @@ static inline BOOL ASLayoutCanTransitionAsynchronous(ASLayout *layout) {
/** /**
* @abstract Stores the nodes at the given indexes in the `storedNodes` array, storing indexes in a `storedPositions` c++ vector. * @abstract Stores the nodes at the given indexes in the `storedNodes` array, storing indexes in a `storedPositions` c++ vector.
*/ */
static inline void findNodesInLayoutAtIndexes(ASLayout *layout, static inline std::vector<NSUInteger> findNodesInLayoutAtIndexes(ASLayout *layout,
NSIndexSet *indexes, NSIndexSet *indexes,
NSArray<ASDisplayNode *> * __strong *storedNodes, NSArray<ASDisplayNode *> * __strong *storedNodes)
std::vector<NSUInteger> *storedPositions)
{ {
findNodesInLayoutAtIndexesWithFilteredNodes(layout, indexes, nil, storedNodes, storedPositions); return findNodesInLayoutAtIndexesWithFilteredNodes(layout, indexes, nil, storedNodes);
} }
/** /**
* @abstract Stores the nodes at the given indexes in the `storedNodes` array, storing indexes in a `storedPositions` c++ vector. * @abstract Stores the nodes at the given indexes in the `storedNodes` array, storing indexes in a `storedPositions` c++ vector.
* @discussion If the node exists in the `filteredNodes` array, the node is not added to `storedNodes`. * @discussion If the node exists in the `filteredNodes` array, the node is not added to `storedNodes`.
*/ */
static inline void findNodesInLayoutAtIndexesWithFilteredNodes(ASLayout *layout, static inline std::vector<NSUInteger> findNodesInLayoutAtIndexesWithFilteredNodes(ASLayout *layout,
NSIndexSet *indexes, NSIndexSet *indexes,
NSArray<ASDisplayNode *> *filteredNodes, NSArray<ASDisplayNode *> *filteredNodes,
NSArray<ASDisplayNode *> * __strong *storedNodes, NSArray<ASDisplayNode *> * __strong *storedNodes)
std::vector<NSUInteger> *storedPositions)
{ {
NSMutableArray<ASDisplayNode *> *nodes = [NSMutableArray arrayWithCapacity:indexes.count]; NSMutableArray<ASDisplayNode *> *nodes = [NSMutableArray arrayWithCapacity:indexes.count];
std::vector<NSUInteger> positions = std::vector<NSUInteger>(); std::vector<NSUInteger> positions = std::vector<NSUInteger>();
// From inspection, this is how enumerateObjectsAtIndexes: works under the hood // From inspection, this is how enumerateObjectsAtIndexes: works under the hood
NSUInteger firstIndex = indexes.firstIndex; NSUInteger firstIndex = indexes.firstIndex;
NSUInteger lastIndex = indexes.lastIndex; NSUInteger lastIndex = indexes.lastIndex;
@ -236,7 +235,8 @@ static inline void findNodesInLayoutAtIndexesWithFilteredNodes(ASLayout *layout,
idx += 1; idx += 1;
} }
*storedNodes = nodes; *storedNodes = nodes;
*storedPositions = positions;
return positions;
} }
@end @end