bpo-33016: Fix potential use of uninitialized memory in nt._getfinalpathname by izbyshev · Pull Request #6010 · python/cpython (original) (raw)
Tangential discussion:
Because we only try VOLUME_NAME_DOS
, this function always fails for a volume that's not mounted as either a drive letter or a junction mount point. The error in this case is ERROR_PATH_NOT_FOUND
. We know that the path is valid because we have a handle (in this case the file system ensures that no parent directory in the path can be unlinked or renamed). To address this, if the flags parameter isn't already VOLUME_NAME_GUID
, it could switch to it and continue the while loop (no need to realloc). Otherwise if it's already VOLUME_NAME_GUID
or for any other error, the call should fail.
For DOS devices in paths (e.g. "C:\Temp\NUL"), CreateFile
commonly succeeds, but GetFinalPathNameByHandle
commonly fails with either ERROR_INVALID_FUNCTION
or ERROR_INVALID_PARAMETER
. What do you think about handling this failure by calling GetFullPathName
instead (e.g. "C:\Temp\NUL" => "\\.\NUL")? That way most DOS device paths won't cause this function to fail (e.g. when called by pathlib's resolve
method). We could do the same if CreateFile
fails with ERROR_INVALID_PARAMETER
, which should only occur for CON (e.g. "C:\Temp\CON") because it needs to be opened with either generic read or generic write access.
CreatFile
also commonly fails here with either ERROR_SHARING_VIOLATION
(from a paging file) or ERROR_ACCESS_DENIED
. But I think those are best handled by the caller, with a PermissionError
exception handler. Currently pathlib's resolve
method doesn't handle PermissionError
like I think it should in non-strict mode. It only handles FileNotFoundError
.