1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.coreFoundation.CFNumber,osx.cocoa.RetainCount -analyzer-store=region -verify -triple x86_64-apple-darwin9 %s
2 
3 typedef signed long CFIndex;
4 typedef const struct __CFAllocator * CFAllocatorRef;
5 enum { kCFNumberSInt8Type = 1, kCFNumberSInt16Type = 2,
6        kCFNumberSInt32Type = 3, kCFNumberSInt64Type = 4,
7        kCFNumberFloat32Type = 5, kCFNumberFloat64Type = 6,
8        kCFNumberCharType = 7, kCFNumberShortType = 8,
9        kCFNumberIntType = 9, kCFNumberLongType = 10,
10        kCFNumberLongLongType = 11, kCFNumberFloatType = 12,
11        kCFNumberDoubleType = 13, kCFNumberCFIndexType = 14,
12        kCFNumberNSIntegerType = 15, kCFNumberCGFloatType = 16,
13        kCFNumberMaxType = 16 };
14 typedef CFIndex CFNumberType;
15 typedef const struct __CFNumber * CFNumberRef;
16 typedef unsigned char Boolean;
17 extern CFNumberRef CFNumberCreate(CFAllocatorRef allocator, CFNumberType theType, const void *valuePtr);
18 Boolean CFNumberGetValue(CFNumberRef number, CFNumberType theType, void *valuePtr);
19 
f1(unsigned char x)20 __attribute__((cf_returns_retained)) CFNumberRef f1(unsigned char x) {
21   return CFNumberCreate(0, kCFNumberSInt16Type, &x);  // expected-warning{{An 8-bit integer is used to initialize a CFNumber object that represents a 16-bit integer; 8 bits of the CFNumber value will be garbage}}
22 }
23 
f2(unsigned short x)24 __attribute__((cf_returns_retained)) CFNumberRef f2(unsigned short x) {
25   return CFNumberCreate(0, kCFNumberSInt8Type, &x); // expected-warning{{A 16-bit integer is used to initialize a CFNumber object that represents an 8-bit integer; 8 bits of the integer value will be lost}}
26 }
27 
28 // test that the attribute overrides the naming convention.
CreateNum(unsigned char x)29 __attribute__((cf_returns_not_retained)) CFNumberRef CreateNum(unsigned char x) {
30   return CFNumberCreate(0, kCFNumberSInt8Type, &x); // expected-warning{{leak}}
31 }
32 
f3(unsigned i)33 __attribute__((cf_returns_retained)) CFNumberRef f3(unsigned i) {
34   return CFNumberCreate(0, kCFNumberLongType, &i); // expected-warning{{A 32-bit integer is used to initialize a CFNumber object that represents a 64-bit integer}}
35 }
36 
getValueTest1(CFNumberRef x)37 unsigned char getValueTest1(CFNumberRef x) {
38   unsigned char scalar = 0;
39   CFNumberGetValue(x, kCFNumberSInt16Type, &scalar);  // expected-warning{{A CFNumber object that represents a 16-bit integer is used to initialize an 8-bit integer; 8 bits of the CFNumber value will overwrite adjacent storage}}
40   return scalar;
41 }
42 
getValueTest2(CFNumberRef x)43 unsigned char getValueTest2(CFNumberRef x) {
44   unsigned short scalar = 0;
45   CFNumberGetValue(x, kCFNumberSInt8Type, &scalar);  // expected-warning{{A CFNumber object that represents an 8-bit integer is used to initialize a 16-bit integer; 8 bits of the integer value will be garbage}}
46   return scalar;
47 }
48