-
Notifications
You must be signed in to change notification settings - Fork 7
replicate clippy lint ptr_offset_with_cast
#107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,35 @@ | ||
| pattern ptr-offset-with-cast | ||
|
|
||
| patt { | ||
| #[diag = "ptr-offset-with-cast"] | ||
| p_offset[ | ||
| $PTR: type where is_ptr(self), | ||
| ] = fn _ (..) -> _ { | ||
| let $ptr: $PTR = _; | ||
| let $offset_usize: usize = _; | ||
| let $offset_isize: isize = copy $offset_usize as isize (IntToInt); | ||
| 'ptr_offset_call: | ||
| _ = std::intrinsics::arith_offset::<u8>(copy $ptr, copy $offset_isize); | ||
| } | ||
|
|
||
| #[diag = "ptr-offset-with-cast"] | ||
| p_wrapping_offset[ | ||
| $PTR: type where is_ptr(self), | ||
| ] = fn _ (..) -> _ { | ||
| let $ptr: $PTR = _; | ||
| let $offset_usize: usize = _; | ||
| let $offset_isize: isize = copy $offset_usize as isize (IntToInt); | ||
| 'ptr_offset_call: | ||
| _ = Offset(copy $ptr, copy $offset_isize); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| diag { | ||
| ptr-offset-with-cast = { | ||
| primary(ptr_offset_call) = "usage of the `offset` pointer method with a usize casted to an isize.", | ||
|
stuuupidcat marked this conversation as resolved.
|
||
| help = "if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead.", | ||
|
stuuupidcat marked this conversation as resolved.
|
||
| name = "ptr_offset_with_cast", | ||
| level = "warn", | ||
| } | ||
| } | ||
This file contains hidden or 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,45 @@ | ||
| //@compile-flags: -Z mir-opt-level=0 | ||
| fn main() { | ||
| let vec = vec![b'a', b'b', b'c']; | ||
| let ptr = vec.as_ptr(); | ||
|
|
||
| let offset_u8 = 1_u8; | ||
| let offset_usize = 1_usize; | ||
| let offset_isize = 1_isize; | ||
|
|
||
| unsafe { | ||
| let _ = ptr.offset(offset_usize as isize); | ||
| //~^ ptr_offset_with_cast | ||
| //~|ERROR: it is an undefined behavior to offset a pointer using an unchecked integer | ||
| let _ = ptr.offset(offset_isize as isize); | ||
| //~^ERROR: it is an undefined behavior to offset a pointer using an unchecked integer | ||
| let _ = ptr.offset(offset_u8 as isize); | ||
| //~^ERROR: it is an undefined behavior to offset a pointer using an unchecked integer | ||
|
|
||
| let _ = ptr.wrapping_offset(offset_usize as isize); | ||
| //~^ ptr_offset_with_cast | ||
| let _ = ptr.wrapping_offset(offset_isize as isize); | ||
| let _ = ptr.wrapping_offset(offset_u8 as isize); | ||
|
|
||
| let _ = S.offset(offset_usize as isize); | ||
| let _ = S.wrapping_offset(offset_usize as isize); | ||
|
|
||
| let _ = (&ptr).offset(offset_usize as isize); | ||
| //~^ ptr_offset_with_cast | ||
| //~|ERROR: it is an undefined behavior to offset a pointer using an unchecked integer | ||
| let _ = (&ptr).wrapping_offset(offset_usize as isize); | ||
| //~^ ptr_offset_with_cast | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy)] | ||
| struct S; | ||
|
|
||
| impl S { | ||
| fn offset(self, _: isize) -> Self { | ||
| self | ||
| } | ||
| fn wrapping_offset(self, _: isize) -> Self { | ||
| self | ||
| } | ||
| } |
This file contains hidden or 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,82 @@ | ||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:19:21 | ||
| | | ||
| LL | let _ = ptr.wrapping_offset(offset_usize as isize); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
| = note: `-D rpl::ptr-offset-with-cast` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(rpl::ptr_offset_with_cast)]` | ||
|
|
||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:30:24 | ||
| | | ||
| LL | let _ = (&ptr).wrapping_offset(offset_usize as isize); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
|
|
||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:11:21 | ||
| | | ||
| LL | let _ = ptr.offset(offset_usize as isize); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
|
|
||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:27:24 | ||
| | | ||
| LL | let _ = (&ptr).offset(offset_usize as isize); | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
|
|
||
| error: it is an undefined behavior to offset a pointer using an unchecked integer | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:11:21 | ||
| | | ||
| LL | let _ = ptr.offset(offset_usize as isize); | ||
| | --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ offset here | ||
| | | | ||
| | pointer used here | ||
| | | ||
| = help: check whether it's in bound before offsetting | ||
| = note: See the safety section in https://doc.rust-lang.org/std/primitive.pointer.html#method.offset | ||
| = note: `-D rpl::unchecked-pointer-offset` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(rpl::unchecked_pointer_offset)]` | ||
|
|
||
| error: it is an undefined behavior to offset a pointer using an unchecked integer | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:14:21 | ||
| | | ||
| LL | let _ = ptr.offset(offset_isize as isize); | ||
| | --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ offset here | ||
| | | | ||
| | pointer used here | ||
| | | ||
| = help: check whether it's in bound before offsetting | ||
| = note: See the safety section in https://doc.rust-lang.org/std/primitive.pointer.html#method.offset | ||
|
|
||
| error: it is an undefined behavior to offset a pointer using an unchecked integer | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:16:21 | ||
| | | ||
| LL | let _ = ptr.offset(offset_u8 as isize); | ||
| | --- ^^^^^^^^^^^^^^^^^^^^^^^^^^ offset here | ||
| | | | ||
| | pointer used here | ||
| | | ||
| = help: check whether it's in bound before offsetting | ||
| = note: See the safety section in https://doc.rust-lang.org/std/primitive.pointer.html#method.offset | ||
|
|
||
| error: it is an undefined behavior to offset a pointer using an unchecked integer | ||
| --> tests/ui/clippy/ptr_offset_with_cast.rs:27:24 | ||
| | | ||
| LL | let _ = (&ptr).offset(offset_usize as isize); | ||
| | ------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ offset here | ||
| | | | ||
| | pointer used here | ||
| | | ||
| = help: check whether it's in bound before offsetting | ||
| = note: See the safety section in https://doc.rust-lang.org/std/primitive.pointer.html#method.offset | ||
|
|
||
| error: aborting due to 8 previous errors | ||
|
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/cve/cve_2021_25905/minimal.rs:19:38 | ||
| | | ||
| LL | buf.as_mut_ptr().offset(b as isize), | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| ... | ||
| LL | cases!(Vec::new()); | ||
| | ------------------ in this macro invocation | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
| = note: `-D rpl::ptr-offset-with-cast` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(rpl::ptr_offset_with_cast)]` | ||
| = note: this error originates in the macro `cases` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/cve/cve_2021_25905/minimal.rs:19:38 | ||
| | | ||
| LL | buf.as_mut_ptr().offset(b as isize), | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| ... | ||
| LL | cases!(vec![1, 2, 3]); | ||
| | --------------------- in this macro invocation | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
| = note: this error originates in the macro `cases` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/cve/cve_2021_25905/minimal.rs:19:38 | ||
| | | ||
| LL | buf.as_mut_ptr().offset(b as isize), | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| ... | ||
| LL | cases!(Vec::with_capacity(0)); | ||
| | ----------------------------- in this macro invocation | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
| = note: this error originates in the macro `cases` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: usage of the `offset` pointer method with a usize casted to an isize. | ||
| --> tests/ui/cve/cve_2021_25905/minimal.rs:19:38 | ||
| | | ||
| LL | buf.as_mut_ptr().offset(b as isize), | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| ... | ||
| LL | cases!(Vec::with_capacity(1)); | ||
| | ----------------------------- in this macro invocation | ||
| | | ||
| = help: if you’re always increasing the pointer address, you can avoid the numeric cast by using the `add` method instead. | ||
| = note: this error originates in the macro `cases` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.