fix crash when removing an array from itself

This commit is contained in:
Richard Frith-Macdonald 2019-09-25 09:54:59 +01:00
parent 169a5036e3
commit ae95e859d6
2 changed files with 36 additions and 8 deletions

View file

@ -1,3 +1,9 @@
2019-09-25 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSArray.m: [-removeObjectsInArray:] add checks to prevent
possible crashes, particulary if the method argument is the receiver;
crash seen by Wolfgang.
2019-09-21 Richard Frith-Macdonald <rfm@gnu.org>
* Source/GSTLS.m: Re-order code to avoid setting session priority

View file

@ -2503,16 +2503,38 @@ compare(id elem1, id elem2, void* context)
*/
- (void) removeObjectsInArray: (NSArray*)otherArray
{
NSUInteger c = [otherArray count];
if (c > 0)
if (otherArray == self)
{
NSUInteger i;
IMP get = [otherArray methodForSelector: oaiSel];
IMP rem = [self methodForSelector: @selector(removeObject:)];
[self removeAllObjects];
}
else if (otherArray != nil)
{
NSUInteger c;
for (i = 0; i < c; i++)
(*rem)(self, @selector(removeObject:), (*get)(otherArray, oaiSel, i));
if (NO == [otherArray isKindOfClass: NSArrayClass])
{
[NSException raise: NSInvalidArgumentException
format: @"-removeObjectsInArray: non-array argument"];
}
if ((c = [otherArray count]) > 0)
{
NSUInteger i;
IMP get = [otherArray methodForSelector: oaiSel];
IMP rem = [self methodForSelector: @selector(removeObject:)];
/* Guard otherArray in case it's a subclass which does not
* retain its contents; in that case it would be possible
* for otherArray to be contained by the receiver and be
* deallocated during the loop below.
*/
RETAIN(otherArray);
for (i = 0; i < c; i++)
{
(*rem)(self, @selector(removeObject:),
(*get)(otherArray, oaiSel, i));
}
RELEASE(otherArray);
}
}
}