@@ -1227,8 +1227,13 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
1227
1227
1228
1228
let new_len_without_nul_in_bytes = ( new. len ( ) - 1 ) . try_into ( ) . unwrap ( ) ;
1229
1229
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 > ( ) ) ;
1232
1237
1233
1238
let struct_size: u32 = struct_size. try_into ( ) . unwrap ( ) ;
1234
1239
@@ -1296,8 +1301,6 @@ pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
1296
1301
}
1297
1302
. unwrap_or_else ( || create_file ( 0 , 0 ) ) ?;
1298
1303
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.
1301
1304
let layout = core:: alloc:: Layout :: from_size_align (
1302
1305
struct_size as _ ,
1303
1306
mem:: align_of :: < c:: FILE_RENAME_INFO > ( ) ,
0 commit comments