Skip to content

Rustonomicon: Missing bounds check in later Vec allocation code #31941

Closed
@itaibn

Description

@itaibn

In the Rustonomicon example code for implementing Vec, the first version of the grow method to increase capacity includes a bounds check to ensure that the new capacity is not an integer overflow, but when grow is reimplemented in later sections this check is omitted.

Specifically, in the section named "Allocating Memory", the method grow is implemented for Vec which includes the following line:

// Long comment explaining this assertion
assert!(old_num_bytes <= (::std::isize::MAX as usize) / 2,
        "capacity overflow");

However, in the sections "RawVec" and "The Final Code", this same method is implemented for RawVec, but the implementation does not contain this line or any equivalent functionality. This means that these later versions might behave incorrectly when the capacity reaches memory limits.

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