]> granicus.if.org Git - clang/commitdiff
[analyzer] Teach malloc checker that initWith[Bytes|Characters}NoCopy
authorAnna Zaks <ganna@apple.com>
Fri, 22 Jun 2012 22:42:30 +0000 (22:42 +0000)
committerAnna Zaks <ganna@apple.com>
Fri, 22 Jun 2012 22:42:30 +0000 (22:42 +0000)
relinquish memory.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159043 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/MallocChecker.cpp
test/Analysis/malloc.mm

index b0fcb2507954574b6437ff451dcacd325c85f5aa..50d4273e48c0a1dae4a33f3e297ee0090c130f22 100644 (file)
@@ -504,7 +504,9 @@ void MallocChecker::checkPreObjCMessage(const ObjCMessage &Msg,
   // Ex:  [NSData dataWithBytesNoCopy:bytes length:10];
   // Unless 'freeWhenDone' param set to 0.
   // TODO: Check that the memory was allocated with malloc.
-  if (S.getNameForSlot(0) == "dataWithBytesNoCopy" &&
+  if ((S.getNameForSlot(0) == "dataWithBytesNoCopy" ||
+       S.getNameForSlot(0) == "initWithBytesNoCopy" ||
+       S.getNameForSlot(0) == "initWithCharactersNoCopy") &&
       !isFreeWhenDoneSetToZero(Call, S)){
     unsigned int argIdx  = 0;
     C.addTransition(FreeMemAux(C, Call.getArg(argIdx),
index 64135b2366a279ea68f3a2a6edf317c4b87a60b3..7a9d881b151811c420ae424081769097056a6c72 100644 (file)
@@ -21,6 +21,16 @@ void testNSDataFreeWhenDoneYES2(NSUInteger dataLength) {
   NSData *nsdata = [[NSData alloc] initWithBytesNoCopy:data length:dataLength freeWhenDone:1]; // no-warning
 }
 
+void testNSStringFreeWhenDoneYES3(NSUInteger dataLength) {
+  unsigned char *data = (unsigned char *)malloc(42);
+  NSString *nsstr = [[NSString alloc] initWithBytesNoCopy:data length:dataLength encoding:NSUTF8StringEncoding freeWhenDone:1];
+}
+
+void testNSStringFreeWhenDoneYES4(NSUInteger dataLength) {
+  unichar *data = (unichar*)malloc(42);
+  NSString *nsstr = [[NSString alloc] initWithCharactersNoCopy:data length:dataLength freeWhenDone:1];
+  free(data); //expected-warning {{Attempt to free non-owned memory}}
+}
 
 void testNSStringFreeWhenDoneYES(NSUInteger dataLength) {
   unsigned char *data = (unsigned char *)malloc(42);