blob: 9201c2b75fe6a94023f3246a4c907e972f104b87 [file] [log] [blame]
Anna Zaksaca0ac52012-05-03 23:50:28 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store=region -verify -Wno-objc-root-class -fblocks %s
Chandler Carruth1b22cec2012-09-12 01:11:10 +00002#include "Inputs/system-header-simulator-objc.h"
Ted Kremenek4c62b552012-02-28 00:56:05 +00003
4@class NSString;
5typedef __typeof(sizeof(int)) size_t;
6void *malloc(size_t);
7void free(void *);
8
9// RDar10579586 - Test use of malloc() with Objective-C string literal as a
10// test condition. Not really a malloc() issue, but this also exercises
11// the check that malloc() returns uninitialized memory.
12@interface RDar10579586
13struct rdar0579586_str {
14 char str_c;
15};
16@end
17
18void rdar10579586(char x);
19
20@implementation RDar10579586
21+ (NSString *)foobar
22{
23 struct rdar0579586_str *buffer = ((void*)0);
24 NSString *error = ((void*)0);
25
26 if ((buffer = malloc(sizeof(struct rdar0579586_str))) == ((void*)0))
27 error = @"buffer allocation failure";
28
29 if (error != ((void*)0))
30 return error;
31
32 rdar10579586(buffer->str_c); // expected-warning {{Function call argument is an uninitialized value}}
33 free(buffer);
34 return ((void*)0);
35}
36@end
37
Anna Zakse7a5c822013-05-31 23:47:32 +000038@interface MyArray : NSObject {
Anna Zaksee1af232013-05-31 22:39:13 +000039 id * objects;
40}
41@end
42
Anna Zakse7a5c822013-05-31 23:47:32 +000043void _ArrayCreate() {
44 MyArray *array = (MyArray *)malloc(12);
Anna Zaksee1af232013-05-31 22:39:13 +000045 array = [array init];
46 free(array); // no-warning
Anna Zakse7a5c822013-05-31 23:47:32 +000047}
48
49void testNSDataTruePositiveLeak() {
50 char *b = (char *)malloc(12);
51 NSData *d = [[NSData alloc] initWithBytes: b length: 12]; // expected-warning {{Potential leak of memory pointed to by 'b'}}
Stephen Hines651f13c2014-04-23 16:59:28 -070052}
53
54id wrapInNSValue() {
55 void *buffer = malloc(4);
56 return [NSValue valueWithPointer:buffer]; // no-warning
Anna Zaksee1af232013-05-31 22:39:13 +000057}