brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.5 KiB · 230e536 Raw
57 lines · plain
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc -verify -Wno-objc-root-class -fblocks %s2#include "Inputs/system-header-simulator-objc.h"3 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 a10// test condition.  Not really a malloc() issue, but this also exercises11// the check that malloc() returns uninitialized memory.12@interface RDar1057958613struct rdar0579586_str {14    char str_c;15};16@end17 18void rdar10579586(char x);19 20@implementation RDar1057958621+ (NSString *)foobar22{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 {{1st function call argument is an uninitialized value}}33    free(buffer);34    return ((void*)0);35}36@end37 38@interface MyArray : NSObject {39  id * objects;40}41@end42 43void _ArrayCreate(void) {44  MyArray *array = (MyArray *)malloc(12);45  array = [array init];46  free(array); // no-warning47}48 49void testNSDataTruePositiveLeak(void) {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'}}52}53 54id wrapInNSValue(void) {55  void *buffer = malloc(4);56  return [NSValue valueWithPointer:buffer]; // no-warning57}