2019-03-30 06:57:49 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -w -x c -analyzer-checker=core -analyzer-output=text\
|
|
|
|
// RUN: -verify %s
|
2018-02-24 07:26:56 +08:00
|
|
|
|
|
|
|
typedef __typeof(sizeof(int)) size_t;
|
|
|
|
void *memset(void *__s, int __c, size_t __n);
|
|
|
|
|
|
|
|
int initializer1(int *p, int x) {
|
[analyzer] ConditionBRVisitor: Enhance to write out more information
Summary:
Add extra messages to the bug report to inform the user why the analyzer
`Taking true/false branch`.
Reviewers: NoQ, george.karpenkov
Reviewed By: NoQ
Subscribers: gerazo, gsd, dkrupp, whisperity, baloghadamsoftware, xazax.hun,
eraman, szepet, a.sidorin, mikhail.ramalho, Szelethus,
donat.nagy, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D53076
llvm-svn: 362020
2019-05-30 04:06:09 +08:00
|
|
|
if (x) { // expected-note{{'x' is 0}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
2018-02-24 07:26:56 +08:00
|
|
|
*p = 1;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1; // expected-note {{Returning without writing to '*p'}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int param_not_initialized_by_func(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int p; // expected-note {{'p' declared without an initial value}}
|
|
|
|
int out = initializer1(&p, 0); // expected-note{{Calling 'initializer1'}}
|
|
|
|
// expected-note@-1{{Returning from 'initializer1'}}
|
|
|
|
return p; // expected-note{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-warning@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int param_initialized_properly(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int p;
|
|
|
|
int out = initializer1(&p, 1);
|
|
|
|
return p; //no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
static int global;
|
|
|
|
|
|
|
|
int initializer2(int **p, int x) {
|
[analyzer] ConditionBRVisitor: Enhance to write out more information
Summary:
Add extra messages to the bug report to inform the user why the analyzer
`Taking true/false branch`.
Reviewers: NoQ, george.karpenkov
Reviewed By: NoQ
Subscribers: gerazo, gsd, dkrupp, whisperity, baloghadamsoftware, xazax.hun,
eraman, szepet, a.sidorin, mikhail.ramalho, Szelethus,
donat.nagy, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D53076
llvm-svn: 362020
2019-05-30 04:06:09 +08:00
|
|
|
if (x) { // expected-note{{'x' is 0}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
2018-02-24 07:26:56 +08:00
|
|
|
*p = &global;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1; // expected-note {{Returning without writing to '*p'}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int param_not_written_into_by_func(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int *p = 0; // expected-note{{'p' initialized to a null pointer value}}
|
|
|
|
int out = initializer2(&p, 0); // expected-note{{Calling 'initializer2'}}
|
|
|
|
// expected-note@-1{{Returning from 'initializer2'}}
|
|
|
|
return *p; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
|
|
|
|
// expected-note@-1{{Dereference of null pointer (loaded from variable 'p')}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initializer3(int *p, int param) {
|
[analyzer] ConditionBRVisitor: Enhance to write out more information
Summary:
Add extra messages to the bug report to inform the user why the analyzer
`Taking true/false branch`.
Reviewers: NoQ, george.karpenkov
Reviewed By: NoQ
Subscribers: gerazo, gsd, dkrupp, whisperity, baloghadamsoftware, xazax.hun,
eraman, szepet, a.sidorin, mikhail.ramalho, Szelethus,
donat.nagy, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D53076
llvm-svn: 362020
2019-05-30 04:06:09 +08:00
|
|
|
if (param) // expected-note{{'param' is 0}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
2018-02-24 07:26:56 +08:00
|
|
|
*p = 0;
|
|
|
|
} // expected-note{{Returning without writing to '*p'}}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int param_written_into_by_void_func(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int p; // expected-note{{'p' declared without an initial value}}
|
|
|
|
initializer3(&p, 0); // expected-note{{Calling 'initializer3'}}
|
|
|
|
// expected-note@-1{{Returning from 'initializer3'}}
|
|
|
|
return p; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void initializer4(int *p, int param) {
|
[analyzer] ConditionBRVisitor: Enhance to write out more information
Summary:
Add extra messages to the bug report to inform the user why the analyzer
`Taking true/false branch`.
Reviewers: NoQ, george.karpenkov
Reviewed By: NoQ
Subscribers: gerazo, gsd, dkrupp, whisperity, baloghadamsoftware, xazax.hun,
eraman, szepet, a.sidorin, mikhail.ramalho, Szelethus,
donat.nagy, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D53076
llvm-svn: 362020
2019-05-30 04:06:09 +08:00
|
|
|
if (param) // expected-note{{'param' is 0}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
2018-02-24 07:26:56 +08:00
|
|
|
*p = 0;
|
|
|
|
} // expected-note{{Returning without writing to '*p'}}
|
|
|
|
|
|
|
|
void initializer5(int *p, int param) {
|
[analyzer] ConditionBRVisitor: Enhance to write out more information
Summary:
Add extra messages to the bug report to inform the user why the analyzer
`Taking true/false branch`.
Reviewers: NoQ, george.karpenkov
Reviewed By: NoQ
Subscribers: gerazo, gsd, dkrupp, whisperity, baloghadamsoftware, xazax.hun,
eraman, szepet, a.sidorin, mikhail.ramalho, Szelethus,
donat.nagy, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D53076
llvm-svn: 362020
2019-05-30 04:06:09 +08:00
|
|
|
if (!param) // expected-note{{'param' is 1}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
2018-02-24 07:26:56 +08:00
|
|
|
*p = 0;
|
|
|
|
} // expected-note{{Returning without writing to '*p'}}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int multi_init_tries_func(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int p; // expected-note{{'p' declared without an initial value}}
|
|
|
|
initializer4(&p, 0); // expected-note{{Calling 'initializer4'}}
|
|
|
|
// expected-note@-1{{Returning from 'initializer4'}}
|
|
|
|
initializer5(&p, 1); // expected-note{{Calling 'initializer5'}}
|
|
|
|
// expected-note@-1{{Returning from 'initializer5'}}
|
|
|
|
return p; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
int initializer6(const int *p) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int no_msg_on_const(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int p; // expected-note{{'p' declared without an initial value}}
|
|
|
|
initializer6(&p);
|
|
|
|
return p; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int x;
|
|
|
|
} S;
|
|
|
|
|
|
|
|
int initializer7(S *s, int param) {
|
[analyzer] ConditionBRVisitor: Enhance to write out more information
Summary:
Add extra messages to the bug report to inform the user why the analyzer
`Taking true/false branch`.
Reviewers: NoQ, george.karpenkov
Reviewed By: NoQ
Subscribers: gerazo, gsd, dkrupp, whisperity, baloghadamsoftware, xazax.hun,
eraman, szepet, a.sidorin, mikhail.ramalho, Szelethus,
donat.nagy, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D53076
llvm-svn: 362020
2019-05-30 04:06:09 +08:00
|
|
|
if (param) { // expected-note{{'param' is 0}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
2018-02-24 07:26:56 +08:00
|
|
|
s->x = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1; // expected-note{{Returning without writing to 's->x'}}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int initialize_struct_field(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
S local;
|
|
|
|
initializer7(&local, 0); // expected-note{{Calling 'initializer7'}}
|
|
|
|
// expected-note@-1{{Returning from 'initializer7'}}
|
|
|
|
return local.x; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nullwriter(int **p) {
|
|
|
|
*p = 0; // expected-note{{Null pointer value stored to 'p'}}
|
|
|
|
} // no extra note
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int usage(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int x = 0;
|
|
|
|
int *p = &x;
|
|
|
|
nullwriter(&p); // expected-note{{Calling 'nullwriter'}}
|
|
|
|
// expected-note@-1{{Returning from 'nullwriter'}}
|
|
|
|
return *p; // expected-warning{{Dereference of null pointer (loaded from variable 'p')}}
|
|
|
|
// expected-note@-1{{Dereference of null pointer (loaded from variable 'p')}}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
} A;
|
|
|
|
|
|
|
|
void partial_initializer(A *a) {
|
|
|
|
a->x = 0;
|
|
|
|
} // expected-note{{Returning without writing to 'a->y'}}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int use_partial_initializer(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
A a;
|
|
|
|
partial_initializer(&a); // expected-note{{Calling 'partial_initializer'}}
|
|
|
|
// expected-note@-1{{Returning from 'partial_initializer'}}
|
|
|
|
return a.y; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
} B;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
B b;
|
|
|
|
} C;
|
|
|
|
|
|
|
|
void partial_nested_initializer(C *c) {
|
|
|
|
c->b.x = 0;
|
|
|
|
} // expected-note{{Returning without writing to 'c->b.y'}}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int use_partial_nested_initializer(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
B localB;
|
|
|
|
C localC;
|
|
|
|
localC.b = localB;
|
|
|
|
partial_nested_initializer(&localC); // expected-note{{Calling 'partial_nested_initializer'}}
|
|
|
|
// expected-note@-1{{Returning from 'partial_nested_initializer'}}
|
|
|
|
return localC.b.y; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_subregion_assignment(C* c) {
|
|
|
|
B b;
|
|
|
|
c->b = b;
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int use_subregion_assignment(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
C c;
|
|
|
|
test_subregion_assignment(&c); // expected-note{{Calling 'test_subregion_assignment'}}
|
|
|
|
// expected-note@-1{{Returning from 'test_subregion_assignment'}}
|
|
|
|
return c.b.x; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
int confusing_signature(int *);
|
|
|
|
int confusing_signature(int *p) {
|
|
|
|
return 0; // expected-note{{Returning without writing to '*p'}}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int use_confusing_signature(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int a; // expected-note {{'a' declared without an initial value}}
|
|
|
|
confusing_signature(&a); // expected-note{{Calling 'confusing_signature'}}
|
|
|
|
// expected-note@-1{{Returning from 'confusing_signature'}}
|
|
|
|
return a; // expected-note{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-warning@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int coin(void);
|
2018-02-24 07:26:56 +08:00
|
|
|
|
|
|
|
int multiindirection(int **p) {
|
|
|
|
if (coin()) // expected-note{{Assuming the condition is true}}
|
|
|
|
// expected-note@-1{{Taking true branch}}
|
|
|
|
return 1; // expected-note{{Returning without writing to '**p'}}
|
|
|
|
*(*p) = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int usemultiindirection(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
int a; // expected-note {{'a' declared without an initial value}}
|
|
|
|
int *b = &a;
|
|
|
|
multiindirection(&b); // expected-note{{Calling 'multiindirection'}}
|
|
|
|
// expected-note@-1{{Returning from 'multiindirection'}}
|
|
|
|
return a; // expected-note{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-warning@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
|
|
|
|
|
|
|
int indirectingstruct(S** s) {
|
|
|
|
if (coin()) // expected-note{{Assuming the condition is true}}
|
|
|
|
// expected-note@-1{{Taking true branch}}
|
|
|
|
return 1; // expected-note{{Returning without writing to '(*s)->x'}}
|
|
|
|
|
|
|
|
(*s)->x = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int useindirectingstruct(void) {
|
2018-02-24 07:26:56 +08:00
|
|
|
S s;
|
|
|
|
S* p = &s;
|
|
|
|
indirectingstruct(&p); //expected-note{{Calling 'indirectingstruct'}}
|
|
|
|
//expected-note@-1{{Returning from 'indirectingstruct'}}
|
|
|
|
return s.x; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
2018-08-02 10:02:40 +08:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int *x;
|
|
|
|
} D;
|
|
|
|
|
|
|
|
void initializeMaybeInStruct(D* pD) {
|
|
|
|
if (coin()) // expected-note{{Assuming the condition is false}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
|
|
|
*pD->x = 120;
|
|
|
|
} // expected-note{{Returning without writing to 'pD->x'}}
|
|
|
|
|
2022-02-12 20:23:43 +08:00
|
|
|
int useInitializeMaybeInStruct(void) {
|
2018-08-02 10:02:40 +08:00
|
|
|
int z; // expected-note{{'z' declared without an initial value}}
|
|
|
|
D d;
|
|
|
|
d.x = &z;
|
|
|
|
initializeMaybeInStruct(&d); // expected-note{{Calling 'initializeMaybeInStruct'}}
|
|
|
|
// expected-note@-1{{Returning from 'initializeMaybeInStruct'}}
|
|
|
|
return z; // expected-warning{{Undefined or garbage value returned to caller}}
|
|
|
|
// expected-note@-1{{Undefined or garbage value returned to caller}}
|
|
|
|
}
|
2019-03-30 06:57:49 +08:00
|
|
|
|
|
|
|
void test_implicit_function_decl(int *x) {
|
|
|
|
if (x) {} // expected-note{{Assuming 'x' is null}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
|
|
|
implicit_function(x);
|
|
|
|
*x = 4; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
|
|
|
|
// expected-note@-1{{Dereference of null pointer (loaded from variable 'x')}}
|
|
|
|
}
|
|
|
|
int implicit_function(int *y) {}
|