Skip to content

Commit fb9a117

Browse files
committed
Fix an invalid memory access in run_program and friends
1 parent a0ab57b commit fb9a117

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/lib/run_program.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ fn arg_vec(str prog, vec[str] args) -> vec[sbuf] {
1414
}
1515

1616
fn run_program(str prog, vec[str] args) -> int {
17-
auto pid =
18-
rustrt::rust_run_program(vec::buf[sbuf](arg_vec(prog, args)), 0, 0,
19-
0);
17+
// Note: we have to hold on to this vector reference while we hold a
18+
// pointer to its buffer
19+
auto argv = arg_vec(prog, args);
20+
auto pid = rustrt::rust_run_program(vec::buf(argv), 0, 0, 0);
2021
ret os::waitpid(pid);
2122
}
2223

@@ -32,8 +33,11 @@ type program =
3233
fn start_program(str prog, vec[str] args) -> @program {
3334
auto pipe_input = os::pipe();
3435
auto pipe_output = os::pipe();
36+
// Note: we have to hold on to this vector reference while we hold a
37+
// pointer to its buffer
38+
auto argv = arg_vec(prog, args);
3539
auto pid =
36-
rustrt::rust_run_program(vec::buf[sbuf](arg_vec(prog, args)),
40+
rustrt::rust_run_program(vec::buf(argv),
3741
pipe_input._0, pipe_output._1, 0);
3842
if (pid == -1) { fail; }
3943
os::libc::close(pipe_input._0);

src/test/run-pass/lib-run.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// xfail-stage0
2+
3+
use std;
4+
import std::run;
5+
6+
// Regression test for memory leaks
7+
fn test_leaks() {
8+
run::run_program("echo", []);
9+
run::start_program("echo", []);
10+
run::program_output("echo", []);
11+
}
12+
13+
fn main() {
14+
test_leaks();
15+
}

0 commit comments

Comments
 (0)