Skip to content

Use niche length type for strlen to guarantee isize::MAX bound - #162930

Open
clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:strlen-is-bounded
Open

clarfonthey wants to merge 1 commit into
rust-lang:mainfrom
clarfonthey:strlen-is-bounded

Conversation

@clarfonthey

@clarfonthey clarfonthey commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Alternative to #162912.

r? hanna-kruppe
cc @Darksonn

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 17, 2026
@rustbot

rustbot commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

hanna-kruppe is currently at their maximum review capacity.
They may take a while to respond.

unsafe { strlen(s) }
// SAFETY: Outer caller has provided a pointer to a valid C string,
// and its length is within bounds.
unsafe { UsizeNoHighBitMinusOne::new_unchecked(strlen(s)) }

@clarfonthey clarfonthey Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I decided to do this (which is just a transmute) instead of making strlen explicitly return this type, since it felt apt to include this as part of this particular method's unsafe precondition instead of trying to assume that would hold for the C version.

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants