• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Analyzer in error?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Analyzer in error?


  • Subject: Re: Analyzer in error?
  • From: Greg Parker <email@hidden>
  • Date: Wed, 13 Jan 2010 18:09:56 -0800

On Jan 13, 2010, at 5:54 PM, Graham Cox wrote:
> @implementation NSData (SHA1Hash)
>
> - (NSData*)	sha1Hash
> {
> 	// calculates the 160 bit SHA-1 digest of the given data
>
> 	unsigned char* digest = (unsigned char*) malloc(20);
> 	SHA1([self bytes], [self length], digest);
>
> 	return [[self class] dataWithBytesNoCopy:digest length:20];
> }
>
> @end
>
> The static analyzer reports this:
>
> 	Potential leak of an object allocated on line 622
>
> 		Method returns an Objective-C object with a +1 retain count (owning reference)
> 		Object returned to caller as an owning reference (single retain count transferred to caller)
> 		Object allocated on line 622 is returned from a method whose name ('sha1Hash') does not contain 'copy' or otherwise starts with 'new' or 'alloc'.  This violates 		the naming convention rules given in the Memory Management Guide for Cocoa (object leaked)
>
> But I think this is wrong - [NSData dataWithBytesNoCopy] returns an object I don't own, and it in turn takes ownership of <digest>. So where's the leak? Is clang simply mistaking the fact that +dataWithBytesNoCopy CONTAINS 'copy' and not STARTS WITH copy?

Yes, the static analyzer is wrong here.

The Official Rule is '... contains "copy" ...'. This is a case where a mechanical interpretation of the Official Rule isn't quite right.

The static analyzer does have a special case for +[NSData dataWithBytesNoCopy:length:].
http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20090511/017258.html

But your code sends the message to [self class] instead of NSData, so perhaps the special case is not recognized. Alternatively, perhaps your Xcode isn't the newest available.

Try this to suppress the complaint:
    return [(NSData *)[self class] dataWithBytesNoCopy:digest length:20];


--
Greg Parker     email@hidden     Runtime Wrangler


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Analyzer in error?
      • From: Graham Cox <email@hidden>
References: 
 >Analyzer in error? (From: Graham Cox <email@hidden>)

  • Prev by Date: Re: Analyzer in error?
  • Next by Date: How to enable/disable a UITextField or UITextView programmatically?
  • Previous by thread: Re: Analyzer in error?
  • Next by thread: Re: Analyzer in error?
  • Index(es):
    • Date
    • Thread