Skip to content

Accessors of unions return dangling pointers for reinterpreted unions #311

Description

@lucic71

In refcount, when the union is reinterpreted, saving the pointer returned by a union accessor is resulting in a dangling pointer. For example:

/*
union U {
  int i;
  float f;
};
*/
pub struct U {
    __bytes: Value<Box<[u8]>>,
}

impl Number {
    pub fn i(&self) -> Ptr<i32> {
        (self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
    }
    pub fn f(&self) -> Ptr<f32> {
        (self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
    }
}

fn foo() {
  let p: Ptr<...> = ...;
  let reinterpreted_u: Ptr<U> = p.reinterpret_cast();
  let p_i: Ptr<i32> = reinterpreted_u.upgrade().deref().i();
  ...
  // p_i is dangling because reinterpreted_u.deref().upgrade() is creating a temporary union
}

Activity

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