Skip to content

Document that str, slices, (more?) can't safely straddle allocation boundaries #62765

Open
@hanna-kruppe

Description

@hanna-kruppe

Slicing, indexing, and other safe operations on slices and strings pervasively use <*T>::offset and APIs built on top of it. These have the requirement that

Both the starting and resulting pointer must be either in bounds or one byte past the end of the same allocated object.

So if one allocates two pieces of memory and after proper checking miraculously finds they are directly adjacent, one can't safely construct a slice/str/etc. that spans both of these allocations. At least, one can't do very many things with the result it without causing UB from crossing the boundary between the allocations.

I couldn't find anything documenting this. It should be noted on the unsafe constructors (from_raw_parts etc.) at minimum. These already link to offset's documentation but only refer to its "no larger than isize::MAX" requirement, with no mention that the other requirements are also relevant.

cc oberien/str-concat#8
cc @rust-lang/wg-unsafe-code-guidelines

(Similar issues apply to references-to-arrays and field accesses in aggregates, but this is due to the compiler's codegen for language primitives rather than due to standard library code, so it should go into the UCG and I believe we're more or less covering that already.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions