llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.98k stars 11.94k forks source link

catch and flag unnecessary checks for null when calling free() and delete #3272

Open seanm opened 16 years ago

seanm commented 16 years ago
Bugzilla Link 2900
Version unspecified
OS MacOS X

Extended Description

The C standard guarantees that you can safely pass NULL to free() and that nothing happens in response. Likewise for C++'s delete. Likewise for other memory allocation functions, see:

http://developer.apple.com/qa/qa2001/qa1259.html

Consider:

int main(int argc, char argv[]) { char foo = malloc(1024); if (foo) free (foo);

return 0;

}

Sometime people add such checks for null when there is really no need.

It would be nice for the static analyzer to catch such things, perhaps under a 'coding conventions' grouping.

The reverse would be nice too. For example, passing null to CFRelease() is documented to be a no-no. Catching such situations would also be helpful.

int main(int argc, char *argv[]) { CFStringRef str = CFStringCreateWithCString (kCFAllocatorDefault, "foo", kCFStringEncodingASCII); // may return NULL! str = NULL; CFRelease(str);

return 0;

}

seanm commented 16 years ago

assigned to @tkremenek