Skip to content

?? on a Result/Option with an owned payload double-frees: the subject temporary's enum drop frees the payload after it moved out #1042

Closed

Description

@ehartford

Found 2026-09-04 when #953 (read_file -> Result[str, IoError]) landed and std.build's generated gunzip helper (let input = read_file(p) ?? "" then bytes_from_str(input)) died in the build-w-toolfs-archive selfhost case with invalid free: pointer is not an allocated payload start (under WITH_DEBUG_ALLOC: DOUBLE FREE … origin=Vec first_drop=drop#struct __drop_struct_16 second_drop=drop#struct __drop_struct_78).

Route that found it (docs/deep-debugging-tools.md): allocator verdict → the lane's own compiled helper → WITH_DEBUG_ALLOC_TRAP_FREE=<addr> under lldb (ASLR off): alloc#1 (file content) → freed by drop#enum __drop_enum_443 (the Result temporary) → realloc as a Vec buffer → freed by the str drop inside bytes_from_str (dangling input) → freed again by the Vec. Eight-line repro:

fn bytes_from_str(data: str) -> Vec[u8]: …
fn main -> i32:
    let argv = args()
    let input = read_file(argv.get(1)) ?? ""
    if input.len() == 0: return 1
    let input_bytes = bytes_from_str(input)
    …

Cause (src/MirLower.w): lower_question_mark consumes its materialized scrutinee after moving the payload out (#605/#606); lower_double_question moved the payload out on the success path and left the subject to its scope-exit drop, so the variant-aware enum drop freed the moved-out str.

Fix: on the default path drop the subject explicitly (its Err payload, once); after the join consume it for the scope exactly as ? does. Pin: test/debug_alloc/da_coalesce_result_payload_owned.w. Lands with the #953 battery.

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