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