Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
clarfonthey:strlen-is-bounded
Sep 19, 2026
Merged

rust-bors[bot] merged 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

@joshtriplett

Copy link
Copy Markdown
Member

@bors r+ rollup

Seems like a reasonable approach to me. Nice to be able to eliminate the conditions and panics on the basis of our existing requirement for sizes.

@rust-bors

rust-bors Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 2605c8d has been approved by joshtriplett

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 18, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
Rollup of 9 pull requests

Successful merges:

 - #161777 (Add Natvis visualiser and debuginfo tests for `f128`)
 - #162423 (Rename the src install build step to rust-src.)
 - #162824 (link Enzyme and the offload with in-tree lld if possible)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - #160401 (sparc: make ABI consistent with clang)
 - #161777 (Add Natvis visualiser and debuginfo tests for `f128`)
 - #162423 (Rename the src install build step to rust-src.)
 - #162740 (stdarch subtree update)
 - #162824 (link Enzyme and the offload with in-tree lld if possible)
 - #162832 (add `Div` and `Mul` for `Complex<{float}>`)
 - #161005 (fix: unfulfilled nested dead code lint)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162661 (simplify `Target::GenericParam`)
 - #162666 (Tidy footnote in `platform-support.md`)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - #160401 (sparc: make ABI consistent with clang)
 - #161777 (Add Natvis visualiser and debuginfo tests for `f128`)
 - #162423 (Rename the src install build step to rust-src.)
 - #162740 (stdarch subtree update)
 - #162824 (link Enzyme and the offload with in-tree lld if possible)
 - #161005 (fix: unfulfilled nested dead code lint)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162661 (simplify `Target::GenericParam`)
 - #162666 (Tidy footnote in `platform-support.md`)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162929 (Update unicode_data to Unicode version 18.0.0)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #160401 (sparc: make ABI consistent with clang)
 - #161777 (Add Natvis visualiser and debuginfo tests for `f128`)
 - #162740 (stdarch subtree update)
 - #162824 (link Enzyme and the offload with in-tree lld if possible)
 - #161005 (fix: unfulfilled nested dead code lint)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162661 (simplify `Target::GenericParam`)
 - #162666 (Tidy footnote in `platform-support.md`)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162929 (Update unicode_data to Unicode version 18.0.0)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 18, 2026
…oshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to rust-lang#162912.

r? hanna-kruppe
cc @Darksonn
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
…uwer

Rollup of 20 pull requests

Successful merges:

 - #160401 (sparc: make ABI consistent with clang)
 - #162715 (don't calculate dtors if the self ty has impossible bounds)
 - #162740 (stdarch subtree update)
 - #162824 (link Enzyme and the offload with in-tree lld if possible)
 - #162946 (Simplify query stack printing)
 - #161005 (fix: unfulfilled nested dead code lint)
 - #161246 (Normalize non-rigid aliases in ty_known_to_outlive)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162661 (simplify `Target::GenericParam`)
 - #162666 (Tidy footnote in `platform-support.md`)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162879 (use u64 limbs in core::num::bignum)
 - #162903 (PassWrapper: adapt to LLVM reading exception model from module flag)
 - #162905 (c-variadic: add checks for windows i686)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162929 (Update unicode_data to Unicode version 18.0.0)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
 - #162960 (Guard types with unstable `Allocator` params ahead of partial stabilization)
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #160401 (sparc: make ABI consistent with clang)
 - #162715 (don't calculate dtors if the self ty has impossible bounds)
 - #162740 (stdarch subtree update)
 - #162946 (Simplify query stack printing)
 - #161005 (fix: unfulfilled nested dead code lint)
 - #161246 (Normalize non-rigid aliases in ty_known_to_outlive)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162661 (simplify `Target::GenericParam`)
 - #162666 (Tidy footnote in `platform-support.md`)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162879 (use u64 limbs in core::num::bignum)
 - #162903 (PassWrapper: adapt to LLVM reading exception model from module flag)
 - #162905 (c-variadic: add checks for windows i686)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162929 (Update unicode_data to Unicode version 18.0.0)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
 - #162960 (Guard types with unstable `Allocator` params ahead of partial stabilization)
rust-bors Bot pushed a commit that referenced this pull request Sep 18, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - #160401 (sparc: make ABI consistent with clang)
 - #162715 (don't calculate dtors if the self ty has impossible bounds)
 - #162740 (stdarch subtree update)
 - #162946 (Simplify query stack printing)
 - #161005 (fix: unfulfilled nested dead code lint)
 - #161246 (Normalize non-rigid aliases in ty_known_to_outlive)
 - #161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - #162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - #162661 (simplify `Target::GenericParam`)
 - #162666 (Tidy footnote in `platform-support.md`)
 - #162803 (docs(num): add documentation for `NonZero::from_str`)
 - #162879 (use u64 limbs in core::num::bignum)
 - #162903 (PassWrapper: adapt to LLVM reading exception model from module flag)
 - #162905 (c-variadic: add checks for windows i686)
 - #162906 (Move more `rustdoc-html` tests in the right location)
 - #162922 (An assortment of polonius tweaks)
 - #162929 (Update unicode_data to Unicode version 18.0.0)
 - #162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
 - #162960 (Guard types with unstable `Allocator` params ahead of partial stabilization)
@rust-bors
rust-bors Bot merged commit 40f4756 into rust-lang:main Sep 19, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 19, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 19, 2026
Rollup merge of #162930 - clarfonthey:strlen-is-bounded, r=joshtriplett

Use niche length type for strlen to guarantee `isize::MAX` bound

Alternative to #162912.

r? hanna-kruppe
cc @Darksonn
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Sep 19, 2026
…uwer

Rollup of 19 pull requests

Successful merges:

 - rust-lang/rust#160401 (sparc: make ABI consistent with clang)
 - rust-lang/rust#162715 (don't calculate dtors if the self ty has impossible bounds)
 - rust-lang/rust#162740 (stdarch subtree update)
 - rust-lang/rust#162946 (Simplify query stack printing)
 - rust-lang/rust#161005 (fix: unfulfilled nested dead code lint)
 - rust-lang/rust#161246 (Normalize non-rigid aliases in ty_known_to_outlive)
 - rust-lang/rust#161803 (Fix docs of make_ascii_lowercase/make_ascii_upercase)
 - rust-lang/rust#162256 (Add mentions to sync back `RELEASES.md` to the `main` branch)
 - rust-lang/rust#162661 (simplify `Target::GenericParam`)
 - rust-lang/rust#162666 (Tidy footnote in `platform-support.md`)
 - rust-lang/rust#162803 (docs(num): add documentation for `NonZero::from_str`)
 - rust-lang/rust#162879 (use u64 limbs in core::num::bignum)
 - rust-lang/rust#162903 (PassWrapper: adapt to LLVM reading exception model from module flag)
 - rust-lang/rust#162905 (c-variadic: add checks for windows i686)
 - rust-lang/rust#162906 (Move more `rustdoc-html` tests in the right location)
 - rust-lang/rust#162922 (An assortment of polonius tweaks)
 - rust-lang/rust#162929 (Update unicode_data to Unicode version 18.0.0)
 - rust-lang/rust#162930 (Use niche length type for strlen to guarantee `isize::MAX` bound)
 - rust-lang/rust#162960 (Guard types with unstable `Allocator` params ahead of partial stabilization)
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 21, 2026
…Simulacrum

Avoid unreachable integer underflow check in `CStr::count_bytes()`

Follow up to rust-lang#162930.

cc @clarfonthey, @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 21, 2026
…Simulacrum

Avoid unreachable integer underflow check in `CStr::count_bytes()`

Follow up to rust-lang#162930.

cc @clarfonthey, @hanna-kruppe
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 21, 2026
…Simulacrum

Avoid unreachable integer underflow check in `CStr::count_bytes()`

Follow up to rust-lang#162930.

cc @clarfonthey, @hanna-kruppe
rust-bors Bot pushed a commit that referenced this pull request Sep 21, 2026
Rollup merge of #163005 - Darksonn:cstr-count-bytes, r=Mark-Simulacrum

Avoid unreachable integer underflow check in `CStr::count_bytes()`

Follow up to #162930.

cc @clarfonthey, @hanna-kruppe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

4 participants