Skip to content

Commit 479d96c

Browse files
committed
Issue python#29416: Prevent infinite loop in pathlib.Path.mkdir
2 parents ef51767 + 1add96f commit 479d96c

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Lib/pathlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
12351235
if not exist_ok or not self.is_dir():
12361236
raise
12371237
except OSError as e:
1238-
if e.errno != ENOENT:
1238+
if e.errno != ENOENT or self.parent == self:
12391239
raise
12401240
self.parent.mkdir(parents=True)
12411241
self._accessor.mkdir(self, mode)

Lib/test/test_pathlib.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,17 @@ def test_mkdir_exist_ok_with_parent(self):
17761776
self.assertTrue(p.exists())
17771777
self.assertEqual(p.stat().st_ctime, st_ctime_first)
17781778

1779+
@only_nt # XXX: not sure how to test this on POSIX
1780+
def test_mkdir_with_unknown_drive(self):
1781+
for d in 'ZYXWVUTSRQPONMLKJIHGFEDCBA':
1782+
p = self.cls(d + ':\\')
1783+
if not p.is_dir():
1784+
break
1785+
else:
1786+
self.skipTest("cannot find a drive that doesn't exist")
1787+
with self.assertRaises(OSError):
1788+
(p / 'child' / 'path').mkdir(parents=True)
1789+
17791790
def test_mkdir_with_child_file(self):
17801791
p = self.cls(BASE, 'dirB', 'fileB')
17811792
self.assertTrue(p.exists())

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ Extension Modules
223223
Library
224224
-------
225225

226+
- Issue #29416: Prevent infinite loop in pathlib.Path.mkdir
227+
226228
- Issue #29444: Fixed out-of-bounds buffer access in the group() method of
227229
the match object. Based on patch by WGH.
228230

0 commit comments

Comments
 (0)