Skip to content

Commit fd89315

Browse files
committed
gh-144023: prevent follow_symlinks from being allowed with an fd of 0
fd_and_follow_symlinks_invalid lets you use fd and follow_symlinks together if fd is 0. This is incorrect, as 0 is a valid fd.
1 parent f84ea11 commit fd89315

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Lib/test/test_os/test_posix.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,18 @@ def test_fstat(self):
668668
finally:
669669
fp.close()
670670

671+
@unittest.skipUnless(hasattr(posix, 'stat'),
672+
'test needs posix.stat()')
673+
@unittest.skipUnless(os.stat in os.supports_follow_symlinks,
674+
'test needs follow_symlinks support in os.stat()')
675+
def test_stat_fd_zero_follow_symlinks(self):
676+
with self.assertRaisesRegex(ValueError,
677+
'cannot use fd and follow_symlinks together'):
678+
posix.stat(0, follow_symlinks=False)
679+
with self.assertRaisesRegex(ValueError,
680+
'cannot use fd and follow_symlinks together'):
681+
posix.stat(1, follow_symlinks=False)
682+
671683
def check_statlike_path(self, func):
672684
self.assertTrue(func(os_helper.TESTFN))
673685
self.assertTrue(func(os.fsencode(os_helper.TESTFN)))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed validation of file descriptor 0 in posix functions when used with
2+
follow_symlinks parameter.

Modules/posixmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ static int
16371637
fd_and_follow_symlinks_invalid(const char *function_name, int fd,
16381638
int follow_symlinks)
16391639
{
1640-
if ((fd > 0) && (!follow_symlinks)) {
1640+
if ((fd >= 0) && (!follow_symlinks)) {
16411641
PyErr_Format(PyExc_ValueError,
16421642
"%s: cannot use fd and follow_symlinks together",
16431643
function_name);

0 commit comments

Comments
 (0)