Skip to content

validate-ownership accepts a reset-on-move blank of a place that is still Init (a lost value) #1487

Closed

Description

@ehartford

--validate-ownership accepts a reset-on-move blank (_N = const zst) of a place whose drop state is Init — a value no path moved is overwritten without a drop, so it is lost (leak) and every later read sees the blank.

Found on #1383's branch: with Sema's let-else rejection fixed, MIR for

fn vec_or_err(k: i32) -> Result[Vec[str], str]:
    if k == 0: return Err("none".clone())
    var v: Vec[str] = Vec.new()
    v.push("a".clone())
    v
fn consume(s: str) -> i32: s.len() as i32
fn h(k: i32) -> i32:
    let keep = "keep".clone()
    let Ok(v) = vec_or_err(k) else: return consume(keep) - 100
    keep.len() as i32 + v.len() as i32

blanked keep on the matching path (the else branch's pending reset leaked into the let-else statement's flush; fixed in the #1383 PR). --trace-ownership h:_2 showed the fact:

bb4.term  event=move   before=_2=Init after=_2=Moved  call ...(move _2)
bb5.stmt20 event=assign before=_2=Init after=_2=Init  "_2 = const zst(ty16);"

yet --validate-ownership printed ok; the program printed -96 1 (not -96 5) and leaked keep's buffer.

Expected: the ownership validator rejects an Assign of a zero-sized/blank constant into a drop-bearing place (local or projected field) whose state before the statement is Init: "reset of _2 on a path where it was never moved; the value is lost". Moved, Maybe, Uninit and MaybeGarbage stay legal (a reset after a move, a conditional-move join, a zero-init).

Needs a sweep of the corpora with the new check before it lands (false-positive risk: any other lowering that writes a blank over a live value is itself this bug, but that has to be established case by case).

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