From eda8c70a80b3d6a7b8345f01dd80efd331e5f94b Mon Sep 17 00:00:00 2001
From: topjohnwu <topjohnwu@gmail.com>
Date: Wed, 11 Oct 2023 23:48:54 -0700
Subject: [PATCH] Borrow value instead of moving in FsPath::from()

When accepting a value of AsRef<Utf8CStr> in FsPath::from(), the
existing code will move a value of Utf8CStrBufArr, creating a reference
that lives longer than the borrowing value, causing undefined behavior.

The issue is only visible on release builds, as more advanced
optimizations will be more aggressive re-using the stack of variables
that no longer lives.

Fix #7408
---
 native/src/base/cstr.rs  | 4 ++--
 native/src/base/files.rs | 4 ++--
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/native/src/base/cstr.rs b/native/src/base/cstr.rs
index ab9f9ab31..53d0504b8 100644
--- a/native/src/base/cstr.rs
+++ b/native/src/base/cstr.rs
@@ -385,12 +385,12 @@ pub struct FsPath(Utf8CStr);
 
 impl FsPath {
     #[inline(always)]
-    pub fn from<'a, T: AsRef<Utf8CStr>>(value: T) -> &'a FsPath {
+    pub fn from<T: AsRef<Utf8CStr> + ?Sized>(value: &T) -> &FsPath {
         unsafe { mem::transmute(value.as_ref()) }
     }
 
     #[inline(always)]
-    pub fn from_mut<'a, T: AsMut<Utf8CStr>>(mut value: T) -> &'a mut FsPath {
+    pub fn from_mut<T: AsMut<Utf8CStr> + ?Sized>(value: &mut T) -> &mut FsPath {
         unsafe { mem::transmute(value.as_mut()) }
     }
 }
diff --git a/native/src/base/files.rs b/native/src/base/files.rs
index 253113e83..424bdaa04 100644
--- a/native/src/base/files.rs
+++ b/native/src/base/files.rs
@@ -229,13 +229,13 @@ impl DirEntry<'_> {
     pub fn get_attr(&self) -> io::Result<FileAttr> {
         let mut path = Utf8CStrBufArr::default();
         self.path(&mut path)?;
-        FsPath::from(path).get_attr()
+        FsPath::from(&path).get_attr()
     }
 
     pub fn set_attr(&self, attr: &FileAttr) -> io::Result<()> {
         let mut path = Utf8CStrBufArr::default();
         self.path(&mut path)?;
-        FsPath::from(path).set_attr(attr)
+        FsPath::from(&path).set_attr(attr)
     }
 }