llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Lint] RetainCount: If neither the create rule nor the get rule applies, default to the get rule?

Open llvmbot opened this issue 18 years ago • 7 comments

Bugzilla Link 2646
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor
CC @tkremenek,@haoNoQ

Extended Description

I have some code that was not autoreleasing a string created with CFXMLCreateStringByEscapingEntities ... it leaked at runtime; it should have been caught by Clang, yes?

This is using Build 73.

Keep up the good work!

llvmbot avatar Aug 07 '08 02:08 llvmbot

This sort of bugs is indeed now often caught inter-procedurally.

I'll leave this bug report around in order to keep track of the idea of adding an stricter opt-in policy.

haoNoQ avatar Dec 23 '18 23:12 haoNoQ

Very interesting.

I wonder if there could be a 'switch' to allow for detection of all methods that are returning 'owned' object references. I know that I, and probably most Objective C programmers, have been drinking the "always return an autoreleased object" Kool-Aid (it's very tasty, BTW -- much better than Grape or Strawberry) and so to be warned of any method of my own which returns an owned reference is very likely a BIG FAT STINKING BUG. :-)

Could you make this be feature request to allow for these to be treated as bugs? And for those people who write code that allows for owning references, they could just (for now) ignore these warnings....

Gracias!

This is a matter of programming style, so I'm more prone to treat this as an opt-in than an opt-out. We can easily add another flag to scan-build to treat these cases as errors.

tkremenek avatar Aug 07 '08 03:08 tkremenek

Very interesting.

I wonder if there could be a 'switch' to allow for detection of all methods that are returning 'owned' object references. I know that I, and probably most Objective C programmers, have been drinking the "always return an autoreleased object" Kool-Aid (it's very tasty, BTW -- much better than Grape or Strawberry) and so to be warned of any method of my own which returns an owned reference is very likely a BIG FAT STINKING BUG. :-)

Could you make this be feature request to allow for these to be treated as bugs? And for those people who write code that allows for owning references, they could just (for now) ignore these warnings....

Gracias!

llvmbot avatar Aug 07 '08 02:08 llvmbot

Sure thing.

These leak -- detected by 'leaks' -- but weren't detected by Clang.

  • (NSString *)stringByEscapingXMLEntities:(NSDictionary *)entities { return (NSString *)CFXMLCreateStringByEscapingEntities(NULL,

                                 (CFStringRef)self,
    
                                 (CFMutableDictionaryRef)entities);
    

}

  • (NSString *)stringByUnescapingXMLEntities:(NSDictionary *)entities { return (NSString *)CFXMLCreateStringByUnescapingEntities(NULL,

                                       (CFStringRef)self,
    
                                       (CFMutableDictionaryRef)entities);
    

}

In this case the analyzer doesn't know if the two methods should be treated as returning an owning reference (i.e., the "Create" rule) or that they are leaking memory. Either is a valid interpretation of this code. We're opting for false negatives in this case because stylistically developers are allowed to not use autorelease here and to instead return an owning reference.

When we add inter-procedural analysis, we will be able to better tell whether or not there is a leak here by seeing how these methods are called. Currently we don't propagate this information up to the callers, but once we do it will be possible to resolve the ambiguity of whether or not there is a leak here.

tkremenek avatar Aug 07 '08 02:08 tkremenek

Sure thing.

These leak -- detected by 'leaks' -- but weren't detected by Clang.

  • (NSString *)stringByEscapingXMLEntities:(NSDictionary *)entities { return (NSString *)CFXMLCreateStringByEscapingEntities(NULL, (CFStringRef)self, (CFMutableDictionaryRef)entities); }

  • (NSString *)stringByUnescapingXMLEntities:(NSDictionary *)entities { return (NSString *)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)self, (CFMutableDictionaryRef)entities); }

This seems to fix things:

  • (NSString *)stringByEscapingXMLEntities:(NSDictionary *)entities { NSString *result = (NSString *)CFXMLCreateStringByEscapingEntities(NULL, (CFStringRef)self, (CFMutableDictionaryRef)entities); return [result autorelease]; }

  • (NSString *)stringByUnescapingXMLEntities:(NSDictionary *)entities { NSString *result = (NSString *)CFXMLCreateStringByUnescapingEntities(NULL, (CFStringRef)self, (CFMutableDictionaryRef)entities); return [result autorelease]; }

llvmbot avatar Aug 07 '08 02:08 llvmbot

I have some code that was not autoreleasing a string created with CFXMLCreateStringByEscapingEntities ... it leaked at runtime; it should have been caught by Clang, yes?

This is using Build 73.

Keep up the good work!

Dan: If possible, can you provide a reduced test case that exhibits the problem? A simple .m file with a single function that uses CFXMLCreateStringByEscapingEntities and has a leak would suffice. I'm trying to understand if the issue is the tool not tracking the value returned by CFXMLCreateStringByEscapingEntities or something else.

tkremenek avatar Aug 07 '08 02:08 tkremenek

assigned to @tkremenek

llvmbot avatar Aug 07 '08 02:08 llvmbot