File: retain-release-safe.c

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (93 lines) | stat: -rw-r--r-- 3,154 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.RetainCount -verify %s
// RUN: %clang_analyze_cc1 -analyzer-checker=core,osx.coreFoundation.CFRetainRelease,osx.cocoa.RetainCount -analyzer-inline-max-stack-depth=0 -verify %s

#pragma clang arc_cf_code_audited begin
typedef const void * CFTypeRef;
extern CFTypeRef CFRetain(CFTypeRef cf);
extern void CFRelease(CFTypeRef cf);
#pragma clang arc_cf_code_audited end

#define CF_RETURNS_RETAINED __attribute__((cf_returns_retained))
#define CF_CONSUMED __attribute__((cf_consumed))

extern CFTypeRef CFCreate() CF_RETURNS_RETAINED;

// A "safe" variant of CFRetain that doesn't crash when a null pointer is
// retained. This is often defined by users in a similar manner. The
// CF_RETURNS_RETAINED annotation is misleading here, because the function
// is not supposed to return an object with a +1 retain count. Instead, it
// is supposed to return an object with +(N+1) retain count, where N is
// the original retain count of 'cf'. However, there is no good annotation
// to use in this case, and it is pointless to provide such annotation
// because the only use cases would be CFRetain and SafeCFRetain.
// So instead we teach the analyzer to be able to accept such code
// and ignore the misplaced annotation.
CFTypeRef SafeCFRetain(CFTypeRef cf) CF_RETURNS_RETAINED {
  if (cf) {
    return CFRetain(cf);
  }
  return cf;
}

// A "safe" variant of CFRelease that doesn't crash when a null pointer is
// released. The CF_CONSUMED annotation seems reasonable here.
void SafeCFRelease(CFTypeRef CF_CONSUMED cf) {
  if (cf)
    CFRelease(cf); // no-warning (when inlined)
}

// The same thing, just with a different naming style.
CFTypeRef retainCFType(CFTypeRef cf) CF_RETURNS_RETAINED {
  if (cf) {
    return CFRetain(cf);
  }
  return cf;
}

void releaseCFType(CFTypeRef CF_CONSUMED cf) {
  if (cf)
    CFRelease(cf); // no-warning (when inlined)
}

void escape(CFTypeRef cf);

void makeSureTestsWork() {
  CFTypeRef cf = CFCreate();
  CFRelease(cf);
  CFRelease(cf); // expected-warning{{Reference-counted object is used after it is released}}
}

// Make sure we understand that the second SafeCFRetain doesn't return an
// object with +1 retain count, which we won't be able to release twice.
void falseOverrelease(CFTypeRef cf) {
  SafeCFRetain(cf);
  SafeCFRetain(cf);
  SafeCFRelease(cf);
  SafeCFRelease(cf); // no-warning after inlining this.
}

// Regular CFRelease() should behave similarly.
void sameWithNormalRelease(CFTypeRef cf) {
  SafeCFRetain(cf);
  SafeCFRetain(cf);
  CFRelease(cf);
  CFRelease(cf); // no-warning
}

// Make sure we understand that the second SafeCFRetain doesn't return an
// object with +1 retain count, which would no longer be owned by us after
// it escapes to escape() and released once.
void falseReleaseNotOwned(CFTypeRef cf) {
  SafeCFRetain(cf);
  SafeCFRetain(cf);
  escape(cf);
  SafeCFRelease(cf);
  SafeCFRelease(cf); // no-warning after inlining this.
}

void testTheOtherNamingConvention(CFTypeRef cf) {
  retainCFType(cf);
  retainCFType(cf);
  releaseCFType(cf);
  releaseCFType(cf); // no-warning
}