Skip to content

Safety hole: ephemeral iterator escaping a statement-temporary origin is not rejected (silent use-after-free) #725

Closed

Description

@ehartford

Spec reference

§5.2 (Ephemeral propagation, D2 / BDFL ruling 2026-07-04): an ephemeral value "may not escape its origin's scope … enforced by borrow-origin tracking, so … one that borrows a stack local it would outlive is rejected." §2.4: "Temporaries created within an expression are dropped at the end of the enclosing statement."

Minimal reproduction

fn vec_123() -> Vec[i32]:
    let xs: Vec[i32] = Vec.new()
    xs.push(1)
    xs.push(2)
    xs.push(3)
    xs

fn main:
    let pairs = vec_123().iter() |> zip(vec_123().iter())
    let (lefts, rights) = pairs.unzip()
    print(f"lefts=[{lefts.get(0)},{lefts.get(1)},{lefts.get(2)}]")

Expected

Compile error: pairs (ephemeral ZipIter holding VecIter raw data_ptr views) escapes the scope of its origins — the two statement temporaries, which die at the end of the let pairs statement.

Actual

Compiles silently; prints lefts=[1,0,0]. MIR shows the temp Vecs dropped (buffers freed) immediately after the let pairs statement; the unzip loop then reads through the stale data_ptr while its own lefts/rights allocations recycle the freed blocks — a use-after-free that happens to read zeros. An explicit next() loop over the same dangling iterator reads stale-but-intact memory and "works", which is the worse failure mode.

Provenance

Became reachable when #716 flipped iter to self: &Self (borrowing). Under the retired consuming iter, the temporary was consumed by the iterator and no early drop existed. The checker's borrow-origin tracking covers named locals (returning a view of a local is rejected) but not rvalue/statement-temporary origins bound via let.

Surfaced by spec_ss13_3_collection_operations test_unzip (fixture re-spelled to named locals; the miscompile is this issue). Related: #712, #716; seam class escape-view-consume in the seam-sites inventory.

Workaround

Bind the source container to a named local that outlives the iterator.

Activity

  1. ehartford commented on Sep 1, 2026

    @ehartford
    ContributorAuthor

    Fixed: check_let_binding scans an EPHEMERAL initializer for @[iter_of_self] calls on OWNED rvalue receivers (through pipelines, call args, casts, field bases) and rejects with 'ephemeral value borrows a temporary that dies at the end of this statement' + a bind-the-collection-first help. The filed repro (zip over two temp .iter()s, silently printing [1,0,0] through freed memory) and the direct form (let it = vec_123().iter()) both error now.

    The hole was maybe_register_iter_of_self_borrow bailing when the receiver has no place root — an rvalue receiver registered no borrow at all. Named receivers keep the existing origin tracking; view-typed rvalue receivers are excluded (they borrow something that outlives the statement); same-statement uses (for v in vec_123():) stay legal; compiler self-checks clean. Full :test green, evidence recorded.

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