This is an archive of the discontinued LLVM Phabricator instance.

[ObjC] New warning: circular containers
ClosedPublic

Authored by AlexDenisov on Mar 2 2015, 1:28 PM.

Details

Summary

The patch adds new warning to prevent user from creating 'circular containers'.
Mutable collections from NSFoundation allows user to add collection to itself, e.g.:

NSMutableArray *a = [NSMutableArray new];
[a addObject:a];

The code above leads to really weird behaviour (crashes, 'endless' recursion) and retain cycles (collection retains itself) and it's really hard to debug and fix.
This which is really hard to debug in a real application.

Patch checks the following collections: NSMutableArray, NSMutableDictionary, NSMutableSet, NSMutableOrderedSet, NSCountedSet.

P.S. any suggestions for better wordings and methods/warning names - are welcome.

Diff Detail

Event Timeline

AlexDenisov updated this revision to Diff 21034.Mar 2 2015, 1:28 PM
AlexDenisov retitled this revision from to [ObjC] New warning: circular containers.
AlexDenisov updated this object.
AlexDenisov edited the test plan for this revision. (Show Details)
AlexDenisov added a subscriber: Unknown Object (MLST).

 Provide a note where receiver/argument has been declared.

Do you mean ‘add comments’?

 Place CheckObjCCircularContainer(Result) right after checkRetainCycles(Result).

It still causes weird behaviour even without ARC. Of course there is no retain cycle anymore, but app still hangs with recursion/crash.

This patch does not address the general case of same expression used as receiver and addObject argument.
Is this something that you care enough to address?

Do you mean something like ’[self.array addObject:self.array]’?
If so, then it doesn’t really makes sense, because we can’t ensure that returned objects are the same, there’ll be false positives.
-- 
AlexDenisov
Software Engineer, http://alexdenisov.github.io

AlexDenisov removed a subscriber: Unknown Object (MLST).Mar 4 2015, 6:35 AM
AlexDenisov abandoned this revision.Apr 11 2016, 7:16 AM
AlexDenisov reclaimed this revision.Apr 16 2016, 1:19 AM
AlexDenisov accepted this revision.
AlexDenisov added a reviewer: AlexDenisov.
This revision is now accepted and ready to land.Apr 16 2016, 1:19 AM
AlexDenisov closed this revision.Apr 16 2016, 1:19 AM