Skip to content

Soundness: reassignment after move double-drops the moved-out Drop value #614

Closed

Description

@ehartford

Summary

A reassignment of a var whose previous value was moved out still runs the
drop-before-overwrite on that (already moved) value — a double-drop / double-free
for a Drop type.

Repro

type R { id: i32, slot: *mut i32 }
impl Drop for R:
    fn drop(move self: Self):
        unsafe: *self.slot = *self.slot + 1
fn take(r: R): ()
fn make(slot: *mut i32) -> R: R { id: 1, slot }

fn main:
    var drops = 0
    var r = make(&raw mut drops)
    take(r)                       // moves r1 into take; take drops r1 (drops=1)
    r = make(&raw mut drops)      // BUG: drop-before-overwrite drops r1 AGAIN (drops=2)
    take(r)                       // drops r2 (drops=3)
    print_i32(drops)             // prints 3; correct is 2

print_i32(drops) prints 3; the correct count is 2. The reassignment
r = make() drops r1 even though r1 was already consumed by take(r).

Root cause (direction)

MIR drop-before-overwrite for an assignment target does not consult whether the
target's current value was moved out; it emits the drop unconditionally. The
straight-line move ledger (M4) should mark r moved after take(r) so the
reassignment skips the drop (then re-marks it live with the new value).

Spec

§2.2 (after a move the source is invalid), §2.4 (drop on reassignment — drops the
old value, but only if it is still owned). req 1.1.1.14 ("double-free … caught at
compile time. Always.").

Discovery

Surfaced while implementing loop conditional moves (#613): the §2.4 reinit-in-loop
pattern (while …: take(r); r = make()) over-counts drops by one per iteration.
The bug is independent of loops (straight-line repro above). Related to the
move-is-a-copy substrate #605/#606/#607.

Activity

  1. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    Closing as fixed — reassignment after a move no longer double-drops: reset-on-move (spec §2.5.1) blanks the moved-out source, drop-before-overwrite on the blanked value is a no-op, and the reassigned value drops normally. Record: docs/completed/drop-elaboration-soundness.md; pinned by da_reassign_after_move (leak count=0, exact counts) and the loop-reinit fixtures. The static drop-elaboration approach this issue originally proposed was deliberately retired (§2.5.2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions