Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

container_of / offset_of #35

Closed
Closed
@diwic

Description

@diwic

There is some uncertainty about how to create a Rust version of container_of that is never UB, which is currently needed in order to create Arc::from_raw here: rust-lang/rust#37192

Since there is a warning in ptr.offset saying this:

Both the starting and resulting pointer must be either in bounds or one byte past the end of an 
allocated object. If either pointer is out of bounds or arithmetic overflow occurs then any further
use of the returned value will result in undefined behavior.

...the idea has come up to create a temporary object like this:

let t: T = mem::uninitialized(); // or mem::zeroed()
// do pointer arithmetic on t 
mem::forget(t);

...but this seems risky; in case do pointer arithmetic on t would panic for some unforeseen reason,
we'll try to drop an uninitialized object.

The other version is to start off with a null pointer, like this: https://github.com/alexcrichton/ctest/blob/2839e49847a6adca6e96cc81c46a1f03f8562ac0/src/lib.rs#L731 - but I'm not sure if that's better.

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