forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split
fuzzy_provenance_casts
into lossy and fuzzy, feature gate and…
… test it * split `fuzzy_provenance_casts` into a ptr2int and a int2ptr lint * feature gate both lints * update documentation to be more realistic short term * add tests for these lints
- Loading branch information
Showing
11 changed files
with
281 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/doc/unstable-book/src/language-features/strict-provenance.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# `strict_provenance` | ||
|
||
The tracking issue for this feature is: [#95228] | ||
|
||
[#95228]: https://github.com/rust-lang/rust/issues/95228 | ||
----- | ||
|
||
The `strict_provenance` feature allows to enable the `fuzzy_provenance_casts` and `lossy_provenance_casts` lints. | ||
These lint on casts between integers and pointers, that are recommended against or invalid in the strict provenance model. | ||
The same feature gate is also used for the experimental strict provenance API in `std` (actually `core`). | ||
|
||
## Example | ||
|
||
```rust | ||
#![feature(strict_provenance)] | ||
#![warn(fuzzy_provenance_casts)] | ||
|
||
fn main() { | ||
let _dangling = 16_usize as *const u8; | ||
//~^ WARNING: strict provenance disallows casting integer `usize` to pointer `*const u8` | ||
} | ||
``` |
19 changes: 19 additions & 0 deletions
19
src/test/ui/feature-gates/feature-gate-strict_provenance.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// check-pass | ||
|
||
#![deny(fuzzy_provenance_casts)] | ||
//~^ WARNING unknown lint: `fuzzy_provenance_casts` | ||
//~| WARNING unknown lint: `fuzzy_provenance_casts` | ||
//~| WARNING unknown lint: `fuzzy_provenance_casts` | ||
#![deny(lossy_provenance_casts)] | ||
//~^ WARNING unknown lint: `lossy_provenance_casts` | ||
//~| WARNING unknown lint: `lossy_provenance_casts` | ||
//~| WARNING unknown lint: `lossy_provenance_casts` | ||
|
||
fn main() { | ||
// no warnings emitted since the lints are not activated | ||
|
||
let _dangling = 16_usize as *const u8; | ||
|
||
let x: u8 = 37; | ||
let _addr: usize = &x as *const u8 as usize; | ||
} |
63 changes: 63 additions & 0 deletions
63
src/test/ui/feature-gates/feature-gate-strict_provenance.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
warning: unknown lint: `fuzzy_provenance_casts` | ||
--> $DIR/feature-gate-strict_provenance.rs:3:1 | ||
| | ||
LL | #![deny(fuzzy_provenance_casts)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `#[warn(unknown_lints)]` on by default | ||
= note: the `fuzzy_provenance_casts` lint is unstable | ||
= note: see issue #95228 <https://github.com/rust-lang/rust/issues/95228> for more information | ||
= help: add `#![feature(strict_provenance)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `lossy_provenance_casts` | ||
--> $DIR/feature-gate-strict_provenance.rs:7:1 | ||
| | ||
LL | #![deny(lossy_provenance_casts)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `lossy_provenance_casts` lint is unstable | ||
= note: see issue #95228 <https://github.com/rust-lang/rust/issues/95228> for more information | ||
= help: add `#![feature(strict_provenance)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `fuzzy_provenance_casts` | ||
--> $DIR/feature-gate-strict_provenance.rs:3:1 | ||
| | ||
LL | #![deny(fuzzy_provenance_casts)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `fuzzy_provenance_casts` lint is unstable | ||
= note: see issue #95228 <https://github.com/rust-lang/rust/issues/95228> for more information | ||
= help: add `#![feature(strict_provenance)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `lossy_provenance_casts` | ||
--> $DIR/feature-gate-strict_provenance.rs:7:1 | ||
| | ||
LL | #![deny(lossy_provenance_casts)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `lossy_provenance_casts` lint is unstable | ||
= note: see issue #95228 <https://github.com/rust-lang/rust/issues/95228> for more information | ||
= help: add `#![feature(strict_provenance)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `fuzzy_provenance_casts` | ||
--> $DIR/feature-gate-strict_provenance.rs:3:1 | ||
| | ||
LL | #![deny(fuzzy_provenance_casts)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `fuzzy_provenance_casts` lint is unstable | ||
= note: see issue #95228 <https://github.com/rust-lang/rust/issues/95228> for more information | ||
= help: add `#![feature(strict_provenance)]` to the crate attributes to enable | ||
|
||
warning: unknown lint: `lossy_provenance_casts` | ||
--> $DIR/feature-gate-strict_provenance.rs:7:1 | ||
| | ||
LL | #![deny(lossy_provenance_casts)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: the `lossy_provenance_casts` lint is unstable | ||
= note: see issue #95228 <https://github.com/rust-lang/rust/issues/95228> for more information | ||
= help: add `#![feature(strict_provenance)]` to the crate attributes to enable | ||
|
||
warning: 6 warnings emitted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![feature(strict_provenance)] | ||
#![deny(fuzzy_provenance_casts)] | ||
|
||
fn main() { | ||
let dangling = 16_usize as *const u8; | ||
//~^ ERROR strict provenance disallows casting integer `usize` to pointer `*const u8` | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
error: strict provenance disallows casting integer `usize` to pointer `*const u8` | ||
--> $DIR/lint-strict-provenance-fuzzy-casts.rs:5:20 | ||
| | ||
LL | let dangling = 16_usize as *const u8; | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/lint-strict-provenance-fuzzy-casts.rs:2:9 | ||
| | ||
LL | #![deny(fuzzy_provenance_casts)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
= help: if you can't comply with strict provenance and don't have a pointer with the correct provenance you can use `std::ptr::from_exposed_addr()` instead | ||
help: use `.with_addr()` to adjust a valid pointer in the same allocation, to this address | ||
| | ||
LL | let dangling = (...).with_addr(16_usize); | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![feature(strict_provenance)] | ||
#![deny(lossy_provenance_casts)] | ||
|
||
fn main() { | ||
let x: u8 = 37; | ||
let addr: usize = &x as *const u8 as usize; | ||
//~^ ERROR under strict provenance it is considered bad style to cast pointer `*const u8` to integer `usize` | ||
|
||
let addr_32bit = &x as *const u8 as u32; | ||
//~^ ERROR under strict provenance it is considered bad style to cast pointer `*const u8` to integer `u32` | ||
} |
Oops, something went wrong.