xref: /llvm-project/clang/test/Analysis/std-string.cpp (revision e1fdec875ff13504057fa01227458c9afa08222f)
1 // RUN: %clang_analyze_cc1 -std=c++14 %s -verify                  \
2 // RUN:   -analyzer-checker=core,unix.Malloc,debug.ExprInspection \
3 // RUN:   -analyzer-checker=cplusplus.StringChecker               \
4 // RUN:   -analyzer-config eagerly-assume=false                   \
5 // RUN:   -analyzer-output=text
6 
7 #include "Inputs/system-header-simulator-cxx.h"
8 
9 void clang_analyzer_eval(bool);
10 void clang_analyzer_warnIfReached();
11 
12 void free(void *ptr);
13 
14 void irrelevant_std_string_ctors(const char *p) {
15   std::string x1;                             // no-warning
16   std::string x2(2, 'x');                     // no-warning
17   std::string x3(x1, /*pos=*/2);              // no-warning
18   std::string x4(x1, /*pos=*/2, /*count=*/2); // no-warning
19   std::string x5(p, /*count=*/(size_t)2);     // no-warning
20   // skip std::string(const char*)
21   std::string x6(x1.begin(), x1.end()); // no-warning
22   std::string x7(x1);                   // no-warning
23   std::string x8(std::move(x1));        // no-warning
24   std::string x9({'a', 'b', '\0'});     // no-warning
25 }
26 
27 void null_cstring_parameter(const char *p) {
28   clang_analyzer_eval(p == 0); // expected-warning {{UNKNOWN}} expected-note {{UNKNOWN}}
29   if (!p) {
30     // expected-note@-1 2 {{Assuming 'p' is null}}
31     // expected-note@-2 2 {{Taking true branch}}
32     clang_analyzer_eval(p == 0); // expected-warning {{TRUE}} expected-note {{TRUE}}
33     std::string x(p);
34     // expected-warning@-1 {{The parameter must not be null}}
35     // expected-note@-2    {{The parameter must not be null}}
36     clang_analyzer_warnIfReached(); // no-warning
37   }
38 }
39 
40 void null_constant_parameter() {
41   std::string x((char *)0);
42   // expected-warning@-1 {{The parameter must not be null}}
43   // expected-note@-2    {{The parameter must not be null}}
44 }
45 
46 void ctor_notetag_on_constraining_symbol(const char *p) {
47   clang_analyzer_eval(p == 0); // expected-warning {{UNKNOWN}} expected-note {{UNKNOWN}}
48   std::string x(p);            // expected-note {{Assuming the pointer is not null}}
49   clang_analyzer_eval(p == 0); // expected-warning {{FALSE}} expected-note {{FALSE}}
50 
51   free((void *)p); // expected-note {{Memory is released}}
52   free((void *)p);
53   // expected-warning@-1 {{Attempt to free released memory}}
54   // expected-note@-2    {{Attempt to free released memory}}
55 }
56 
57 void ctor_no_notetag_symbol_already_constrained(const char *p) {
58   // expected-note@+2 + {{Assuming 'p' is non-null}}
59   // expected-note@+1 + {{Taking false branch}}
60   if (!p)
61     return;
62 
63   clang_analyzer_eval(p == 0); // expected-warning {{FALSE}} expected-note {{FALSE}}
64   std::string x(p);            // no-note: 'p' is already constrained to be non-null.
65   clang_analyzer_eval(p == 0); // expected-warning {{FALSE}} expected-note {{FALSE}}
66 
67   free((void *)p); // expected-note {{Memory is released}}
68   free((void *)p);
69   // expected-warning@-1 {{Attempt to free released memory}}
70   // expected-note@-2    {{Attempt to free released memory}}
71 }
72 
73 void ctor_no_notetag_if_not_interesting(const char *p1, const char *p2) {
74   std::string s1(p1); // expected-note {{Assuming the pointer is not null}}
75   std::string s2(p2); // no-note: s2 is not interesting
76 
77   free((void *)p1); // expected-note {{Memory is released}}
78   free((void *)p1);
79   // expected-warning@-1 {{Attempt to free released memory}}
80   // expected-note@-2    {{Attempt to free released memory}}
81 }
82