-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a
unsafe_ffi_drop_implementations
lint.
This detects cases where a struct or enum are annotated with `#[repr(C)]`, and *do not* have `#[unsafe_no_drop_flag]`, whereby it warns the user that the type may not have the expected size or layout. Also includes tests to ensure the lint is triggered by FFI+Drop structs and enums, *not* triggered by FFI+Drop+unsafe_no_drop_flag structs and enums, and *not* triggered by FFI+!Drop structs and enums. It also contains a tangential change to libstd/sys/windows/backtrace.rs. Specifically, the `Cleanup` type had `#[repr(C)]` and Drop, but was never passed to any FFI function.
- Loading branch information
1 parent
de8bc44
commit 26fcbd7
Showing
5 changed files
with
138 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,7 +284,6 @@ mod arch { | |
} | ||
} | ||
|
||
#[repr(C)] | ||
struct Cleanup { | ||
handle: libc::HANDLE, | ||
SymCleanup: SymCleanupFn, | ||
|
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,37 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![deny(unsafe_ffi_drop_implementations)] | ||
#![allow(dead_code)] | ||
|
||
extern { | ||
fn f(x: *const FfiUnsafeStruct, y: *const FfiUnsafeEnum); | ||
} | ||
|
||
#[repr(C)] | ||
struct FfiUnsafeStruct { //~ ERROR: unexpected size and layout | ||
i: i32, | ||
} | ||
|
||
impl Drop for FfiUnsafeStruct { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
#[repr(C)] | ||
enum FfiUnsafeEnum { //~ ERROR: unexpected size and layout | ||
Kaboom = 0, | ||
Splang = 1, | ||
} | ||
|
||
impl Drop for FfiUnsafeEnum { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
fn main() {} |
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,51 @@ | ||
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![deny(unsafe_ffi_drop_implementations)] | ||
#![allow(dead_code)] | ||
|
||
extern { | ||
fn f(x: *const FfiSafeStruct, y: *const FfiSafeEnum); | ||
} | ||
|
||
#[repr(C)] | ||
#[unsafe_no_drop_flag] | ||
struct FfiSafeStruct { | ||
i: i32, | ||
} | ||
|
||
impl Drop for FfiSafeStruct { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
#[repr(C)] | ||
#[unsafe_no_drop_flag] | ||
enum FfiSafeEnum { | ||
Kaboom = 0, | ||
Splang = 1, | ||
} | ||
|
||
impl Drop for FfiSafeEnum { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
// These two should not be affected as they have no Drop impl. | ||
#[repr(C)] | ||
struct FfiSafeStructNoDrop { | ||
i: i32, | ||
} | ||
|
||
#[repr(C)] | ||
enum FfiSafeEnumNoDrop { | ||
Peace = 0, | ||
WhaleSong = 1, | ||
} | ||
|
||
fn main() {} |