xref: /llvm-project/clang/test/Analysis/diagnostics/undef-value-param.c (revision 0dd49a5628bbe01cecf6516017da59ae44863ab3)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=text -verify %s
2 // RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=plist-multi-file  %s -o %t.plist
3 // RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/undef-value-param.c.plist -
4 
foo_irrelevant(int c)5 void foo_irrelevant(int c) {
6     if (c)
7         return;
8     c++;
9     return;
10 }
foo(int c,int * x)11 void foo(int c, int *x) {
12     if (c)
13            //expected-note@-1{{Assuming 'c' is not equal to 0}}
14            //expected-note@-2{{Taking true branch}}
15            return; // expected-note{{Returning without writing to '*x'}}
16     *x = 5;
17 }
18 
use(int c)19 int use(int c) {
20     int xx; //expected-note {{'xx' declared without an initial value}}
21     int *y = &xx;
22     foo (c, y);
23                 //expected-note@-1{{Calling 'foo'}}
24                 //expected-note@-2{{Returning from 'foo'}}
25     foo_irrelevant(c);
26     return xx+3; //expected-warning{{The left operand of '+' is a garbage value}}
27                  //expected-note@-1{{The left operand of '+' is a garbage value}}
28 }
29 
initArray(int x,double XYZ[3])30 void initArray(int x, double XYZ[3]) {
31     if (x <= 0) //expected-note {{Taking true branch}}
32                 //expected-note@-1 {{Assuming 'x' is <= 0}}
33         return;
34     XYZ[0] = 1;
35     XYZ[1] = 1;
36     XYZ[2] = 1;
37 }
testPassingParentRegionArray(int x)38 int testPassingParentRegionArray(int x) {
39     double XYZ[3];
40     initArray(x, XYZ); //expected-note {{Calling 'initArray'}}
41                        //expected-note@-1 {{Returning from 'initArray'}}
42     return 1 * XYZ[1]; //expected-warning {{The right operand of '*' is a garbage value}}
43                        //expected-note@-1 {{The right operand of '*' is a garbage value}}
44 }
45 
46 double *getValidPtr(void);
47 struct WithFields {
48   double *f1;
49 };
initStruct(int x,struct WithFields * X)50 void initStruct(int x, struct WithFields *X) {
51   if (x <= 0) //expected-note {{Taking true branch}}
52               //expected-note@-1 {{Assuming 'x' is <= 0}}
53 
54     return; //expected-note{{Returning without writing to 'X->f1'}}
55   X->f1 = getValidPtr();
56 }
testPassingParentRegionStruct(int x)57 double testPassingParentRegionStruct(int x) {
58   struct WithFields st;
59   st.f1 = 0; // expected-note {{Null pointer value stored to 'st.f1'}}
60   initStruct(x, &st); //expected-note {{Calling 'initStruct'}}
61                       //expected-note@-1 {{Returning from 'initStruct'}}
62   return (*st.f1); //expected-warning {{Dereference of null pointer}}
63                    //expected-note@-1{{Dereference of null pointer (loaded from field 'f1')}}
64 }
65 
66