summaryrefslogtreecommitdiffstats
path: root/clang/test/Analysis/dangling-internal-buffer.cpp
blob: cd7ebb17c66ee11263cfabdc8d5bce245c7a7e3c (plain)
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
//RUN: %clang_analyze_cc1 -analyzer-checker=alpha.cplusplus.DanglingInternalBuffer %s -analyzer-output=text -verify

namespace std {

template< typename CharT >
class basic_string {
public:
  ~basic_string();
  const CharT *c_str();
};

typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;
typedef basic_string<char16_t> u16string;
typedef basic_string<char32_t> u32string;

} // end namespace std

void consume(const char *) {}
void consume(const wchar_t *) {}
void consume(const char16_t *) {}
void consume(const char32_t *) {}

void deref_after_scope_char() {
  const char *c;
  {
    std::string s;
    c = s.c_str();
  } // expected-note {{Internal buffer is released because the object was destroyed}}
  consume(c); // expected-warning {{Use of memory after it is freed}}
  // expected-note@-1 {{Use of memory after it is freed}}
}

void deref_after_scope_wchar_t() {
  const wchar_t *w;
  {
    std::wstring ws;
    w = ws.c_str();
  } // expected-note {{Internal buffer is released because the object was destroyed}}
  consume(w); // expected-warning {{Use of memory after it is freed}}
  // expected-note@-1 {{Use of memory after it is freed}}
}

void deref_after_scope_char16_t() {
  const char16_t *c16;
  {
    std::u16string s16;
    c16 = s16.c_str();
  } // expected-note {{Internal buffer is released because the object was destroyed}}
  consume(c16); // expected-warning {{Use of memory after it is freed}}
  // expected-note@-1 {{Use of memory after it is freed}}
}

void deref_after_scope_char32_t() {
  const char32_t *c32;
  {
    std::u32string s32;
    c32 = s32.c_str();
  } // expected-note {{Internal buffer is released because the object was destroyed}}
  consume(c32); // expected-warning {{Use of memory after it is freed}}
  // expected-note@-1 {{Use of memory after it is freed}}
}

void deref_after_scope_ok() {
  const char *c;
  std::string s;
  {
    c = s.c_str();
  }
  consume(c); // no-warning
}
OpenPOWER on IntegriCloud