2021-08-11 17:19:19 +08:00
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.security.ReturnPtrRange -analyzer-output text -verify %s
2015-12-10 17:28:06 +08:00
int conjure_index ( ) ;
2021-08-11 17:19:19 +08:00
namespace test_element_index_lifetime {
int arr [ 10 ] ; // expected-note{{Original object declared here}} expected-note{{Original object declared here}}
int * ptr ;
int * test_global_ptr ( ) {
do { // expected-note{{Loop condition is false. Exiting loop}}
2015-12-10 17:28:06 +08:00
int x = conjure_index ( ) ;
2021-08-11 17:19:19 +08:00
ptr = arr + x ; // expected-note{{Value assigned to 'ptr'}}
if ( x ! = 20 ) // expected-note{{Assuming 'x' is equal to 20}}
// expected-note@-1{{Taking false branch}}
2015-12-10 17:28:06 +08:00
return arr ; // no-warning
2021-08-11 17:19:19 +08:00
} while ( 0 ) ;
return ptr ; // expected-warning{{Returned pointer value points outside the original object (potential buffer overflow) [alpha.security.ReturnPtrRange]}}
// expected-note@-1{{Returned pointer value points outside the original object (potential buffer overflow)}}
// expected-note@-2{{Original object 'arr' is an array of 10 'int' objects}}
2015-12-10 17:28:06 +08:00
}
2021-08-11 17:19:19 +08:00
int * test_local_ptr ( ) {
2015-12-10 17:28:06 +08:00
int * local_ptr ;
2021-08-11 17:19:19 +08:00
do { // expected-note{{Loop condition is false. Exiting loop}}
2015-12-10 17:28:06 +08:00
int x = conjure_index ( ) ;
2021-08-11 17:19:19 +08:00
local_ptr = arr + x ; // expected-note{{Value assigned to 'local_ptr'}}
if ( x ! = 20 ) // expected-note{{Assuming 'x' is equal to 20}}
// expected-note@-1{{Taking false branch}}
2015-12-10 17:28:06 +08:00
return arr ; // no-warning
2021-08-11 17:19:19 +08:00
} while ( 0 ) ;
return local_ptr ; // expected-warning{{Returned pointer value points outside the original object (potential buffer overflow) [alpha.security.ReturnPtrRange]}}
// expected-note@-1{{Returned pointer value points outside the original object (potential buffer overflow)}}
// expected-note@-2{{Original object 'arr' is an array of 10 'int' objects}}
}
2015-12-10 17:28:06 +08:00
}
2020-11-02 23:37:59 +08:00
template < typename T , int N >
T * end ( T ( & arr ) [ N ] ) {
return arr + N ; // no-warning, because we want to avoid false positives on returning the end() iterator of a container.
}
void get_end_of_array ( ) {
static int arr [ 10 ] ;
end ( arr ) ;
}
template < int N >
class Iterable {
int buffer [ N ] ;
int * start , * finish ;
public :
Iterable ( ) : start ( buffer ) , finish ( buffer + N ) { }
int * begin ( ) { return start ; }
int * end ( ) { return finish ; }
} ;
void use_iterable_object ( ) {
Iterable < 20 > iter ;
iter . end ( ) ;
}
template < int N >
class BadIterable {
2021-08-11 17:19:19 +08:00
int buffer [ N ] ; // expected-note{{Original object declared here}}
2020-11-02 23:37:59 +08:00
int * start , * finish ;
public :
2021-08-11 17:19:19 +08:00
BadIterable ( ) : start ( buffer ) , finish ( buffer + N ) { } // expected-note{{Value assigned to 'iter.finish'}}
2020-11-02 23:37:59 +08:00
int * begin ( ) { return start ; }
2021-08-11 17:19:19 +08:00
int * end ( ) { return finish + 1 ; } // expected-warning{{Returned pointer value points outside the original object}}
// expected-note@-1{{Returned pointer value points outside the original object}}
// expected-note@-2{{Original object 'buffer' is an array of 20 'int' objects, returned pointer points at index 21}}
2020-11-02 23:37:59 +08:00
} ;
void use_bad_iterable_object ( ) {
2021-08-11 17:19:19 +08:00
BadIterable < 20 > iter ; // expected-note{{Calling default constructor for 'BadIterable<20>'}}
// expected-note@-1{{Returning from default constructor for 'BadIterable<20>'}}
iter . end ( ) ; // expected-note{{Calling 'BadIterable::end'}}
2020-11-02 23:37:59 +08:00
}
2021-08-11 17:19:19 +08:00
int * test_idx_sym ( int I ) {
static int arr [ 10 ] ; // expected-note{{Original object declared here}} expected-note{{'arr' initialized here}}
if ( I ! = 11 ) // expected-note{{Assuming 'I' is equal to 11}}
// expected-note@-1{{Taking false branch}}
return arr ;
return arr + I ; // expected-warning{{Returned pointer value points outside the original object}}
// expected-note@-1{{Returned pointer value points outside the original object}}
// expected-note@-2{{Original object 'arr' is an array of 10 'int' objects, returned pointer points at index 11}}
}
namespace test_array_of_struct {
struct Data {
int A ;
char * B ;
} ;
Data DataArr [ 10 ] ; // expected-note{{Original object declared here}}
Data * test_struct_array ( ) {
int I = conjure_index ( ) ;
if ( I ! = 11 ) // expected-note{{Assuming 'I' is equal to 11}}
// expected-note@-1{{Taking false branch}}
return DataArr ;
return DataArr + I ; // expected-warning{{Returned pointer value points outside the original object}}
// expected-note@-1{{Returned pointer value points outside the original object}}
// expected-note@-2{{Original object 'DataArr' is an array of 10 'test_array_of_struct::Data' objects, returned pointer points at index 11}}
}
}