Ted Kremenek | 033a07e | 2011-08-03 23:14:55 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -analyze -analyzer-checker=experimental.osx.KeychainAPI %s -verify |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 2 | |
| 3 | // Fake typedefs. |
| 4 | typedef unsigned int OSStatus; |
| 5 | typedef unsigned int SecKeychainAttributeList; |
| 6 | typedef unsigned int SecKeychainItemRef; |
| 7 | typedef unsigned int SecItemClass; |
| 8 | typedef unsigned int UInt32; |
| 9 | typedef unsigned int CFTypeRef; |
| 10 | typedef unsigned int UInt16; |
| 11 | typedef unsigned int SecProtocolType; |
| 12 | typedef unsigned int SecAuthenticationType; |
| 13 | enum { |
| 14 | noErr = 0, |
| 15 | GenericError = 1 |
| 16 | }; |
| 17 | |
| 18 | // Functions that allocate data. |
| 19 | OSStatus SecKeychainItemCopyContent ( |
| 20 | SecKeychainItemRef itemRef, |
| 21 | SecItemClass *itemClass, |
| 22 | SecKeychainAttributeList *attrList, |
| 23 | UInt32 *length, |
| 24 | void **outData |
| 25 | ); |
| 26 | OSStatus SecKeychainFindGenericPassword ( |
| 27 | CFTypeRef keychainOrArray, |
| 28 | UInt32 serviceNameLength, |
| 29 | const char *serviceName, |
| 30 | UInt32 accountNameLength, |
| 31 | const char *accountName, |
| 32 | UInt32 *passwordLength, |
| 33 | void **passwordData, |
| 34 | SecKeychainItemRef *itemRef |
| 35 | ); |
| 36 | OSStatus SecKeychainFindInternetPassword ( |
| 37 | CFTypeRef keychainOrArray, |
| 38 | UInt32 serverNameLength, |
| 39 | const char *serverName, |
| 40 | UInt32 securityDomainLength, |
| 41 | const char *securityDomain, |
| 42 | UInt32 accountNameLength, |
| 43 | const char *accountName, |
| 44 | UInt32 pathLength, |
| 45 | const char *path, |
| 46 | UInt16 port, |
| 47 | SecProtocolType protocol, |
| 48 | SecAuthenticationType authenticationType, |
| 49 | UInt32 *passwordLength, |
| 50 | void **passwordData, |
| 51 | SecKeychainItemRef *itemRef |
| 52 | ); |
| 53 | |
| 54 | // Function which frees data. |
| 55 | OSStatus SecKeychainItemFreeContent ( |
| 56 | SecKeychainAttributeList *attrList, |
| 57 | void *data |
| 58 | ); |
| 59 | |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 60 | void errRetVal() { |
| 61 | unsigned int *ptr = 0; |
| 62 | OSStatus st = 0; |
| 63 | UInt32 length; |
| 64 | void *outData; |
| 65 | st = SecKeychainItemCopyContent(2, ptr, ptr, &length, &outData); |
| 66 | if (st == GenericError) // expected-warning{{Missing a call to SecKeychainItemFreeContent}} |
| 67 | SecKeychainItemFreeContent(ptr, outData); |
| 68 | } |
| 69 | |
| 70 | // If null is passed in, the data is not allocated, so no need for the matching free. |
| 71 | void fooDoNotReportNull() { |
| 72 | unsigned int *ptr = 0; |
| 73 | OSStatus st = 0; |
| 74 | UInt32 *length = 0; |
| 75 | void **outData = 0; |
| 76 | SecKeychainItemCopyContent(2, ptr, ptr, 0, 0); |
| 77 | SecKeychainItemCopyContent(2, ptr, ptr, length, outData); |
| 78 | }// no-warning |
| 79 | |
| 80 | void fooOnlyFree() { |
| 81 | unsigned int *ptr = 0; |
| 82 | OSStatus st = 0; |
| 83 | UInt32 length; |
| 84 | void *outData = &length; |
| 85 | SecKeychainItemFreeContent(ptr, outData);// expected-warning{{Trying to free data which has not been allocated}} |
| 86 | } |
| 87 | |
| 88 | // Do not warn if undefined value is passed to a function. |
| 89 | void fooOnlyFreeUndef() { |
| 90 | unsigned int *ptr = 0; |
| 91 | OSStatus st = 0; |
| 92 | UInt32 length; |
| 93 | void *outData; |
| 94 | SecKeychainItemFreeContent(ptr, outData); |
| 95 | }// no-warning |
| 96 | |
| 97 | // Do not warn if the address is a parameter in the enclosing function. |
| 98 | void fooOnlyFreeParam(void* X) { |
| 99 | SecKeychainItemFreeContent(X, X); |
| 100 | }// no-warning |
| 101 | |
| 102 | // If we are returning the value, no not report. |
| 103 | void* returnContent() { |
| 104 | unsigned int *ptr = 0; |
| 105 | OSStatus st = 0; |
| 106 | UInt32 length; |
| 107 | void *outData; |
| 108 | st = SecKeychainItemCopyContent(2, ptr, ptr, &length, &outData); |
| 109 | return outData; |
| 110 | } // no-warning |
| 111 | |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 112 | int foo () { |
| 113 | unsigned int *ptr = 0; |
| 114 | OSStatus st = 0; |
| 115 | |
| 116 | UInt32 length; |
| 117 | void *outData; |
| 118 | |
| 119 | st = SecKeychainItemCopyContent(2, ptr, ptr, &length, &outData); |
Anna Zaks | e68b5f1 | 2011-08-02 17:11:03 +0000 | [diff] [blame] | 120 | if (st == noErr) |
| 121 | SecKeychainItemFreeContent(ptr, outData); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 122 | |
| 123 | return 0; |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 124 | }// no-warning |