-
Notifications
You must be signed in to change notification settings - Fork 52
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
Improve usage of unsafe blocks #18
Conversation
|
||
fn try_from(tss_signature: TPMT_SIGNATURE) -> Result<Self> { | ||
impl Signature { | ||
pub unsafe fn try_from(tss_signature: TPMT_SIGNATURE) -> Result<Self> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that is a very good decision to put the unsafe
on the method here as we can not trust that the tss_signature
argument is always well-composed. The caller of this method needs to make the decision as only him knows where the parameter comes from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking: "what if the method is not public
?"
But even so, it would maybe be wrong to put it safe anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a comment would be nice to explain those things? Or that could be part of the documentation of the method itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I decided to just leave it to documentation, a comment isn't as visible for users
|
||
// Close the context. | ||
unsafe { tss2_esys::Esys_Finalize(&mut esys_context.into_raw() as *mut *mut ESYS_CONTEXT) }; | ||
unsafe { tss2_esys::Esys_Finalize(&mut esys_context.into_raw()) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💌
@@ -412,7 +412,7 @@ impl Context { | |||
|
|||
if ret.is_success() { | |||
let signature = unsafe { MBox::from_raw(signature) }; | |||
Ok((*signature).try_into()?) | |||
Ok(unsafe { Signature::try_from(*signature)? }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a comment to explain why this is safe would make us better unsafe
citizens 👷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same everywhere we use the unsafe
block actually in my opinion. The same way we kind of have to justify when we panic explicitely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, will add
This commit improves our handling of unsafe blocks. It moves the unsafe label to methods that could lead to undefined behaviour instead of hiding the lack of safety inside the method. Signed-off-by: Ionut Mihalcea <ionut.mihalcea@arm.com>
Add a MAINTAINERS file
This commit improves our handling of
unsafe
blocks. It moves theunsafe
label to methods that could lead to undefined behaviourinstead of hiding the lack of safety inside the method.
All remaining
unsafe
blocks should be safe to a sane level - we control the inputs to the function calls within and can guarantee, based on trust (usually in the TSS stack below) that the calls are safe.Thread safety is offered by all
Context
struct methods requiring mutable references toself
.Fixes #2