Skip to content
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

Bug: _Impl trait should use unsafe fn if any of its parameters are pointers #1506

Open
saschanaz opened this issue Feb 5, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@saschanaz
Copy link

Which crate is this about?

windows

Crate version

0.32.0

Summary

#[implement(Windows::Win32::Graphics::Imaging::IWICBitmapFrameDecode)]
pub struct Foo {}

impl IWICBitmapSource_Impl for Foo {
    fn GetSize(&mut self, puiwidth: *mut u32, puiheight: *mut u32) -> windows::core::Result<()> {
        unsafe {
            *puiwidth = 0u32;
            *puiheight = 0u32;
        }
        Ok(())
    }
    /* ... */
}

Expected behavior

It should be an unsafe fn as the implementation is expected to dereference the pointer parameters.

Actual behavior

Everything is just a safe fn.

Additional comments

cargo clippy is unsatisfied:

error: this public function might dereference a raw pointer but is not marked `unsafe`
   --> src\lib.rs:149:14
    |
149 |             *puiwidth = self.decoded.borrow().basic_info.xsize;
    |              ^^^^^^^^
    |
    = note: `#[deny(clippy::not_unsafe_ptr_arg_deref)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#not_unsafe_ptr_arg_deref
@saschanaz saschanaz added the bug Something isn't working label Feb 5, 2022
@kennykerr
Copy link
Collaborator

Good feedback - thanks! Will add detection for this and use unsafe fn accordingly.

@saschanaz
Copy link
Author

BTW, can this specific function accept a tuple as a return type instead?

@kennykerr
Copy link
Collaborator

BTW, can this specific function accept a tuple as a return type instead?

I'm not sure what you mean. Do you have an example?

@saschanaz
Copy link
Author

I mean, IWICBitmapSource_Impl::GetSize has two [out] parameters, and windows-rs can convert out parameters into a return type, so why not a tuple here? 👀

@kennykerr
Copy link
Collaborator

The challenge is figuring out whether it is appropriate just by looking at the metadata.

@kennykerr
Copy link
Collaborator

Regarding the original issue/question, I think I need to finally look a little closer at how we delineate what is safe and unsafe in the Windows API. Right now, all WinRT APIs are safe and the rest are not. That is somewhat arbitrary, and I'd like to do something perhaps more in line with clippy's decision making here where it depends on the signature of a particular function.

@Plecra
Copy link

Plecra commented Jan 24, 2023

Could this be implemented as an attribute within the win32metadata? "This function is sound to call with any arguments/environment, requiring only the parameters satisfy the listed types"

@kennykerr
Copy link
Collaborator

You could ask them. I doubt it would be simple to figure out though, given the large scope of APIs. I also think .NET has its own interpretation of safety here so it may be language specific. .NET seems to define a method as unsafe if any of its parameters include a pointer but doesn't account for value types that contain pointers. 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants