File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
rust/ql/test/query-tests/security/CWE-825 Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -403,3 +403,29 @@ pub fn test_vec_reserve() {
403403 println ! ( " v4 = {}" , v4) ; // corrupt in practice
404404 }
405405}
406+
407+ // --- pointer to reference ---
408+
409+ pub fn test_pointer_converted_to_reference ( ) {
410+ let layout = std:: alloc:: Layout :: new :: < u128 > ( ) ;
411+ let m3;
412+
413+ // allocate
414+ unsafe {
415+ let m1 = std:: alloc:: alloc ( layout) ; // *mut u8
416+ let m2 = m1 as * mut u128 ; // *mut u128
417+ m3 = & mut * m2; // &u128
418+ }
419+
420+ * m3 = 1 ; // GOOD
421+ println ! ( " v1 = {}" , * m3) ; // GOOD
422+
423+ // free
424+ unsafe {
425+ std:: alloc:: dealloc ( ( & raw mut * m3) as * mut u8 , layout) ; // $ MISSING: Source[rust/access-invalid-pointer]=dealloc
426+ }
427+ // (m1, m2, m3 are now dangling)
428+
429+ // (this is corrupt in practice)
430+ println ! ( " v2 = {} (!)" , * m3) ; // $ MISSING: Alert[rust/access-invalid-pointer]=dealloc
431+ }
Original file line number Diff line number Diff line change @@ -143,6 +143,9 @@ fn main() {
143143 println ! ( "test_vec_reserve:" ) ;
144144 test_vec_reserve ( ) ;
145145
146+ println ! ( "test_pointer_converted_to_reference:" ) ;
147+ test_pointer_converted_to_reference ( ) ;
148+
146149 // ---
147150
148151 println ! ( "test_local_dangling:" ) ;
You can’t perform that action at this time.
0 commit comments