@@ -1152,7 +1152,7 @@ win32_stat(const char* path, struct win32_stat *result)
11521152 NULL , /* security attributes */
11531153 OPEN_EXISTING ,
11541154 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
1155- FILE_FLAG_BACKUP_SEMANTICS ,
1155+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS ,
11561156 NULL );
11571157
11581158 if (hFile == INVALID_HANDLE_VALUE ) {
@@ -1175,22 +1175,32 @@ win32_stat(const char* path, struct win32_stat *result)
11751175 }
11761176 code = attribute_data_to_stat (& info , result );
11771177 }
1178-
1179- buf_size = Py_GetFinalPathNameByHandleA (hFile , 0 , 0 , VOLUME_NAME_DOS );
1180- if (!buf_size ) return -1 ;
1181- target_path = (char * )malloc ((buf_size + 1 )* sizeof (char ));
1182- result_length = Py_GetFinalPathNameByHandleA (hFile , target_path ,
1183- buf_size , VOLUME_NAME_DOS );
1184-
1185- if (!result_length )
1186- return -1 ;
1178+ else {
1179+ /* We have a good handle to the target, use it to determine the target
1180+ path name (then we'll call lstat on it). */
1181+ buf_size = Py_GetFinalPathNameByHandleA (hFile , 0 , 0 , VOLUME_NAME_DOS );
1182+ if (!buf_size ) return -1 ;
1183+ /* Due to a slight discrepancy between GetFinalPathNameByHandleA
1184+ and GetFinalPathNameByHandleW, we must allocate one more byte
1185+ than reported. */
1186+ target_path = (char * )malloc ((buf_size + 2 )* sizeof (char ));
1187+ result_length = Py_GetFinalPathNameByHandleA (hFile , target_path ,
1188+ buf_size + 1 , VOLUME_NAME_DOS );
1189+
1190+ if (!result_length ) {
1191+ free (target_path );
1192+ return -1 ;
1193+ }
11871194
1188- if (!CloseHandle (hFile ))
1189- return -1 ;
1195+ if (!CloseHandle (hFile )) {
1196+ free (target_path );
1197+ return -1 ;
1198+ }
11901199
1191- target_path [result_length ] = 0 ;
1192- code = win32_lstat (target_path , result );
1193- free (target_path );
1200+ target_path [result_length ] = 0 ;
1201+ code = win32_lstat (target_path , result );
1202+ free (target_path );
1203+ }
11941204
11951205 return code ;
11961206}
@@ -1254,11 +1264,15 @@ win32_stat_w(const wchar_t* path, struct win32_stat *result)
12541264 result_length = Py_GetFinalPathNameByHandleW (hFile , target_path ,
12551265 buf_size , VOLUME_NAME_DOS );
12561266
1257- if (!result_length )
1267+ if (!result_length ) {
1268+ free (target_path );
12581269 return -1 ;
1270+ }
12591271
1260- if (!CloseHandle (hFile ))
1272+ if (!CloseHandle (hFile )) {
1273+ free (target_path );
12611274 return -1 ;
1275+ }
12621276
12631277 target_path [result_length ] = 0 ;
12641278 code = win32_lstat_w (target_path , result );
0 commit comments