This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Update _dirnameW to accept long path names
Type: Stage: patch review
Components: Windows Versions: Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jopamer, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: patch

Created on 2018-10-09 13:55 by jopamer, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 9769 open jopamer, 2018-10-09 14:14
Messages (1)
msg327402 - (view) Author: Joe Pamer (jopamer) * Date: 2018-10-09 13:55
The fix for issue 32557 updated os__getdiskusage_impl to use _dirnameW for obtaining the parent directory of a file. This would cause a regression if the path exceeded 260 characters, since _dirnameW currently returns -1 if given a path >= MAX_PATH in length.

As suggested in the issue's comments, _dirnameW should be updated to use PathCchRemoveFileSpec when available (on Win8.1 or greater) to avoid throwing an unnecessary error on a long path.

If PathCchRemoveFileSpec isn't available, we can call through PathRemoveFileSpecW, which is otherwise deprecated. What's interesting there is that while the docs say it expects a buffer of size MAX_PATH, analysis of the function shows that it doesn't make assumptions about the size of the path other than it's less than 32k characters in length. It calls through PathCchRemoveFileSpec under the hood on Win8 and greater, passing in 0x8000h as the Cch argument. PathCchRemoveFileSpec then scans through the path for '\' via wcschr, stops when it hits the last one and inserts a NULL. (Analysis of PathRemoveFileSpecW on a Win7 VM shows that it does basically the same thing, and is also resilient to paths greater than MAX_PATH in length.)
Date User Action Args
2022-04-11 14:59:06adminsetgithub: 79125
2018-10-09 14:14:32jopamersetkeywords: + patch
stage: patch review
pull_requests: + pull_request9157
2018-10-09 13:55:54jopamercreate