Skip to content

PtrExt::offset taking an isize argument is not ideal #22104

Closed

Description

@carllerche

PtrExt::offset taking an isize argument prevents being able to advance forward / back the full possible range of a pointer. A better option might be to have separate functions for forwards / backwards that take usize arguments.

Activity

  1. changed the title [-]ptr::offset taking an isize argument is not ideal[/-] [+]PtrExt::offset taking an isize argument is not ideal[/+] on Feb 9, 2015
  2. steveklabnik commented on Feb 9, 2015

    @steveklabnik
    Contributor
  3. bluss commented on Feb 9, 2015

    @bluss
    Contributor

    It's a fundamental issue with LLVM, see #18726 and GEP docs

    cc @gankro

  4. Gankra commented on Feb 9, 2015

    @Gankra
    Contributor

    With high probability we will add add and sub methods which take uints, because 99% of the time you have a uint, and 99% of the time you're doing an add. That said, they will just be sugar for casting to an int, because that's just what the ptr offset intrinsic in LLVM takes.

    It's not like anyone ever does a check for "the uint is too large" when they cast, so this would just be an ergonomic improvement. I suppose we could add some friendly debug asserts in there.

    That said, this API taking a signed int is a soundness issue for us due to being able to allocate more than int::MAX contiguous bytes in some situations. There are a few options to address this, but all of them have drawbacks.

  5. bluss commented on Feb 9, 2015

    @bluss
    Contributor

    Not sure we need those methods. I'll be the conservative side and say that hiding the problem doesn't help. Users of .offset need to be aware, and a cast to isize might help them with that.

  6. carllerche commented on Feb 9, 2015

    @carllerche
    ContributorAuthor

    Why is the intrinsic different than casting the ptr to usize, modifying it, and casting back to a ptr?

  7. bluss commented on Feb 9, 2015

    @bluss
    Contributor

    That's this question in the GEP FAQ.

    thestinger was the guy to make sure Rust used GEP so that llvm could optimize much better.

  8. Thiez commented on Feb 10, 2015

    @Thiez
    Contributor

    The maximum object size in Rust is equal to std::isize::MAX, see here. It cannot be the case that you need to offset a pointer more than that, or you're probably invoking undefined behavior.

  9. Gankra commented on Feb 10, 2015

    @Gankra
    Contributor

    @Thiez that specification was of course added in response to this problem, so it's a bit of a circular argument.

  10. Thiez commented on Feb 11, 2015

    @Thiez
    Contributor

    You couldn't do this in C either, because the difference between two pointers has type ptrdiff_t, which is signed. I see we don't check for this when allocating memory, which should probably be considered a bug.

  11. aturon commented on Feb 16, 2015

    @aturon
    Contributor

    Nominating for 1.0-beta P-backcompat-libs. We need to resolve this issue.

  12. pnkfelix commented on Feb 19, 2015

    @pnkfelix
    Contributor

    assigning to self (to get answers to some questions I have)

  13. self-assigned this
    on Feb 19, 2015
  14. pnkfelix commented on Feb 19, 2015

    @pnkfelix
    Contributor

    Not 1.0 blocker based on current understanding of situation. P-high.

  15. alexcrichton commented on Jan 21, 2016

    @alexcrichton
    Member

    The libs team discussed this in triage yesterday and the conclusion was that this cannot be changed due to it being stable, and the situation is also intended due to the limitations imposed by LLVM.

    Pointer offsets need to be able to go backwards (e.g. move around in an array), but it's undefined behavior to overflow, so the only defined way to do this is to have a signed offset.

  16. alexcrichton commented on Jan 21, 2016

    @alexcrichton
    Member

    I also just reread the docs and I think that they adequately reflect this.

  17. added a commit that references this issue on Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P-mediumMedium priorityT-libs-api[DEPRECATED; DO NOT USE]

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions