Skip to content

Result[T]::unwrap() double-frees when T has a Drop impl (e.g. CString) #648

Closed

Description

@ehartford

Summary

Result[T, E].unwrap() double-frees when T owns a resource (has an impl Drop). str.to_cstring().unwrap() panics with invalid free: pointer is not an allocated payload start — the recovered CString is dropped twice (the moved-out value and the Result's copy).

Repro (verified on the seed and HEAD)

fn main:
    let c = "hi".to_cstring().unwrap()   // panic: invalid free
    print_i32(c.len() as i32)
  • match "hi".to_cstring(): .Ok(c) => ... works (2).
  • Direct CString { ptr, len } construct + drop works (1).
  • Vec (also Drop) round-trips fine, so it is specific to unwrap's move of the payload out of the Result — the Result's Ok payload is not marked consumed, so both drop.

Spec

§9.x Result / §2.5 ownership: unwrap consumes the Result and moves the payload out; the payload must be dropped exactly once.

Impact

Any Result[DropType]::unwrap() / likely expect() and possibly ? on a Result[DropType] double-frees. Surfaced building the #602 retains: owned-argument path (worked around with match).

Activity

  1. added a commit that references this issue on Jul 5, 2026
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