You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The style::gecko_bindings::sugar module implements various safe methods and traits for FFI types from Gecko. However, many of these methods are actually unsafe because they depend on invariants that are not enforced. For example, this program has undefined behavior without using unsafe:
externcrate style;use style::gecko_bindings::structs::nsTArray;fnmain(){let a = nsTArray{mBuffer: std::ptr::null_mut()};let b = &a[..];}
Since the structs’ fields are public, we can’t statically enforce invariants on them. Assuming we don’t want to add runtime checks, we need to make sure these methods and impls can be used only on valid struct values. Possible solutions:
Create wrapper types that cannot be constructed in safe code, and implement methods/traits on the wrapper types instead of the original FFI structs.
Generate binding structs with private fields, and include the bindings and the impls in a single module.
The
style::gecko_bindings::sugar
module implements various safe methods and traits for FFI types from Gecko. However, many of these methods are actually unsafe because they depend on invariants that are not enforced. For example, this program has undefined behavior without usingunsafe
:Since the structs’ fields are public, we can’t statically enforce invariants on them. Assuming we don’t want to add runtime checks, we need to make sure these methods and impls can be used only on valid struct values. Possible solutions:
pub(super)
or similar to make their fields public within some trusted supermodule, but private to outside code. Not yet possible in stable Rust (Tracking issue forpub(restricted)
privacy (RFC #1422) rust-lang/rust#32409).The text was updated successfully, but these errors were encountered: