casey / casey/x-serialization-format

Check may need to be unsafe to implement

Open
#101 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1
Forks
1
PR merge metrics
No merged PRs in 30d

Description

Check has this signature:

```
fn check<'value>(suspect: &'value MaybeUninit, _buffer: &[u8]) -> Result<&'value Self>;
```

Code that calls `check` assumes that if a call to check returns `Ok`, the `suspect` argument must be a valid instance of `Self`.

This is reasonable if the result is the references that is passed in. However, a bad check implementation could return a reference to a valid self that was not derived from `suspect`:

```
fn check<'value>(suspect: &'value MaybeUninit, _buffer: &[u8]) -> Result<&'value Self> {
Ok(&STATIC)
}
```

This would make the calling code's assumption that the passed-in suspect is valid false, which, if suspect were invalid, could lead to UB.

To fix this, I should probably make the `check` function unsafe, and indicate that it must only return `Ok` if `suspect` is valid.

As an alternative, I could also make all calling code assert that the the references point to the same memory location, thus eliminating the assumption, and keeping `check` safe to implement.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.