From 0e594939aa032c074df925d03c7d1289d6c2efc2 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Tue, 12 Jul 2011 11:21:14 -0700 Subject: [PATCH] Fix potential use-before-init bug in trans This was being masked by a bug in typestate (fixed in the next commit). --- src/comp/middle/trans.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index e732a5b83e3f..592757014316 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -7129,7 +7129,7 @@ fn trans_anon_obj(@block_ctxt bcx, &span sp, &ast::anon_obj anon_obj, methods = anon_obj.methods, dtor = none[@ast::method]); - let result with_obj_val; + let option::t[result] with_obj_val = none; let ty::t with_obj_ty; auto vtbl; alt (anon_obj.with_obj) { @@ -7155,7 +7155,7 @@ fn trans_anon_obj(@block_ctxt bcx, &span sp, &ast::anon_obj anon_obj, // If with_obj (the object being extended) exists, translate it. // Translating with_obj returns a ValueRef (pointer to a 2-word // value) wrapped in a result. - with_obj_val = trans_expr(bcx, e); + with_obj_val = some(trans_expr(bcx, e)); // TODO: What makes more sense to get the type of an expr -- // calling ty::expr_ty(ccx.tcx, e) on it or calling @@ -7332,9 +7332,13 @@ fn trans_anon_obj(@block_ctxt bcx, &span sp, &ast::anon_obj anon_obj, GEP_tup_like(bcx, body_ty, body.val, ~[0, abi::obj_body_elt_with_obj]); bcx = body_with_obj.bcx; - bcx = copy_val(bcx, INIT, body_with_obj.val, - with_obj_val.val, - with_obj_ty).bcx; + alt (with_obj_val) { + case (some(?v)) { + bcx = copy_val(bcx, INIT, body_with_obj.val, + v.val, with_obj_ty).bcx; + } + case (_) {} + } // Store box ptr in outer pair. auto p = bcx.build.PointerCast(box.val, llbox_ty);