xref: /minix3/external/bsd/llvm/dist/clang/test/Analysis/malloc.m (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc -analyzer-store=region -verify -Wno-objc-root-class -fblocks %s
2f4a2713aSLionel Sambuc#include "Inputs/system-header-simulator-objc.h"
3f4a2713aSLionel Sambuc
4f4a2713aSLionel Sambuc@class NSString;
5f4a2713aSLionel Sambuctypedef __typeof(sizeof(int)) size_t;
6f4a2713aSLionel Sambucvoid *malloc(size_t);
7f4a2713aSLionel Sambucvoid free(void *);
8f4a2713aSLionel Sambuc
9f4a2713aSLionel Sambuc// RDar10579586 - Test use of malloc() with Objective-C string literal as a
10f4a2713aSLionel Sambuc// test condition.  Not really a malloc() issue, but this also exercises
11f4a2713aSLionel Sambuc// the check that malloc() returns uninitialized memory.
12f4a2713aSLionel Sambuc@interface RDar10579586
13f4a2713aSLionel Sambucstruct rdar0579586_str {
14f4a2713aSLionel Sambuc    char str_c;
15f4a2713aSLionel Sambuc};
16f4a2713aSLionel Sambuc@end
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambucvoid rdar10579586(char x);
19f4a2713aSLionel Sambuc
20f4a2713aSLionel Sambuc@implementation RDar10579586
21f4a2713aSLionel Sambuc+ (NSString *)foobar
22f4a2713aSLionel Sambuc{
23f4a2713aSLionel Sambuc    struct rdar0579586_str *buffer = ((void*)0);
24f4a2713aSLionel Sambuc    NSString *error = ((void*)0);
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc    if ((buffer = malloc(sizeof(struct rdar0579586_str))) == ((void*)0))
27f4a2713aSLionel Sambuc        error = @"buffer allocation failure";
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc    if (error != ((void*)0))
30f4a2713aSLionel Sambuc        return error;
31f4a2713aSLionel Sambuc
32f4a2713aSLionel Sambuc    rdar10579586(buffer->str_c); // expected-warning {{Function call argument is an uninitialized value}}
33f4a2713aSLionel Sambuc    free(buffer);
34f4a2713aSLionel Sambuc    return ((void*)0);
35f4a2713aSLionel Sambuc}
36f4a2713aSLionel Sambuc@end
37f4a2713aSLionel Sambuc
38f4a2713aSLionel Sambuc@interface MyArray : NSObject {
39f4a2713aSLionel Sambuc  id * objects;
40f4a2713aSLionel Sambuc}
41f4a2713aSLionel Sambuc@end
42f4a2713aSLionel Sambuc
43f4a2713aSLionel Sambucvoid _ArrayCreate() {
44f4a2713aSLionel Sambuc  MyArray *array = (MyArray *)malloc(12);
45f4a2713aSLionel Sambuc  array = [array init];
46f4a2713aSLionel Sambuc  free(array); // no-warning
47f4a2713aSLionel Sambuc}
48f4a2713aSLionel Sambuc
49f4a2713aSLionel Sambucvoid testNSDataTruePositiveLeak() {
50f4a2713aSLionel Sambuc  char *b = (char *)malloc(12);
51f4a2713aSLionel Sambuc  NSData *d = [[NSData alloc] initWithBytes: b length: 12]; // expected-warning {{Potential leak of memory pointed to by 'b'}}
52f4a2713aSLionel Sambuc}
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambucid wrapInNSValue() {
55*0a6a1f1dSLionel Sambuc  void *buffer = malloc(4);
56*0a6a1f1dSLionel Sambuc  return [NSValue valueWithPointer:buffer]; // no-warning
57*0a6a1f1dSLionel Sambuc}