-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix(turbopath): avoid incorrect lifetime in RelativeUnixPath::new #9400
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
8 Skipped Deployments
|
Nice write-up! Looking at the other unsafe block in that file we should be ok if &self is OK and this PR fixes that. While we are here, perhaps some proper SAFETY comments would be helpful for future people down the line. Don't have to do it everywhere but the paths lib is used by pretty much every turbo invocation https://std-dev-guide.rust-lang.org/policy/safety-comments.html We don't need the whole explainer some comment that the lifetime is attached to the lifetime of the owned object passed in so we can't end up with dangling pointers would be great. Also is this code also prone to the same UB? I am not sure off the top of my head if PathBuf -> &Path is isomorphic to String -> &str or if they behave the same under APIT
I guess that is why the fn is |
Good catch! |
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.
Oops yeah that's probably my bad
03eb0cc
to
1ad6ed7
Compare
Description
I ended up hitting a panic using this function incorrectly, we can make it safer.
Explanation of Issue
Take the following code snippet:
It panics with the following error:
The issue is with the line
let bar = RelativeUnixPath::new(foo);
so lets take a look at the definition ofRelativeUnixPath::new
:Lets break down the signature,
new
is generic over a lifetime'a
and a typeP
whereP
must implementAsRef<str>
and be valid for at least the lifetime of'a
.new
then returns aResult<&'a Self>
aka a reference to aRelativeUnixPath
valid for at least'a
.It's important to clarify that
P: AsRef<str> + 'a
meansP
is valid for'a
, not the&str
created byas_ref()
.So if we pass a
String
object toRelativeUnixPath::new
we end up withnew<'a>(value: String) -> Result<&'a Self>
, but what's the'a
? There's not any lifetime that's part ofString
!If we look at the Static section of Rust By Example it contains the following remark:
String
is owned so it passes a bound such asT: 'static
or in our caseP: AsRef<str> + 'static
.This means that we can currently write a function like this:
This is clearly incorrect,
foo
takes ownership ofs
, but somehow returns a'static
reference even thoughs
is consumed in the function and will be dropped at the endSolution
The solution is that we make sure we make sure we take a type that's a reference! e.g.
fn new<'a, P: AsRef<str> + ?Sized>(value: &'a P) -> Result<&'a Self>
this prevents us from passing an owned type into the constructor and smuggling in bad lifetimes to the codebase. (?Sized
is just needed becauseSized
is an implicit trait bound in Rust and we're opting out of it. Since we're taking a reference, we don't need to know the size of the type see the Excotically Sized Types section of the Rustonomicon for more information)Testing Instructions
The following will no longer compiles: