Skip to content

Commit 327632c

Browse files
Fulgen301gitbot
authored and
gitbot
committed
Win: rename: Use offset_of! in struct size calculation
1 parent 0dcd865 commit 327632c

File tree

1 file changed

+7
-4
lines changed
  • std/src/sys/pal/windows

1 file changed

+7
-4
lines changed

std/src/sys/pal/windows/fs.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,13 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
12271227

12281228
let new_len_without_nul_in_bytes = (new.len() - 1).try_into().unwrap();
12291229

1230-
let struct_size = mem::size_of::<c::FILE_RENAME_INFO>() - mem::size_of::<u16>()
1231-
+ new.len() * mem::size_of::<u16>();
1230+
// The last field of FILE_RENAME_INFO, the file name, is unsized,
1231+
// and FILE_RENAME_INFO has two padding bytes.
1232+
// Therefore we need to make sure to not allocate less than
1233+
// size_of::<c::FILE_RENAME_INFO>() bytes, which would be the case with
1234+
// 0 or 1 character paths + a null byte.
1235+
let struct_size = mem::size_of::<c::FILE_RENAME_INFO>()
1236+
.max(mem::offset_of!(c::FILE_RENAME_INFO, FileName) + new.len() * mem::size_of::<u16>());
12321237

12331238
let struct_size: u32 = struct_size.try_into().unwrap();
12341239

@@ -1296,8 +1301,6 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
12961301
}
12971302
.unwrap_or_else(|| create_file(0, 0))?;
12981303

1299-
// The last field of FILE_RENAME_INFO, the file name, is unsized.
1300-
// Therefore we need to subtract the size of one wide char.
13011304
let layout = core::alloc::Layout::from_size_align(
13021305
struct_size as _,
13031306
mem::align_of::<c::FILE_RENAME_INFO>(),

0 commit comments

Comments
 (0)