Skip to content

Option.filter passes the payload BY VALUE to the predicate and then keeps it — double free #1379

Closed

Description

@ehartford

Spec: §10 Option table — filter | (fn(&T) -> bool) -> Option[T]. The predicate observes; it must receive &T.

fn main:
    var xs: Vec[i32] = Vec.new()
    xs.push(1)
    let o: Option[Vec[i32]] = Some(xs)
    let m = o.filter((v) => v.len() > 0)
    print(m.is_some())

with run --debug-alloc: prints true, then debug-alloc: DOUBLE FREE addr=... size=32 origin=unknown first_drop=<untagged> second_drop=drop#enum __drop_enum_448, rc=134.

Cause (src/MirLower.w lower_option_combinator_method, the filter branch): filter_args.push(self.operand_for_place(payload_place, payload_ty)) moves the payload into the predicate call (the comment there says Sema types the predicate parameter OWNED), and kept_fields.push(self.operand_for_place(payload_place, payload_ty)) moves the same payload again into the result. The predicate frees it on return; the result owns freed memory.

Expected: the predicate parameter is &T (Sema and MIR), the payload moves once, into the kept Some.

Found while building the #1363 matrix (same class: a payload moved out of a carrier twice / after its owner frees it).

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