Skip to content

Moving a Vec out of a global and reassigning it double-frees: safe code, no diagnostic #1242

Closed

Description

@ehartford

Silent unsoundness in safe code. Sema accepts the program; the debug allocator reports a double free.

var g_items: Vec[str] = Vec.new()

fn note(s: &str): g_items.push(s.clone())

fn take -> Vec[str]:
    let taken = g_items      // moves out of a global?
    g_items = Vec.new()      // reassigns it
    taken

fn main:
    note("a")
    note("b")
    let got = take()
    print(f"{got.len()} {g_items.len()}")
    note("c")
    let again = take()
    print(f"{again.len()} {g_items.len()}")
$ with run --debug-alloc gmove.w
2 0
1 0
debug-alloc: DOUBLE FREE addr=… size=128 origin=Vec first_drop=drop#struct __drop_struct_193 second_drop=drop#struct __drop_struct_193

let taken = g_items byte-copies the Vec header out of the global instead of moving it (or being rejected): the global keeps the old buffer, the reassignment drops it, and taken drops it again. §2.3: transport is not duplication; a non-Copy value never gets a second live owner.

Expected: either reject moving out of a global ("a global always holds a value; clone it, or swap in a replacement") or lower it as a true move that leaves the global uninitialized until the reassignment, with the flow check that it is reassigned before any read. The first is the conservative reading.

Found writing ci_print_take_unknowns in src/CiPrint.w (the drain now copies out and clears). Ownership analysis: fix alone in its batch with the drop audit.

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