Skip to content

Commit 587b863

Browse files
committed
Zero locals that have initializers that might fail
This will avoid running cleanups on uninitialized memory Issue #236
1 parent 4eb3ce3 commit 587b863

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/comp/middle/trans.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4447,7 +4447,9 @@ fn init_local(bcx: @block_ctxt, local: &@ast::local) -> result {
44474447
// Make a note to drop this slot on the way out.
44484448
add_clean(bcx, llptr, ty);
44494449

4450-
if must_zero(local) { bcx = zero_alloca(bcx, llptr, ty).bcx; }
4450+
if must_zero(bcx_ccx(bcx), local) {
4451+
bcx = zero_alloca(bcx, llptr, ty).bcx;
4452+
}
44514453

44524454
alt local.node.init {
44534455
some(init) {
@@ -4473,36 +4475,37 @@ fn init_local(bcx: @block_ctxt, local: &@ast::local) -> result {
44734475
bcx.fcx.lllocals, false);
44744476
ret rslt(bcx, llptr);
44754477

4476-
fn must_zero(local: &@ast::local) -> bool {
4478+
fn must_zero(ccx: &@crate_ctxt, local: &@ast::local) -> bool {
44774479
alt local.node.init {
4478-
some(init) { might_not_init(init.expr) }
4480+
some(init) { might_not_init(ccx, init.expr) }
44794481
none. { true }
44804482
}
44814483
}
44824484

4483-
fn might_not_init(expr: &@ast::expr) -> bool {
4484-
type env = @mutable bool;
4485-
let e = @mutable false;
4486-
// FIXME: Probably also need to account for expressions that
4487-
// fail but since we don't unwind yet, it doesn't seem to be a
4488-
// problem
4485+
fn might_not_init(ccx: &@crate_ctxt, expr: &@ast::expr) -> bool {
4486+
type env = {mutable mightnt: bool,
4487+
ccx: @crate_ctxt};
4488+
let e = {mutable mightnt: false,
4489+
ccx: ccx};
4490+
fn visit_expr(ex: &@ast::expr, e: &env, v: &vt<env>) {
4491+
let might_not_init = alt ex.node {
4492+
ast::expr_ret(_) { true }
4493+
ast::expr_break. { true }
4494+
ast::expr_cont. { true }
4495+
_ {
4496+
let ex_ty = ty::expr_ty(e.ccx.tcx, ex);
4497+
ty::type_is_bot(e.ccx.tcx, ex_ty)
4498+
}
4499+
};
4500+
if might_not_init {
4501+
e.mightnt = true;
4502+
} else { visit::visit_expr(ex, e, v); }
4503+
}
44894504
let visitor =
4490-
visit::mk_vt(
4491-
@{visit_expr:
4492-
fn (ex: &@ast::expr, e: &env, v: &vt<env>) {
4493-
let might_not_init =
4494-
alt ex.node {
4495-
ast::expr_ret(_) { true }
4496-
ast::expr_break. { true }
4497-
ast::expr_cont. { true }
4498-
_ { false }
4499-
};
4500-
if might_not_init {
4501-
*e = true;
4502-
} else { visit::visit_expr(ex, e, v); }
4503-
} with *visit::default_visitor()});
4505+
visit::mk_vt(@{visit_expr: visit_expr
4506+
with *visit::default_visitor()});
45044507
visitor.visit_expr(expr, e, visitor);
4505-
ret *e;
4508+
ret e.mightnt;
45064509
}
45074510
}
45084511

0 commit comments

Comments
 (0)