File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed
library/std/src/sys/pal/windows Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff line change @@ -1099,8 +1099,13 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
10991099
11001100 let new_len_without_nul_in_bytes = ( new. len ( ) - 1 ) . try_into ( ) . unwrap ( ) ;
11011101
1102- let struct_size = mem:: size_of :: < c:: FILE_RENAME_INFO > ( ) - mem:: size_of :: < u16 > ( )
1103- + new. len ( ) * mem:: size_of :: < u16 > ( ) ;
1102+ // The last field of FILE_RENAME_INFO, the file name, is unsized,
1103+ // and FILE_RENAME_INFO has two padding bytes.
1104+ // Therefore we need to make sure to not allocate less than
1105+ // size_of::<c::FILE_RENAME_INFO>() bytes, which would be the case with
1106+ // 0 or 1 character paths + a null byte.
1107+ let struct_size = mem:: size_of :: < c:: FILE_RENAME_INFO > ( )
1108+ . max ( mem:: offset_of!( c:: FILE_RENAME_INFO , FileName ) + new. len ( ) * mem:: size_of :: < u16 > ( ) ) ;
11041109
11051110 let struct_size: u32 = struct_size. try_into ( ) . unwrap ( ) ;
11061111
@@ -1168,8 +1173,6 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
11681173 }
11691174 . unwrap_or_else ( || create_file ( 0 , 0 ) ) ?;
11701175
1171- // The last field of FILE_RENAME_INFO, the file name, is unsized.
1172- // Therefore we need to subtract the size of one wide char.
11731176 let layout = core:: alloc:: Layout :: from_size_align (
11741177 struct_size as _ ,
11751178 mem:: align_of :: < c:: FILE_RENAME_INFO > ( ) ,
You can’t perform that action at this time.
0 commit comments