Skip to content

Commit c618eeb

Browse files
authored
Merge pull request #653 from immunant/kkysen/analysis-test-fix-leaked-args: Fix leaking args in analysis/test's main
This fixes the memory leakage of `args` in `main` in `analysis/test` by not `.into_raw()`ing the `CString`s, but storing them in a `Vec` and using `.as_ptr()`. Checked by `miri` (in #650) to remove the leaks created in `main`.
2 parents 236c494 + 56c0664 commit c618eeb

File tree

2 files changed

+25
-73
lines changed

2 files changed

+25
-73
lines changed

analysis/test/src/pointers.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -540,16 +540,13 @@ unsafe fn main_0(mut argc: libc::c_int, mut argv: *mut *mut libc::c_char) -> lib
540540
return 0i32;
541541
}
542542
pub fn main() {
543-
let mut args: Vec<*mut libc::c_char> = Vec::new();
544-
for arg in ::std::env::args() {
545-
println!("{:?}", arg);
546-
args.push(
547-
::std::ffi::CString::new(arg)
548-
.expect("Failed to convert argument into CString.")
549-
.into_raw(),
550-
);
551-
}
552-
args.push(::std::ptr::null_mut());
543+
let args = ::std::env::args()
544+
.map(|arg| ::std::ffi::CString::new(arg).expect("Failed to convert argument into CString."))
545+
.collect::<Vec<_>>();
546+
let mut args = args.iter()
547+
.map(|arg| arg.as_ptr() as *mut libc::c_char)
548+
.chain(::std::iter::once(::std::ptr::null_mut()))
549+
.collect::<Vec<_>>();
553550
unsafe {
554551
main_0(
555552
(args.len() - 1) as libc::c_int,

pdg/src/snapshots/c2rust_pdg__tests__analysis_test_pdg_snapshot_debug.snap

+18-63
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,42 @@ source: pdg/src/main.rs
33
expression: pdg
44
---
55
g {
6-
n[0]: &_5 _ => _10 @ bb5[4]: fn main; _10 = &mut _5;
7-
n[1]: copy n[0] => _9 @ bb5[5]: fn main; _9 = &mut (*_10);
8-
n[2]: copy n[0] => _9 @ bb5[5]: fn main; _9 = &mut (*_10);
6+
n[0]: &_1 _ => _11 @ bb3[9]: fn main; _11 = &_1;
7+
n[1]: copy n[0] => _1 @ bb0[0]: fn deref; _10 = deref(move _11);
98
}
109
nodes_that_need_write = []
1110

1211
g {
13-
n[0]: &_20 _ => _19 @ bb8[10]: fn main; _19 = &_20;
14-
n[1]: copy n[0] => _18 @ bb8[11]: fn main; _18 = &(*_19);
15-
n[2]: copy n[1] => _17 @ bb8[12]: fn main; _17 = move _18 as &[&str] (Pointer(Unsize));
16-
n[3]: copy n[2] => _1 @ bb0[0]: fn new_v1; _16 = new_v1(move _17, move _21);
12+
n[0]: copy _ => _10 @ bb3[10]: fn main; _10 = deref(move _11);
13+
n[1]: copy n[0] => _9 @ bb4[0]: fn main; _9 = &(*_10);
14+
n[2]: copy n[1] => _1 @ bb0[0]: fn iter; _8 = iter(move _9);
1715
}
1816
nodes_that_need_write = []
1917

2018
g {
21-
n[0]: &_13 _ => _27 @ bb8[21]: fn main; _27 = &_13;
22-
n[1]: copy n[0] => _26 @ bb8[22]: fn main; _26 = &(*_27);
23-
n[2]: copy n[1] => _1 @ bb0[0]: fn new_debug; _25 = new_debug(move _26);
19+
n[0]: copy _ => _14 @ bb6[4]: fn main; _14 = null_mut();
20+
n[1]: copy n[0] => _1 @ bb0[0]: fn once; _13 = once(move _14);
21+
n[2]: value.store _ => _20.* @ bb4[7]: fn invalid; (*_20) = const 0_usize as *mut pointers::S (PointerFromExposedAddress);
22+
n[3]: value.store _ => _17.* @ bb8[4]: fn fdevent_unregister; (*_17) = const 0_usize as *mut pointers::fdnode_st (PointerFromExposedAddress);
23+
n[4]: int_to_ptr _ => _2 @ bb0[2]: fn test_ref_field; _2 = const 0_usize as *const pointers::S (PointerFromExposedAddress);
24+
n[5]: int_to_ptr _ => _5 @ bb0[8]: fn test_ref_field; _5 = const 0_usize as *const pointers::S (PointerFromExposedAddress);
2425
}
2526
nodes_that_need_write = []
2627

2728
g {
28-
n[0]: &_24 _ => _23 @ bb13[3]: fn main; _23 = &_24;
29-
n[1]: copy n[0] => _22 @ bb13[4]: fn main; _22 = &(*_23);
30-
n[2]: copy n[1] => _21 @ bb13[5]: fn main; _21 = move _22 as &[std::fmt::ArgumentV1] (Pointer(Unsize));
31-
n[3]: copy n[2] => _2 @ bb0[0]: fn new_v1; _16 = new_v1(move _17, move _21);
29+
n[0]: &_5 _ => _19 @ bb10[8]: fn main; _19 = &_5;
30+
n[1]: copy n[0] => _1 @ bb0[0]: fn len; _18 = len(move _19);
3231
}
3332
nodes_that_need_write = []
3433

3534
g {
36-
n[0]: &_1 _ => _29 @ bb15[11]: fn main; _29 = &mut _1;
35+
n[0]: &_5 _ => _22 @ bb12[6]: fn main; _22 = &mut _5;
3736
}
3837
nodes_that_need_write = []
3938

4039
g {
41-
n[0]: copy _ => _35 @ bb16[3]: fn main; _35 = const "Failed to convert argument into CString.";
42-
n[1]: copy n[0] => _34 @ bb16[4]: fn main; _34 = &(*_35);
43-
n[2]: copy n[1] => _2 @ bb0[0]: fn expect; _31 = expect(move _32, move _34);
44-
}
45-
nodes_that_need_write = []
46-
47-
g {
48-
n[0]: copy _ => _30 @ bb17[2]: fn main; _30 = into_raw(move _31);
49-
n[1]: copy n[0] => _2 @ bb0[0]: fn push; _28 = push(move _29, move _30);
50-
}
51-
nodes_that_need_write = []
52-
53-
g {
54-
n[0]: &_5 _ => _10 @ bb5[4]: fn main; _10 = &mut _5;
55-
}
56-
nodes_that_need_write = []
57-
58-
g {
59-
n[0]: &_1 _ => _37 @ bb27[4]: fn main; _37 = &mut _1;
60-
}
61-
nodes_that_need_write = []
62-
63-
g {
64-
n[0]: copy _ => _38 @ bb27[6]: fn main; _38 = null_mut();
65-
n[1]: copy n[0] => _2 @ bb0[0]: fn push; _36 = push(move _37, move _38);
66-
n[2]: value.store _ => _20.* @ bb4[7]: fn invalid; (*_20) = const 0_usize as *mut pointers::S (PointerFromExposedAddress);
67-
n[3]: value.store _ => _17.* @ bb8[4]: fn fdevent_unregister; (*_17) = const 0_usize as *mut pointers::fdnode_st (PointerFromExposedAddress);
68-
n[4]: int_to_ptr _ => _2 @ bb0[2]: fn test_ref_field; _2 = const 0_usize as *const pointers::S (PointerFromExposedAddress);
69-
n[5]: int_to_ptr _ => _5 @ bb0[8]: fn test_ref_field; _5 = const 0_usize as *const pointers::S (PointerFromExposedAddress);
70-
}
71-
nodes_that_need_write = []
72-
73-
g {
74-
n[0]: &_1 _ => _43 @ bb29[8]: fn main; _43 = &_1;
75-
n[1]: copy n[0] => _1 @ bb0[0]: fn len; _42 = len(move _43);
76-
}
77-
nodes_that_need_write = []
78-
79-
g {
80-
n[0]: &_1 _ => _46 @ bb31[6]: fn main; _46 = &mut _1;
81-
}
82-
nodes_that_need_write = []
83-
84-
g {
85-
n[0]: copy _ => _45 @ bb31[7]: fn main; _45 = as_mut_ptr(move _46);
86-
n[1]: copy n[0] => _2 @ bb0[0]: fn main_0; _39 = main_0(move _40, move _45);
40+
n[0]: copy _ => _21 @ bb12[7]: fn main; _21 = as_mut_ptr(move _22);
41+
n[1]: copy n[0] => _2 @ bb0[0]: fn main_0; _15 = main_0(move _16, move _21);
8742
}
8843
nodes_that_need_write = []
8944

@@ -972,6 +927,6 @@ g {
972927
}
973928
nodes_that_need_write = [6, 5, 4, 0]
974929

975-
num_graphs = 71
976-
num_nodes = 686
930+
num_graphs = 64
931+
num_nodes = 669
977932

0 commit comments

Comments
 (0)