1*21e58ee9SDonát Nagy================== 2*21e58ee9SDonát NagySource Annotations 3*21e58ee9SDonát Nagy================== 4*21e58ee9SDonát Nagy 5*21e58ee9SDonát NagyThe Clang frontend supports several source-level annotations in the form of 6*21e58ee9SDonát Nagy`GCC-style attributes <https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html>`_ 7*21e58ee9SDonát Nagyand pragmas that can help make using the Clang Static Analyzer more useful. 8*21e58ee9SDonát NagyThese annotations can both help suppress false positives as well as enhance the 9*21e58ee9SDonát Nagyanalyzer's ability to find bugs. 10*21e58ee9SDonát Nagy 11*21e58ee9SDonát NagyThis page gives a practical overview of such annotations. For more technical 12*21e58ee9SDonát Nagyspecifics regarding Clang-specific annotations please see the Clang's list of 13*21e58ee9SDonát Nagy`language extensions <https://clang.llvm.org/docs/LanguageExtensions.html>`_. 14*21e58ee9SDonát NagyDetails of "standard" GCC attributes (that Clang also supports) can 15*21e58ee9SDonát Nagybe found in the `GCC manual <https://gcc.gnu.org/onlinedocs/gcc/>`_, with the 16*21e58ee9SDonát Nagymajority of the relevant attributes being in the section on 17*21e58ee9SDonát Nagy`function attributes <https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html>`_. 18*21e58ee9SDonát Nagy 19*21e58ee9SDonát NagyNote that attributes that are labeled **Clang-specific** are not 20*21e58ee9SDonát Nagyrecognized by GCC. Their use can be conditioned using preprocessor macros 21*21e58ee9SDonát Nagy(examples included on this page). 22*21e58ee9SDonát Nagy 23*21e58ee9SDonát Nagy.. contents:: 24*21e58ee9SDonát Nagy :local: 25*21e58ee9SDonát Nagy 26*21e58ee9SDonát NagyAnnotations to Enhance Generic Checks 27*21e58ee9SDonát Nagy_____________________________________ 28*21e58ee9SDonát Nagy 29*21e58ee9SDonát NagyNull Pointer Checking 30*21e58ee9SDonát Nagy##################### 31*21e58ee9SDonát Nagy 32*21e58ee9SDonát NagyAttribute 'nonnull' 33*21e58ee9SDonát Nagy------------------- 34*21e58ee9SDonát Nagy 35*21e58ee9SDonát NagyThe analyzer recognizes the GCC attribute 'nonnull', which indicates that a 36*21e58ee9SDonát Nagyfunction expects that a given function parameter is not a null pointer. 37*21e58ee9SDonát NagySpecific details of the syntax of using the 'nonnull' attribute can be found in 38*21e58ee9SDonát Nagy`GCC's documentation <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-nonnull-function-attribute>`_. 39*21e58ee9SDonát Nagy 40*21e58ee9SDonát NagyBoth the Clang compiler and GCC will flag warnings for simple cases where a 41*21e58ee9SDonát Nagynull pointer is directly being passed to a function with a 'nonnull' parameter 42*21e58ee9SDonát Nagy(e.g., as a constant). The analyzer extends this checking by using its deeper 43*21e58ee9SDonát Nagysymbolic analysis to track what pointer values are potentially null and then 44*21e58ee9SDonát Nagyflag warnings when they are passed in a function call via a 'nonnull' 45*21e58ee9SDonát Nagyparameter. 46*21e58ee9SDonát Nagy 47*21e58ee9SDonát Nagy**Example** 48*21e58ee9SDonát Nagy 49*21e58ee9SDonát Nagy.. code-block:: c 50*21e58ee9SDonát Nagy 51*21e58ee9SDonát Nagy int bar(int*p, int q, int *r) __attribute__((nonnull(1,3))); 52*21e58ee9SDonát Nagy 53*21e58ee9SDonát Nagy int foo(int *p, int *q) { 54*21e58ee9SDonát Nagy return !p ? bar(q, 2, p) 55*21e58ee9SDonát Nagy : bar(p, 2, q); 56*21e58ee9SDonát Nagy } 57*21e58ee9SDonát Nagy 58*21e58ee9SDonát NagyRunning ``scan-build`` over this source produces the following output: 59*21e58ee9SDonát Nagy 60*21e58ee9SDonát Nagy.. image:: ../images/example_attribute_nonnull.png 61*21e58ee9SDonát Nagy 62*21e58ee9SDonát Nagy.. _custom_assertion_handlers: 63*21e58ee9SDonát Nagy 64*21e58ee9SDonát NagyCustom Assertion Handlers 65*21e58ee9SDonát Nagy######################### 66*21e58ee9SDonát Nagy 67*21e58ee9SDonát NagyThe analyzer exploits code assertions by pruning off paths where the 68*21e58ee9SDonát Nagyassertion condition is false. The idea is capture any program invariants 69*21e58ee9SDonát Nagyspecified in the assertion that the developer may know but is not immediately 70*21e58ee9SDonát Nagyapparent in the code itself. In this way assertions make implicit assumptions 71*21e58ee9SDonát Nagyexplicit in the code, which not only makes the analyzer more accurate when 72*21e58ee9SDonát Nagyfinding bugs, but can help others better able to understand your code as well. 73*21e58ee9SDonát NagyIt can also help remove certain kinds of analyzer false positives by pruning off 74*21e58ee9SDonát Nagyfalse paths. 75*21e58ee9SDonát Nagy 76*21e58ee9SDonát NagyIn order to exploit assertions, however, the analyzer must understand when it 77*21e58ee9SDonát Nagyencounters an "assertion handler". Typically assertions are 78*21e58ee9SDonát Nagyimplemented with a macro, with the macro performing a check for the assertion 79*21e58ee9SDonát Nagycondition and, when the check fails, calling an assertion handler. For 80*21e58ee9SDonát Nagyexample, consider the following code fragment: 81*21e58ee9SDonát Nagy 82*21e58ee9SDonát Nagy.. code-block: c 83*21e58ee9SDonát Nagy 84*21e58ee9SDonát Nagy void foo(int *p) { 85*21e58ee9SDonát Nagy assert(p != NULL); 86*21e58ee9SDonát Nagy } 87*21e58ee9SDonát Nagy 88*21e58ee9SDonát NagyWhen this code is preprocessed on Mac OS X it expands to the following: 89*21e58ee9SDonát Nagy 90*21e58ee9SDonát Nagy.. code-block: c 91*21e58ee9SDonát Nagy 92*21e58ee9SDonát Nagy void foo(int *p) { 93*21e58ee9SDonát Nagy (__builtin_expect(!(p != NULL), 0) ? __assert_rtn(__func__, "t.c", 4, "p != NULL") : (void)0); 94*21e58ee9SDonát Nagy } 95*21e58ee9SDonát Nagy 96*21e58ee9SDonát NagyIn this example, the assertion handler is ``__assert_rtn``. When called, 97*21e58ee9SDonát Nagymost assertion handlers typically print an error and terminate the program. The 98*21e58ee9SDonát Nagyanalyzer can exploit such semantics by ending the analysis of a path once it 99*21e58ee9SDonát Nagyhits a call to an assertion handler. 100*21e58ee9SDonát Nagy 101*21e58ee9SDonát NagyThe trick, however, is that the analyzer needs to know that a called function 102*21e58ee9SDonát Nagyis an assertion handler; otherwise the analyzer might assume the function call 103*21e58ee9SDonát Nagyreturns and it will continue analyzing the path where the assertion condition 104*21e58ee9SDonát Nagyfailed. This can lead to false positives, as the assertion condition usually 105*21e58ee9SDonát Nagyimplies a safety condition (e.g., a pointer is not null) prior to performing 106*21e58ee9SDonát Nagysome action that depends on that condition (e.g., dereferencing a pointer). 107*21e58ee9SDonát Nagy 108*21e58ee9SDonát NagyThe analyzer knows about several well-known assertion handlers, but can 109*21e58ee9SDonát Nagyautomatically infer if a function should be treated as an assertion handler if 110*21e58ee9SDonát Nagyit is annotated with the 'noreturn' attribute or the (Clang-specific) 111*21e58ee9SDonát Nagy'analyzer_noreturn' attribute. Note that, currently, clang does not support 112*21e58ee9SDonát Nagythese attributes on Objective-C methods and C++ methods. 113*21e58ee9SDonát Nagy 114*21e58ee9SDonát NagyAttribute 'noreturn' 115*21e58ee9SDonát Nagy-------------------- 116*21e58ee9SDonát Nagy 117*21e58ee9SDonát NagyThe 'noreturn' attribute is a GCC attribute that can be placed on the 118*21e58ee9SDonát Nagydeclarations of functions. It means exactly what its name implies: a function 119*21e58ee9SDonát Nagywith a 'noreturn' attribute should never return. 120*21e58ee9SDonát Nagy 121*21e58ee9SDonát NagySpecific details of the syntax of using the 'noreturn' attribute can be found 122*21e58ee9SDonát Nagyin `GCC's documentation <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-noreturn-function-attribute>`__. 123*21e58ee9SDonát Nagy 124*21e58ee9SDonát NagyNot only does the analyzer exploit this information when pruning false paths, 125*21e58ee9SDonát Nagybut the compiler also takes it seriously and will generate different code (and 126*21e58ee9SDonát Nagypossibly better optimized) under the assumption that the function does not 127*21e58ee9SDonát Nagyreturn. 128*21e58ee9SDonát Nagy 129*21e58ee9SDonát Nagy**Example** 130*21e58ee9SDonát Nagy 131*21e58ee9SDonát NagyOn Mac OS X, the function prototype for ``__assert_rtn`` (declared in 132*21e58ee9SDonát Nagy``assert.h``) is specifically annotated with the 'noreturn' attribute: 133*21e58ee9SDonát Nagy 134*21e58ee9SDonát Nagy.. code-block: c 135*21e58ee9SDonát Nagy 136*21e58ee9SDonát Nagy void __assert_rtn(const char *, const char *, int, const char *) __attribute__((__noreturn__)); 137*21e58ee9SDonát Nagy 138*21e58ee9SDonát NagyAttribute 'analyzer_noreturn' (Clang-specific) 139*21e58ee9SDonát Nagy---------------------------------------------- 140*21e58ee9SDonát Nagy 141*21e58ee9SDonát NagyThe Clang-specific 'analyzer_noreturn' attribute is almost identical to 142*21e58ee9SDonát Nagy'noreturn' except that it is ignored by the compiler for the purposes of code 143*21e58ee9SDonát Nagygeneration. 144*21e58ee9SDonát Nagy 145*21e58ee9SDonát NagyThis attribute is useful for annotating assertion handlers that actually 146*21e58ee9SDonát Nagy*can* return, but for the purpose of using the analyzer we want to 147*21e58ee9SDonát Nagypretend that such functions do not return. 148*21e58ee9SDonát Nagy 149*21e58ee9SDonát NagyBecause this attribute is Clang-specific, its use should be conditioned with 150*21e58ee9SDonát Nagythe use of preprocessor macros. 151*21e58ee9SDonát Nagy 152*21e58ee9SDonát Nagy**Example** 153*21e58ee9SDonát Nagy 154*21e58ee9SDonát Nagy.. code-block: c 155*21e58ee9SDonát Nagy 156*21e58ee9SDonát Nagy #ifndef CLANG_ANALYZER_NORETURN 157*21e58ee9SDonát Nagy #if __has_feature(attribute_analyzer_noreturn) 158*21e58ee9SDonát Nagy #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn)) 159*21e58ee9SDonát Nagy #else 160*21e58ee9SDonát Nagy #define CLANG_ANALYZER_NORETURN 161*21e58ee9SDonát Nagy #endif 162*21e58ee9SDonát Nagy #endif 163*21e58ee9SDonát Nagy 164*21e58ee9SDonát Nagy void my_assert_rtn(const char *, const char *, int, const char *) CLANG_ANALYZER_NORETURN; 165*21e58ee9SDonát Nagy 166*21e58ee9SDonát NagyMac OS X API Annotations 167*21e58ee9SDonát Nagy________________________ 168*21e58ee9SDonát Nagy 169*21e58ee9SDonát Nagy.. _cocoa_mem: 170*21e58ee9SDonát Nagy 171*21e58ee9SDonát NagyCocoa & Core Foundation Memory Management Annotations 172*21e58ee9SDonát Nagy##################################################### 173*21e58ee9SDonát Nagy 174*21e58ee9SDonát NagyThe analyzer supports the proper management of retain counts for 175*21e58ee9SDonát Nagyboth Cocoa and Core Foundation objects. This checking is largely based on 176*21e58ee9SDonát Nagyenforcing Cocoa and Core Foundation naming conventions for Objective-C methods 177*21e58ee9SDonát Nagy(Cocoa) and C functions (Core Foundation). Not strictly following these 178*21e58ee9SDonát Nagyconventions can cause the analyzer to miss bugs or flag false positives. 179*21e58ee9SDonát Nagy 180*21e58ee9SDonát NagyOne can educate the analyzer (and others who read your code) about methods or 181*21e58ee9SDonát Nagyfunctions that deviate from the Cocoa and Core Foundation conventions using the 182*21e58ee9SDonát Nagyattributes described here. However, you should consider using proper naming 183*21e58ee9SDonát Nagyconventions or the `objc_method_family <https://clang.llvm.org/docs/LanguageExtensions.html#the-objc-method-family-attribute>`_ 184*21e58ee9SDonát Nagyattribute, if applicable. 185*21e58ee9SDonát Nagy 186*21e58ee9SDonát Nagy.. _ns_returns_retained: 187*21e58ee9SDonát Nagy 188*21e58ee9SDonát NagyAttribute 'ns_returns_retained' (Clang-specific) 189*21e58ee9SDonát Nagy------------------------------------------------ 190*21e58ee9SDonát Nagy 191*21e58ee9SDonát NagyThe GCC-style (Clang-specific) attribute 'ns_returns_retained' allows one to 192*21e58ee9SDonát Nagyannotate an Objective-C method or C function as returning a retained Cocoa 193*21e58ee9SDonát Nagyobject that the caller is responsible for releasing (via sending a 194*21e58ee9SDonát Nagy``release`` message to the object). The Foundation framework defines a 195*21e58ee9SDonát Nagymacro ``NS_RETURNS_RETAINED`` that is functionally equivalent to the 196*21e58ee9SDonát Nagyone shown below. 197*21e58ee9SDonát Nagy 198*21e58ee9SDonát Nagy**Placing on Objective-C methods**: For Objective-C methods, this 199*21e58ee9SDonát Nagyannotation essentially tells the analyzer to treat the method as if its name 200*21e58ee9SDonát Nagybegins with "alloc" or "new" or contains the word 201*21e58ee9SDonát Nagy"copy". 202*21e58ee9SDonát Nagy 203*21e58ee9SDonát Nagy**Placing on C functions**: For C functions returning Cocoa objects, the 204*21e58ee9SDonát Nagyanalyzer typically does not make any assumptions about whether or not the object 205*21e58ee9SDonát Nagyis returned retained. Explicitly adding the 'ns_returns_retained' attribute to C 206*21e58ee9SDonát Nagyfunctions allows the analyzer to perform extra checking. 207*21e58ee9SDonát Nagy 208*21e58ee9SDonát Nagy**Example** 209*21e58ee9SDonát Nagy 210*21e58ee9SDonát Nagy.. code-block: objc 211*21e58ee9SDonát Nagy 212*21e58ee9SDonát Nagy #import <Foundation/Foundation.h>; 213*21e58ee9SDonát Nagy 214*21e58ee9SDonát Nagy #ifndef __has_feature // Optional. 215*21e58ee9SDonát Nagy #define __has_feature(x) 0 // Compatibility with non-clang compilers. 216*21e58ee9SDonát Nagy #endif 217*21e58ee9SDonát Nagy 218*21e58ee9SDonát Nagy #ifndef NS_RETURNS_RETAINED 219*21e58ee9SDonát Nagy #if __has_feature(attribute_ns_returns_retained) 220*21e58ee9SDonát Nagy #define NS_RETURNS_RETAINED __attribute__((ns_returns_retained)) 221*21e58ee9SDonát Nagy #else 222*21e58ee9SDonát Nagy #define NS_RETURNS_RETAINED 223*21e58ee9SDonát Nagy #endif 224*21e58ee9SDonát Nagy #endif 225*21e58ee9SDonát Nagy 226*21e58ee9SDonát Nagy @interface MyClass : NSObject {} 227*21e58ee9SDonát Nagy - (NSString*) returnsRetained NS_RETURNS_RETAINED; 228*21e58ee9SDonát Nagy - (NSString*) alsoReturnsRetained; 229*21e58ee9SDonát Nagy @end 230*21e58ee9SDonát Nagy 231*21e58ee9SDonát Nagy @implementation MyClass 232*21e58ee9SDonát Nagy - (NSString*) returnsRetained { 233*21e58ee9SDonát Nagy return [[NSString alloc] initWithCString:"no leak here"]; 234*21e58ee9SDonát Nagy } 235*21e58ee9SDonát Nagy - (NSString*) alsoReturnsRetained { 236*21e58ee9SDonát Nagy return [[NSString alloc] initWithCString:"flag a leak"]; 237*21e58ee9SDonát Nagy } 238*21e58ee9SDonát Nagy @end 239*21e58ee9SDonát Nagy 240*21e58ee9SDonát NagyRunning ``scan-build`` on this source file produces the following output: 241*21e58ee9SDonát Nagy 242*21e58ee9SDonát Nagy.. image:: ../images/example_ns_returns_retained.png 243*21e58ee9SDonát Nagy 244*21e58ee9SDonát Nagy.. _ns_returns_not_retained: 245*21e58ee9SDonát Nagy 246*21e58ee9SDonát NagyAttribute 'ns_returns_not_retained' (Clang-specific) 247*21e58ee9SDonát Nagy---------------------------------------------------- 248*21e58ee9SDonát Nagy 249*21e58ee9SDonát NagyThe 'ns_returns_not_retained' attribute is the complement of 250*21e58ee9SDonát Nagy'`ns_returns_retained`_'. Where a function or method may appear to obey the 251*21e58ee9SDonát NagyCocoa conventions and return a retained Cocoa object, this attribute can be 252*21e58ee9SDonát Nagyused to indicate that the object reference returned should not be considered as 253*21e58ee9SDonát Nagyan "owning" reference being returned to the caller. The Foundation 254*21e58ee9SDonát Nagyframework defines a macro ``NS_RETURNS_NOT_RETAINED`` that is functionally 255*21e58ee9SDonát Nagyequivalent to the one shown below. 256*21e58ee9SDonát Nagy 257*21e58ee9SDonát NagyUsage is identical to `ns_returns_retained`_. When using the 258*21e58ee9SDonát Nagyattribute, be sure to declare it within the proper macro that checks for 259*21e58ee9SDonát Nagyits availability, as it is not available in earlier versions of the analyzer: 260*21e58ee9SDonát Nagy 261*21e58ee9SDonát Nagy.. code-block:objc 262*21e58ee9SDonát Nagy 263*21e58ee9SDonát Nagy #ifndef __has_feature // Optional. 264*21e58ee9SDonát Nagy #define __has_feature(x) 0 // Compatibility with non-clang compilers. 265*21e58ee9SDonát Nagy #endif 266*21e58ee9SDonát Nagy 267*21e58ee9SDonát Nagy #ifndef NS_RETURNS_NOT_RETAINED 268*21e58ee9SDonát Nagy #if __has_feature(attribute_ns_returns_not_retained) 269*21e58ee9SDonát Nagy #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained)) 270*21e58ee9SDonát Nagy #else 271*21e58ee9SDonát Nagy #define NS_RETURNS_NOT_RETAINED 272*21e58ee9SDonát Nagy #endif 273*21e58ee9SDonát Nagy #endif 274*21e58ee9SDonát Nagy 275*21e58ee9SDonát Nagy.. _cf_returns_retained: 276*21e58ee9SDonát Nagy 277*21e58ee9SDonát NagyAttribute 'cf_returns_retained' (Clang-specific) 278*21e58ee9SDonát Nagy------------------------------------------------ 279*21e58ee9SDonát Nagy 280*21e58ee9SDonát NagyThe GCC-style (Clang-specific) attribute 'cf_returns_retained' allows one to 281*21e58ee9SDonát Nagyannotate an Objective-C method or C function as returning a retained Core 282*21e58ee9SDonát NagyFoundation object that the caller is responsible for releasing. The 283*21e58ee9SDonát NagyCoreFoundation framework defines a macro ``CF_RETURNS_RETAINED`` that is 284*21e58ee9SDonát Nagyfunctionally equivalent to the one shown below. 285*21e58ee9SDonát Nagy 286*21e58ee9SDonát Nagy**Placing on Objective-C methods**: With respect to Objective-C methods., 287*21e58ee9SDonát Nagythis attribute is identical in its behavior and usage to 'ns_returns_retained' 288*21e58ee9SDonát Nagyexcept for the distinction of returning a Core Foundation object instead of a 289*21e58ee9SDonát NagyCocoa object. 290*21e58ee9SDonát Nagy 291*21e58ee9SDonát NagyThis distinction is important for the following reason: as Core Foundation is a 292*21e58ee9SDonát NagyC API, the analyzer cannot always tell that a pointer return value refers to a 293*21e58ee9SDonát NagyCore Foundation object. In contrast, it is trivial for the analyzer to 294*21e58ee9SDonát Nagyrecognize if a pointer refers to a Cocoa object (given the Objective-C type 295*21e58ee9SDonát Nagysystem). 296*21e58ee9SDonát Nagy 297*21e58ee9SDonát Nagy**Placing on C functions**: When placing the attribute 298*21e58ee9SDonát Nagy'cf_returns_retained' on the declarations of C functions, the analyzer 299*21e58ee9SDonát Nagyinterprets the function as: 300*21e58ee9SDonát Nagy 301*21e58ee9SDonát Nagy1. Returning a Core Foundation Object 302*21e58ee9SDonát Nagy2. Treating the function as if it its name contained the keywords 303*21e58ee9SDonát Nagy "create" or "copy". This means the returned object as a 304*21e58ee9SDonát Nagy +1 retain count that must be released by the caller, either by sending a 305*21e58ee9SDonát Nagy ``release`` message (via toll-free bridging to an Objective-C object 306*21e58ee9SDonát Nagy pointer), or calling ``CFRelease`` or a similar function. 307*21e58ee9SDonát Nagy 308*21e58ee9SDonát Nagy**Example** 309*21e58ee9SDonát Nagy 310*21e58ee9SDonát Nagy.. code-block:objc 311*21e58ee9SDonát Nagy 312*21e58ee9SDonát Nagy #import <Cocoa/Cocoa.h> 313*21e58ee9SDonát Nagy 314*21e58ee9SDonát Nagy #ifndef __has_feature // Optional. 315*21e58ee9SDonát Nagy #define __has_feature(x) 0 // Compatibility with non-clang compilers. 316*21e58ee9SDonát Nagy #endif 317*21e58ee9SDonát Nagy 318*21e58ee9SDonát Nagy #ifndef CF_RETURNS_RETAINED 319*21e58ee9SDonát Nagy #if __has_feature(attribute_cf_returns_retained) 320*21e58ee9SDonát Nagy #define CF_RETURNS_RETAINED __attribute__((cf_returns_retained)) 321*21e58ee9SDonát Nagy #else 322*21e58ee9SDonát Nagy #define CF_RETURNS_RETAINED 323*21e58ee9SDonát Nagy #endif 324*21e58ee9SDonát Nagy #endif 325*21e58ee9SDonát Nagy 326*21e58ee9SDonát Nagy @interface MyClass : NSObject {} 327*21e58ee9SDonát Nagy - (NSDate*) returnsCFRetained CF_RETURNS_RETAINED; 328*21e58ee9SDonát Nagy - (NSDate*) alsoReturnsRetained; 329*21e58ee9SDonát Nagy - (NSDate*) returnsNSRetained NS_RETURNS_RETAINED; 330*21e58ee9SDonát Nagy @end 331*21e58ee9SDonát Nagy 332*21e58ee9SDonát Nagy CF_RETURNS_RETAINED 333*21e58ee9SDonát Nagy CFDateRef returnsRetainedCFDate() { 334*21e58ee9SDonát Nagy return CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 335*21e58ee9SDonát Nagy } 336*21e58ee9SDonát Nagy 337*21e58ee9SDonát Nagy @implementation MyClass 338*21e58ee9SDonát Nagy - (NSDate*) returnsCFRetained { 339*21e58ee9SDonát Nagy return (NSDate*) returnsRetainedCFDate(); // No leak. 340*21e58ee9SDonát Nagy } 341*21e58ee9SDonát Nagy 342*21e58ee9SDonát Nagy - (NSDate*) alsoReturnsRetained { 343*21e58ee9SDonát Nagy return (NSDate*) returnsRetainedCFDate(); // Always report a leak. 344*21e58ee9SDonát Nagy } 345*21e58ee9SDonát Nagy 346*21e58ee9SDonát Nagy - (NSDate*) returnsNSRetained { 347*21e58ee9SDonát Nagy return (NSDate*) returnsRetainedCFDate(); // Report a leak when using GC. 348*21e58ee9SDonát Nagy } 349*21e58ee9SDonát Nagy @end 350*21e58ee9SDonát Nagy 351*21e58ee9SDonát NagyRunning ``scan-build`` on this example produces the following output: 352*21e58ee9SDonát Nagy 353*21e58ee9SDonát Nagy.. image:: ../images/example_cf_returns_retained.png 354*21e58ee9SDonát Nagy 355*21e58ee9SDonát NagyAttribute 'cf_returns_not_retained' (Clang-specific) 356*21e58ee9SDonát Nagy---------------------------------------------------- 357*21e58ee9SDonát Nagy 358*21e58ee9SDonát NagyThe 'cf_returns_not_retained' attribute is the complement of 359*21e58ee9SDonát Nagy'`cf_returns_retained`_'. Where a function or method may appear to obey the 360*21e58ee9SDonát NagyCore Foundation or Cocoa conventions and return a retained Core Foundation 361*21e58ee9SDonát Nagyobject, this attribute can be used to indicate that the object reference 362*21e58ee9SDonát Nagyreturned should not be considered as an "owning" reference being 363*21e58ee9SDonát Nagyreturned to the caller. The CoreFoundation framework defines a macro 364*21e58ee9SDonát Nagy**``CF_RETURNS_NOT_RETAINED``** that is functionally equivalent to the one 365*21e58ee9SDonát Nagyshown below. 366*21e58ee9SDonát Nagy 367*21e58ee9SDonát NagyUsage is identical to cf_returns_retained_. When using the attribute, be sure 368*21e58ee9SDonát Nagyto declare it within the proper macro that checks for its availability, as it 369*21e58ee9SDonát Nagyis not available in earlier versions of the analyzer: 370*21e58ee9SDonát Nagy 371*21e58ee9SDonát Nagy.. code-block:objc 372*21e58ee9SDonát Nagy 373*21e58ee9SDonát Nagy #ifndef __has_feature // Optional. 374*21e58ee9SDonát Nagy #define __has_feature(x) 0 // Compatibility with non-clang compilers. 375*21e58ee9SDonát Nagy #endif 376*21e58ee9SDonát Nagy 377*21e58ee9SDonát Nagy #ifndef CF_RETURNS_NOT_RETAINED 378*21e58ee9SDonát Nagy #if __has_feature(attribute_cf_returns_not_retained) 379*21e58ee9SDonát Nagy #define CF_RETURNS_NOT_RETAINED __attribute__((cf_returns_not_retained)) 380*21e58ee9SDonát Nagy #else 381*21e58ee9SDonát Nagy #define CF_RETURNS_NOT_RETAINED 382*21e58ee9SDonát Nagy #endif 383*21e58ee9SDonát Nagy #endif 384*21e58ee9SDonát Nagy 385*21e58ee9SDonát Nagy.. _ns_consumed: 386*21e58ee9SDonát Nagy 387*21e58ee9SDonát NagyAttribute 'ns_consumed' (Clang-specific) 388*21e58ee9SDonát Nagy---------------------------------------- 389*21e58ee9SDonát Nagy 390*21e58ee9SDonát NagyThe 'ns_consumed' attribute can be placed on a specific parameter in either 391*21e58ee9SDonát Nagythe declaration of a function or an Objective-C method. It indicates to the 392*21e58ee9SDonát Nagystatic analyzer that a ``release`` message is implicitly sent to the 393*21e58ee9SDonát Nagyparameter upon completion of the call to the given function or method. The 394*21e58ee9SDonát NagyFoundation framework defines a macro ``NS_RELEASES_ARGUMENT`` that 395*21e58ee9SDonát Nagyis functionally equivalent to the ``NS_CONSUMED`` macro shown below. 396*21e58ee9SDonát Nagy 397*21e58ee9SDonát Nagy**Example** 398*21e58ee9SDonát Nagy 399*21e58ee9SDonát Nagy.. code-block:objc 400*21e58ee9SDonát Nagy 401*21e58ee9SDonát Nagy #ifndef __has_feature // Optional. 402*21e58ee9SDonát Nagy #define __has_feature(x) 0 // Compatibility with non-clang compilers. 403*21e58ee9SDonát Nagy #endif 404*21e58ee9SDonát Nagy 405*21e58ee9SDonát Nagy #ifndef NS_CONSUMED 406*21e58ee9SDonát Nagy #if __has_feature(attribute_ns_consumed) 407*21e58ee9SDonát Nagy #define NS_CONSUMED __attribute__((ns_consumed)) 408*21e58ee9SDonát Nagy #else 409*21e58ee9SDonát Nagy #define NS_CONSUMED 410*21e58ee9SDonát Nagy #endif 411*21e58ee9SDonát Nagy #endif 412*21e58ee9SDonát Nagy 413*21e58ee9SDonát Nagy void consume_ns(id NS_CONSUMED x); 414*21e58ee9SDonát Nagy 415*21e58ee9SDonát Nagy void test() { 416*21e58ee9SDonát Nagy id x = [[NSObject alloc] init]; 417*21e58ee9SDonát Nagy consume_ns(x); // No leak! 418*21e58ee9SDonát Nagy } 419*21e58ee9SDonát Nagy 420*21e58ee9SDonát Nagy @interface Foo : NSObject 421*21e58ee9SDonát Nagy + (void) releaseArg:(id) NS_CONSUMED x; 422*21e58ee9SDonát Nagy + (void) releaseSecondArg:(id)x second:(id) NS_CONSUMED y; 423*21e58ee9SDonát Nagy @end 424*21e58ee9SDonát Nagy 425*21e58ee9SDonát Nagy void test_method() { 426*21e58ee9SDonát Nagy id x = [[NSObject alloc] init]; 427*21e58ee9SDonát Nagy [Foo releaseArg:x]; // No leak! 428*21e58ee9SDonát Nagy } 429*21e58ee9SDonát Nagy 430*21e58ee9SDonát Nagy void test_method2() { 431*21e58ee9SDonát Nagy id a = [[NSObject alloc] init]; 432*21e58ee9SDonát Nagy id b = [[NSObject alloc] init]; 433*21e58ee9SDonát Nagy [Foo releaseSecondArg:a second:b]; // 'a' is leaked, but 'b' is released. 434*21e58ee9SDonát Nagy } 435*21e58ee9SDonát Nagy 436*21e58ee9SDonát NagyAttribute 'cf_consumed' (Clang-specific) 437*21e58ee9SDonát Nagy---------------------------------------- 438*21e58ee9SDonát Nagy 439*21e58ee9SDonát NagyThe 'cf_consumed' attribute is practically identical to ns_consumed_. The 440*21e58ee9SDonát Nagyattribute can be placed on a specific parameter in either the declaration of a 441*21e58ee9SDonát Nagyfunction or an Objective-C method. It indicates to the static analyzer that the 442*21e58ee9SDonát Nagyobject reference is implicitly passed to a call to ``CFRelease`` upon 443*21e58ee9SDonát Nagycompletion of the call to the given function or method. The CoreFoundation 444*21e58ee9SDonát Nagyframework defines a macro ``CF_RELEASES_ARGUMENT`` that is functionally 445*21e58ee9SDonát Nagyequivalent to the ``CF_CONSUMED`` macro shown below. 446*21e58ee9SDonát Nagy 447*21e58ee9SDonát NagyOperationally this attribute is nearly identical to 'ns_consumed'. 448*21e58ee9SDonát Nagy 449*21e58ee9SDonát Nagy**Example** 450*21e58ee9SDonát Nagy 451*21e58ee9SDonát Nagy.. code-block:objc 452*21e58ee9SDonát Nagy 453*21e58ee9SDonát Nagy #ifndef __has_feature // Optional. 454*21e58ee9SDonát Nagy #define __has_feature(x) 0 // Compatibility with non-clang compilers. 455*21e58ee9SDonát Nagy #endif 456*21e58ee9SDonát Nagy 457*21e58ee9SDonát Nagy #ifndef CF_CONSUMED 458*21e58ee9SDonát Nagy #if __has_feature(attribute_cf_consumed) 459*21e58ee9SDonát Nagy #define CF_CONSUMED __attribute__((cf_consumed)) 460*21e58ee9SDonát Nagy #else 461*21e58ee9SDonát Nagy #define CF_CONSUMED 462*21e58ee9SDonát Nagy #endif 463*21e58ee9SDonát Nagy #endif 464*21e58ee9SDonát Nagy 465*21e58ee9SDonát Nagy void consume_cf(id CF_CONSUMED x); 466*21e58ee9SDonát Nagy void consume_CFDate(CFDateRef CF_CONSUMED x); 467*21e58ee9SDonát Nagy 468*21e58ee9SDonát Nagy void test() { 469*21e58ee9SDonát Nagy id x = [[NSObject alloc] init]; 470*21e58ee9SDonát Nagy consume_cf(x); // No leak! 471*21e58ee9SDonát Nagy } 472*21e58ee9SDonát Nagy 473*21e58ee9SDonát Nagy void test2() { 474*21e58ee9SDonát Nagy CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 475*21e58ee9SDonát Nagy consume_CFDate(date); // No leak, including under GC! 476*21e58ee9SDonát Nagy 477*21e58ee9SDonát Nagy } 478*21e58ee9SDonát Nagy 479*21e58ee9SDonát Nagy @interface Foo : NSObject 480*21e58ee9SDonát Nagy + (void) releaseArg:(CFDateRef) CF_CONSUMED x; 481*21e58ee9SDonát Nagy @end 482*21e58ee9SDonát Nagy 483*21e58ee9SDonát Nagy void test_method() { 484*21e58ee9SDonát Nagy CFDateRef date = CFDateCreate(0, CFAbsoluteTimeGetCurrent()); 485*21e58ee9SDonát Nagy [Foo releaseArg:date]; // No leak! 486*21e58ee9SDonát Nagy } 487*21e58ee9SDonát Nagy 488*21e58ee9SDonát Nagy.. _ns_consumes_self: 489*21e58ee9SDonát Nagy 490*21e58ee9SDonát NagyAttribute 'ns_consumes_self' (Clang-specific) 491*21e58ee9SDonát Nagy--------------------------------------------- 492*21e58ee9SDonát Nagy 493*21e58ee9SDonát NagyThe 'ns_consumes_self' attribute can be placed only on an Objective-C method 494*21e58ee9SDonát Nagydeclaration. It indicates that the receiver of the message is 495*21e58ee9SDonát Nagy"consumed" (a single reference count decremented) after the message 496*21e58ee9SDonát Nagyis sent. This matches the semantics of all "init" methods. 497*21e58ee9SDonát Nagy 498*21e58ee9SDonát NagyOne use of this attribute is declare your own init-like methods that do not 499*21e58ee9SDonát Nagyfollow the standard Cocoa naming conventions. 500*21e58ee9SDonát Nagy 501*21e58ee9SDonát Nagy**Example** 502*21e58ee9SDonát Nagy 503*21e58ee9SDonát Nagy.. code-block:objc 504*21e58ee9SDonát Nagy #ifndef __has_feature 505*21e58ee9SDonát Nagy #define __has_feature(x) 0 // Compatibility with non-clang compilers. 506*21e58ee9SDonát Nagy #endif 507*21e58ee9SDonát Nagy 508*21e58ee9SDonát Nagy #ifndef NS_CONSUMES_SELF 509*21e58ee9SDonát Nagy #if __has_feature((attribute_ns_consumes_self)) 510*21e58ee9SDonát Nagy #define NS_CONSUMES_SELF __attribute__((ns_consumes_self)) 511*21e58ee9SDonát Nagy #else 512*21e58ee9SDonát Nagy #define NS_CONSUMES_SELF 513*21e58ee9SDonát Nagy #endif 514*21e58ee9SDonát Nagy #endif 515*21e58ee9SDonát Nagy 516*21e58ee9SDonát Nagy @interface MyClass : NSObject 517*21e58ee9SDonát Nagy - initWith:(MyClass *)x; 518*21e58ee9SDonát Nagy - nonstandardInitWith:(MyClass *)x NS_CONSUMES_SELF NS_RETURNS_RETAINED; 519*21e58ee9SDonát Nagy @end 520*21e58ee9SDonát Nagy 521*21e58ee9SDonát NagyIn this example, ``-nonstandardInitWith:`` has the same ownership 522*21e58ee9SDonát Nagysemantics as the init method ``-initWith:``. The static analyzer will 523*21e58ee9SDonát Nagyobserve that the method consumes the receiver, and then returns an object with 524*21e58ee9SDonát Nagya +1 retain count. 525*21e58ee9SDonát Nagy 526*21e58ee9SDonát NagyThe Foundation framework defines a macro ``NS_REPLACES_RECEIVER`` which is 527*21e58ee9SDonát Nagyfunctionally equivalent to the combination of ``NS_CONSUMES_SELF`` and 528*21e58ee9SDonát Nagy``NS_RETURNS_RETAINED`` shown above. 529*21e58ee9SDonát Nagy 530*21e58ee9SDonát NagyLibkern Memory Management Annotations 531*21e58ee9SDonát Nagy##################################### 532*21e58ee9SDonát Nagy 533*21e58ee9SDonát Nagy`Libkern <https://developer.apple.com/documentation/kernel/osobject?language=objc>`_ 534*21e58ee9SDonát Nagyrequires developers to inherit all heap allocated objects from ``OSObject`` and 535*21e58ee9SDonát Nagyto perform manual reference counting. The reference counting model is very 536*21e58ee9SDonát Nagysimilar to MRR (manual retain-release) mode in 537*21e58ee9SDonát Nagy`Objective-C <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html>`_ 538*21e58ee9SDonát Nagyor to CoreFoundation reference counting. 539*21e58ee9SDonát NagyFreshly-allocated objects start with a reference count of 1, and calls to 540*21e58ee9SDonát Nagy``retain`` increment it, while calls to ``release`` decrement it. The object is 541*21e58ee9SDonát Nagydeallocated whenever its reference count reaches zero. 542*21e58ee9SDonát Nagy 543*21e58ee9SDonát NagyManually incrementing and decrementing reference counts is error-prone: 544*21e58ee9SDonát Nagyover-retains lead to leaks, and over-releases lead to uses-after-free. 545*21e58ee9SDonát NagyThe analyzer can help the programmer to check for unbalanced 546*21e58ee9SDonát Nagyretain/release calls. 547*21e58ee9SDonát Nagy 548*21e58ee9SDonát NagyThe reference count checking is based on the principle of *locality*: it should 549*21e58ee9SDonát Nagybe possible to establish correctness (lack of leaks/uses after free) by looking 550*21e58ee9SDonát Nagyat each function body, and the declarations (not the definitions) of all the 551*21e58ee9SDonát Nagyfunctions it interacts with. 552*21e58ee9SDonát Nagy 553*21e58ee9SDonát NagyIn order to support such reasoning, it should be possible to *summarize* the 554*21e58ee9SDonát Nagybehavior of each function, with respect to reference count of its returned 555*21e58ee9SDonát Nagyvalues and attributes. 556*21e58ee9SDonát Nagy 557*21e58ee9SDonát NagyBy default, the following summaries are assumed: 558*21e58ee9SDonát Nagy 559*21e58ee9SDonát Nagy- All functions starting with ``get`` or ``Get``, unless they are returning 560*21e58ee9SDonát Nagy subclasses of ``OSIterator``, are assumed to be returning at +0. That is, the 561*21e58ee9SDonát Nagy caller has no reference count *obligations* with respect to the reference 562*21e58ee9SDonát Nagy count of the returned object and should leave it untouched. 563*21e58ee9SDonát Nagy 564*21e58ee9SDonát Nagy- All other functions are assumed to return at +1. That is, the caller has an 565*21e58ee9SDonát Nagy *obligation* to release such objects. 566*21e58ee9SDonát Nagy 567*21e58ee9SDonát Nagy- Functions are assumed not to change the reference count of their parameters, 568*21e58ee9SDonát Nagy including the implicit ``this`` parameter. 569*21e58ee9SDonát Nagy 570*21e58ee9SDonát NagyThese summaries can be overriden with the following 571*21e58ee9SDonát Nagy`attributes <https://clang.llvm.org/docs/AttributeReference.html#os-returns-not-retained>`_: 572*21e58ee9SDonát Nagy 573*21e58ee9SDonát NagyAttribute 'os_returns_retained' 574*21e58ee9SDonát Nagy------------------------------- 575*21e58ee9SDonát Nagy 576*21e58ee9SDonát NagyThe ``os_returns_retained`` attribute (accessed through the macro 577*21e58ee9SDonát Nagy``LIBKERN_RETURNS_RETAINED``) plays a role identical to `ns_returns_retained`_ 578*21e58ee9SDonát Nagyfor functions returning ``OSObject`` subclasses. The attribute indicates that 579*21e58ee9SDonát Nagyit is a callers responsibility to release the returned object. 580*21e58ee9SDonát Nagy 581*21e58ee9SDonát NagyAttribute 'os_returns_not_retained' 582*21e58ee9SDonát Nagy----------------------------------- 583*21e58ee9SDonát Nagy 584*21e58ee9SDonát NagyThe ``os_returns_not_retained`` attribute (accessed through the macro 585*21e58ee9SDonát Nagy``LIBKERN_RETURNS_NOT_RETAINED``) plays a role identical to 586*21e58ee9SDonát Nagy`ns_returns_not_retained`_ for functions returning ``OSObject`` subclasses. The 587*21e58ee9SDonát Nagyattribute indicates that the caller should not change the retain count of the 588*21e58ee9SDonát Nagyreturned object. 589*21e58ee9SDonát Nagy 590*21e58ee9SDonát Nagy 591*21e58ee9SDonát Nagy**Example** 592*21e58ee9SDonát Nagy 593*21e58ee9SDonát Nagy.. code-block:objc 594*21e58ee9SDonát Nagy 595*21e58ee9SDonát Nagy class MyClass { 596*21e58ee9SDonát Nagy OSObject *f; 597*21e58ee9SDonát Nagy LIBKERN_RETURNS_NOT_RETAINED OSObject *myFieldGetter(); 598*21e58ee9SDonát Nagy } 599*21e58ee9SDonát Nagy 600*21e58ee9SDonát Nagy 601*21e58ee9SDonát Nagy // Note that the annotation only has to be applied to the function declaration. 602*21e58ee9SDonát Nagy OSObject * MyClass::myFieldGetter() { 603*21e58ee9SDonát Nagy return f; 604*21e58ee9SDonát Nagy } 605*21e58ee9SDonát Nagy 606*21e58ee9SDonát NagyAttribute 'os_consumed' 607*21e58ee9SDonát Nagy----------------------- 608*21e58ee9SDonát Nagy 609*21e58ee9SDonát NagySimilarly to `ns_consumed`_ attribute, ``os_consumed`` (accessed through 610*21e58ee9SDonát Nagy``LIBKERN_CONSUMED``) attribute, applied to a parameter, indicates that the 611*21e58ee9SDonát Nagycall to the function *consumes* the parameter: the callee should either release 612*21e58ee9SDonát Nagyit or store it and release it in the destructor, while the caller should assume 613*21e58ee9SDonát Nagyone is subtracted from the reference count after the call. 614*21e58ee9SDonát Nagy 615*21e58ee9SDonát Nagy.. code-block:objc 616*21e58ee9SDonát Nagy IOReturn addToList(LIBKERN_CONSUMED IOPMinformee *newInformee); 617*21e58ee9SDonát Nagy 618*21e58ee9SDonát NagyAttribute 'os_consumes_this' 619*21e58ee9SDonát Nagy---------------------------- 620*21e58ee9SDonát Nagy 621*21e58ee9SDonát NagySimilarly to `ns_consumes_self`_, the ``os_consumes_self`` attribute indicates 622*21e58ee9SDonát Nagythat the method call *consumes* the implicit ``this`` argument: the caller 623*21e58ee9SDonát Nagyshould assume one was subtracted from the reference count of the object after 624*21e58ee9SDonát Nagythe call, and the callee has on obligation to either release the argument, or 625*21e58ee9SDonát Nagystore it and eventually release it in the destructor. 626*21e58ee9SDonát Nagy 627*21e58ee9SDonát Nagy 628*21e58ee9SDonát Nagy.. code-block:objc 629*21e58ee9SDonát Nagy void addThisToList(OSArray *givenList) LIBKERN_CONSUMES_THIS; 630*21e58ee9SDonát Nagy 631*21e58ee9SDonát NagyOut Parameters 632*21e58ee9SDonát Nagy-------------- 633*21e58ee9SDonát Nagy 634*21e58ee9SDonát NagyA function can also return an object to a caller by a means of an out parameter 635*21e58ee9SDonát Nagy(a pointer-to-OSObject-pointer is passed, and a callee writes a pointer to an 636*21e58ee9SDonát Nagyobject into an argument). Currently the analyzer does not track unannotated out 637*21e58ee9SDonát Nagyparameters by default, but with annotations we distinguish four separate cases: 638*21e58ee9SDonát Nagy 639*21e58ee9SDonát Nagy**1. Non-retained out parameters**, identified using 640*21e58ee9SDonát Nagy``LIBKERN_RETURNS_NOT_RETAINED`` applied to parameters, e.g.: 641*21e58ee9SDonát Nagy 642*21e58ee9SDonát Nagy.. code-block:objc 643*21e58ee9SDonát Nagy void getterViaOutParam(LIBKERN_RETURNS_NOT_RETAINED OSObject **obj) 644*21e58ee9SDonát Nagy 645*21e58ee9SDonát NagySuch functions write a non-retained object into an out parameter, and the 646*21e58ee9SDonát Nagycaller has no further obligations. 647*21e58ee9SDonát Nagy 648*21e58ee9SDonát Nagy**2. Retained out parameters**, identified using ``LIBKERN_RETURNS_RETAINED``: 649*21e58ee9SDonát Nagy 650*21e58ee9SDonát Nagy.. code-block:objc 651*21e58ee9SDonát Nagy void getterViaOutParam(LIBKERN_RETURNS_NOT_RETAINED OSObject **obj) 652*21e58ee9SDonát Nagy 653*21e58ee9SDonát NagyIn such cases a retained object is written into an out parameter, which the caller has then to release in order to avoid a leak. 654*21e58ee9SDonát Nagy 655*21e58ee9SDonát NagyThese two cases are simple - but in practice a functions returning an 656*21e58ee9SDonát Nagyout-parameter usually also return a return code, and then an out parameter may 657*21e58ee9SDonát Nagyor may not be written, which conditionally depends on the exit code, e.g.: 658*21e58ee9SDonát Nagy 659*21e58ee9SDonát Nagy.. code-block:objc 660*21e58ee9SDonát Nagy bool maybeCreateObject(LIBKERN_RETURNS_RETAINED OSObject **obj); 661*21e58ee9SDonát Nagy 662*21e58ee9SDonát NagyFor such functions, the usual semantics is that an object is written into on "success", and not written into on "failure". 663*21e58ee9SDonát Nagy 664*21e58ee9SDonát NagyFor ``LIBKERN_RETURNS_RETAINED`` we assume the following definition of 665*21e58ee9SDonát Nagysuccess: 666*21e58ee9SDonát Nagy 667*21e58ee9SDonát Nagy- For functions returning ``OSReturn`` or ``IOReturn`` (any typedef to 668*21e58ee9SDonát Nagy ``kern_return_t``) success is defined as having an output of zero 669*21e58ee9SDonát Nagy (``kIOReturnSuccess`` is zero). 670*21e58ee9SDonát Nagy 671*21e58ee9SDonát Nagy- For all others, success is non-zero (e.g. non-nullptr for pointers) 672*21e58ee9SDonát Nagy 673*21e58ee9SDonát Nagy**3. Retained out parameters on zero return** The annotation 674*21e58ee9SDonát Nagy``LIBKERN_RETURNS_RETAINED_ON_ZERO`` states that a retained object is written 675*21e58ee9SDonát Nagyinto if and only if the function returns a zero value: 676*21e58ee9SDonát Nagy 677*21e58ee9SDonát Nagy.. code-block:objc 678*21e58ee9SDonát Nagy bool OSUnserializeXML(void *data, LIBKERN_RETURNS_RETAINED_ON_ZERO OSString **errString); 679*21e58ee9SDonát Nagy 680*21e58ee9SDonát NagyThen the caller has to release an object if the function has returned zero. 681*21e58ee9SDonát Nagy 682*21e58ee9SDonát Nagy**4. Retained out parameters on non-zero return** Similarly, 683*21e58ee9SDonát Nagy``LIBKERN_RETURNS_RETAINED_ON_NONZERO`` specifies that a retained object is 684*21e58ee9SDonát Nagywritten into the parameter if and only if the function has returned a non-zero 685*21e58ee9SDonát Nagyvalue. 686*21e58ee9SDonát Nagy 687*21e58ee9SDonát NagyNote that for non-retained out parameters conditionals do not matter, as the 688*21e58ee9SDonát Nagycaller has no obligations regardless of whether an object is written into or 689*21e58ee9SDonát Nagynot. 690