Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c3a47b

Browse files
aiskblurb-it[bot]
andauthored
gh-145335: Fix crash when passing -1 as fd in os.pathconf (#145390)
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
1 parent 02288bf commit 5c3a47b

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

Lib/test/test_os/test_os.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,6 +2778,16 @@ def test_fpathconf_bad_fd(self):
27782778
self.check(os.pathconf, "PC_NAME_MAX")
27792779
self.check(os.fpathconf, "PC_NAME_MAX")
27802780

2781+
@unittest.skipUnless(hasattr(os, 'pathconf'), 'test needs os.pathconf()')
2782+
@unittest.skipIf(
2783+
support.linked_to_musl(),
2784+
'musl fpathconf ignores the file descriptor and returns a constant',
2785+
)
2786+
def test_pathconf_negative_fd_uses_fd_semantics(self):
2787+
with self.assertRaises(OSError) as ctx:
2788+
os.pathconf(-1, 1)
2789+
self.assertEqual(ctx.exception.errno, errno.EBADF)
2790+
27812791
@unittest.skipUnless(hasattr(os, 'ftruncate'), 'test needs os.ftruncate()')
27822792
def test_ftruncate(self):
27832793
self.check(os.truncate, 0)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a crash in :func:`os.pathconf` when called with ``-1`` as the path
2+
argument.

Modules/posixmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,8 @@ get_posix_state(PyObject *module)
12801280
* Contains a file descriptor if path.accept_fd was true
12811281
* and the caller provided a signed integer instead of any
12821282
* sort of string.
1283+
* path.is_fd
1284+
* True if path was provided as a file descriptor.
12831285
*
12841286
* WARNING: if your "path" parameter is optional, and is
12851287
* unspecified, path_converter will never get called.
@@ -1332,6 +1334,7 @@ typedef struct {
13321334
const wchar_t *wide;
13331335
const char *narrow;
13341336
int fd;
1337+
bool is_fd;
13351338
int value_error;
13361339
Py_ssize_t length;
13371340
PyObject *object;
@@ -1341,7 +1344,7 @@ typedef struct {
13411344
#define PATH_T_INITIALIZE(function_name, argument_name, nullable, nonstrict, \
13421345
make_wide, suppress_value_error, allow_fd) \
13431346
{function_name, argument_name, nullable, nonstrict, make_wide, \
1344-
suppress_value_error, allow_fd, NULL, NULL, -1, 0, 0, NULL, NULL}
1347+
suppress_value_error, allow_fd, NULL, NULL, -1, false, 0, 0, NULL, NULL}
13451348
#ifdef MS_WINDOWS
13461349
#define PATH_T_INITIALIZE_P(function_name, argument_name, nullable, \
13471350
nonstrict, suppress_value_error, allow_fd) \
@@ -1475,6 +1478,7 @@ path_converter(PyObject *o, void *p)
14751478
}
14761479
path->wide = NULL;
14771480
path->narrow = NULL;
1481+
path->is_fd = true;
14781482
goto success_exit;
14791483
}
14801484
else {
@@ -14328,8 +14332,9 @@ os_pathconf_impl(PyObject *module, path_t *path, int name)
1432814332

1432914333
errno = 0;
1433014334
#ifdef HAVE_FPATHCONF
14331-
if (path->fd != -1)
14335+
if (path->is_fd) {
1433214336
limit = fpathconf(path->fd, name);
14337+
}
1433314338
else
1433414339
#endif
1433514340
limit = pathconf(path->narrow, name);

0 commit comments

Comments
 (0)