summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-06-04 17:12:13 (GMT)
committerGitHub <noreply@github.com>2024-06-04 17:12:13 (GMT)
commitf98ec7574a5ee3dc5db996a5037483f57465e18d (patch)
tree721eea5c4417390ad42e92b5aed18adad2eac90b
parent0e150c36dee1734a6a60f1ebd924e90fb3fe1458 (diff)
downloadcpython-f98ec7574a5ee3dc5db996a5037483f57465e18d.zip
cpython-f98ec7574a5ee3dc5db996a5037483f57465e18d.tar.gz
cpython-f98ec7574a5ee3dc5db996a5037483f57465e18d.tar.bz2
[3.12] gh-89928: Fix integer conversion of device numbers (GH-31794) (GH-120054)
Fix os.major(), os.minor() and os.makedev(). Support device numbers larger than 2**63-1. Support non-existent device number (NODEV). (cherry picked from commit 7111d9605f9db7aa0b095bb8ece7ccc0b8115c3f)
-rw-r--r--Lib/test/test_posix.py15
-rw-r--r--Misc/NEWS.d/next/Library/2022-03-10-16-47-57.bpo-45767.ywmyo1.rst3
-rw-r--r--Modules/clinic/posixmodule.c.h32
-rw-r--r--Modules/posixmodule.c88
4 files changed, 99 insertions, 39 deletions
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index f115aa8..7ed45ac 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -703,7 +703,8 @@ class PosixTester(unittest.TestCase):
self.assertEqual(posix.major(dev), major)
self.assertRaises(TypeError, posix.major, float(dev))
self.assertRaises(TypeError, posix.major)
- self.assertRaises((ValueError, OverflowError), posix.major, -1)
+ for x in -2, 2**64, -2**63-1:
+ self.assertRaises((ValueError, OverflowError), posix.major, x)
minor = posix.minor(dev)
self.assertIsInstance(minor, int)
@@ -711,13 +712,23 @@ class PosixTester(unittest.TestCase):
self.assertEqual(posix.minor(dev), minor)
self.assertRaises(TypeError, posix.minor, float(dev))
self.assertRaises(TypeError, posix.minor)
- self.assertRaises((ValueError, OverflowError), posix.minor, -1)
+ for x in -2, 2**64, -2**63-1:
+ self.assertRaises((ValueError, OverflowError), posix.minor, x)
self.assertEqual(posix.makedev(major, minor), dev)
self.assertRaises(TypeError, posix.makedev, float(major), minor)
self.assertRaises(TypeError, posix.makedev, major, float(minor))
self.assertRaises(TypeError, posix.makedev, major)
self.assertRaises(TypeError, posix.makedev)
+ for x in -2, 2**32, 2**64, -2**63-1:
+ self.assertRaises((ValueError, OverflowError), posix.makedev, x, minor)
+ self.assertRaises((ValueError, OverflowError), posix.makedev, major, x)
+
+ if sys.platform == 'linux':
+ NODEV = -1
+ self.assertEqual(posix.major(NODEV), NODEV)
+ self.assertEqual(posix.minor(NODEV), NODEV)
+ self.assertEqual(posix.makedev(NODEV, NODEV), NODEV)
def _test_all_chown_common(self, chown_func, first_param, stat_func):
"""Common code for chown, fchown and lchown tests."""
diff --git a/Misc/NEWS.d/next/Library/2022-03-10-16-47-57.bpo-45767.ywmyo1.rst b/Misc/NEWS.d/next/Library/2022-03-10-16-47-57.bpo-45767.ywmyo1.rst
new file mode 100644
index 0000000..0cdf1e8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-03-10-16-47-57.bpo-45767.ywmyo1.rst
@@ -0,0 +1,3 @@
+Fix integer conversion in :func:`os.major`, :func:`os.minor`, and
+:func:`os.makedev`. Support device numbers larger than ``2**63-1``. Support
+non-existent device number (``NODEV``).
diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h
index c832ae3..02cb95a 100644
--- a/Modules/clinic/posixmodule.c.h
+++ b/Modules/clinic/posixmodule.c.h
@@ -7958,7 +7958,7 @@ PyDoc_STRVAR(os_major__doc__,
#define OS_MAJOR_METHODDEF \
{"major", (PyCFunction)os_major, METH_O, os_major__doc__},
-static unsigned int
+static PyObject *
os_major_impl(PyObject *module, dev_t device);
static PyObject *
@@ -7966,16 +7966,11 @@ os_major(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
dev_t device;
- unsigned int _return_value;
if (!_Py_Dev_Converter(arg, &device)) {
goto exit;
}
- _return_value = os_major_impl(module, device);
- if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
- goto exit;
- }
- return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
+ return_value = os_major_impl(module, device);
exit:
return return_value;
@@ -7994,7 +7989,7 @@ PyDoc_STRVAR(os_minor__doc__,
#define OS_MINOR_METHODDEF \
{"minor", (PyCFunction)os_minor, METH_O, os_minor__doc__},
-static unsigned int
+static PyObject *
os_minor_impl(PyObject *module, dev_t device);
static PyObject *
@@ -8002,16 +7997,11 @@ os_minor(PyObject *module, PyObject *arg)
{
PyObject *return_value = NULL;
dev_t device;
- unsigned int _return_value;
if (!_Py_Dev_Converter(arg, &device)) {
goto exit;
}
- _return_value = os_minor_impl(module, device);
- if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
- goto exit;
- }
- return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
+ return_value = os_minor_impl(module, device);
exit:
return return_value;
@@ -8031,25 +8021,23 @@ PyDoc_STRVAR(os_makedev__doc__,
{"makedev", _PyCFunction_CAST(os_makedev), METH_FASTCALL, os_makedev__doc__},
static dev_t
-os_makedev_impl(PyObject *module, int major, int minor);
+os_makedev_impl(PyObject *module, dev_t major, dev_t minor);
static PyObject *
os_makedev(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
{
PyObject *return_value = NULL;
- int major;
- int minor;
+ dev_t major;
+ dev_t minor;
dev_t _return_value;
if (!_PyArg_CheckPositional("makedev", nargs, 2, 2)) {
goto exit;
}
- major = _PyLong_AsInt(args[0]);
- if (major == -1 && PyErr_Occurred()) {
+ if (!_Py_Dev_Converter(args[0], &major)) {
goto exit;
}
- minor = _PyLong_AsInt(args[1]);
- if (minor == -1 && PyErr_Occurred()) {
+ if (!_Py_Dev_Converter(args[1], &minor)) {
goto exit;
}
_return_value = os_makedev_impl(module, major, minor);
@@ -12014,4 +12002,4 @@ exit:
#ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#define OS_WAITSTATUS_TO_EXITCODE_METHODDEF
#endif /* !defined(OS_WAITSTATUS_TO_EXITCODE_METHODDEF) */
-/*[clinic end generated code: output=485aa0aed01eb699 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=e2cf3ab750346780 input=a9049054013a1b77]*/
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index abd737d..9cc53c8 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -19,6 +19,7 @@
#include "pycore_fileutils.h" // _Py_closerange()
#include "pycore_import.h" // _PyImport_ReInitLock()
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
+#include "pycore_long.h" // _PyLong_IsNegative()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_object.h" // _PyObject_LookupSpecial()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
@@ -920,16 +921,46 @@ fail:
#endif /* MS_WINDOWS */
-#define _PyLong_FromDev PyLong_FromLongLong
+static PyObject *
+_PyLong_FromDev(dev_t dev)
+{
+#ifdef NODEV
+ if (dev == NODEV) {
+ return PyLong_FromLongLong((long long)dev);
+ }
+#endif
+ return PyLong_FromUnsignedLongLong((unsigned long long)dev);
+}
#if (defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)) || defined(HAVE_DEVICE_MACROS)
static int
_Py_Dev_Converter(PyObject *obj, void *p)
{
- *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
- if (PyErr_Occurred())
+#ifdef NODEV
+ if (PyLong_Check(obj) && _PyLong_IsNegative((PyLongObject *)obj)) {
+ int overflow;
+ long long result = PyLong_AsLongLongAndOverflow(obj, &overflow);
+ if (result == -1 && PyErr_Occurred()) {
+ return 0;
+ }
+ if (!overflow && result == (long long)NODEV) {
+ *((dev_t *)p) = NODEV;
+ return 1;
+ }
+ }
+#endif
+
+ unsigned long long result = PyLong_AsUnsignedLongLong(obj);
+ if (result == (unsigned long long)-1 && PyErr_Occurred()) {
+ return 0;
+ }
+ if ((unsigned long long)(dev_t)result != result) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C dev_t");
return 0;
+ }
+ *((dev_t *)p) = (dev_t)result;
return 1;
}
#endif /* (HAVE_MKNOD && HAVE_MAKEDEV) || HAVE_DEVICE_MACROS */
@@ -11752,9 +11783,31 @@ os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
+static PyObject *
+major_minor_conv(unsigned int value)
+{
+#ifdef NODEV
+ if (value == (unsigned int)NODEV) {
+ return PyLong_FromLong((int)NODEV);
+ }
+#endif
+ return PyLong_FromUnsignedLong(value);
+}
+
+static int
+major_minor_check(dev_t value)
+{
+#ifdef NODEV
+ if (value == NODEV) {
+ return 1;
+ }
+#endif
+ return (dev_t)(unsigned int)value == value;
+}
+
#ifdef HAVE_DEVICE_MACROS
/*[clinic input]
-os.major -> unsigned_int
+os.major
device: dev_t
/
@@ -11762,16 +11815,16 @@ os.major -> unsigned_int
Extracts a device major number from a raw device number.
[clinic start generated code]*/
-static unsigned int
+static PyObject *
os_major_impl(PyObject *module, dev_t device)
-/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
+/*[clinic end generated code: output=4071ffee17647891 input=b1a0a14ec9448229]*/
{
- return major(device);
+ return major_minor_conv(major(device));
}
/*[clinic input]
-os.minor -> unsigned_int
+os.minor
device: dev_t
/
@@ -11779,28 +11832,33 @@ os.minor -> unsigned_int
Extracts a device minor number from a raw device number.
[clinic start generated code]*/
-static unsigned int
+static PyObject *
os_minor_impl(PyObject *module, dev_t device)
-/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
+/*[clinic end generated code: output=306cb78e3bc5004f input=2f686e463682a9da]*/
{
- return minor(device);
+ return major_minor_conv(minor(device));
}
/*[clinic input]
os.makedev -> dev_t
- major: int
- minor: int
+ major: dev_t
+ minor: dev_t
/
Composes a raw device number from the major and minor device numbers.
[clinic start generated code]*/
static dev_t
-os_makedev_impl(PyObject *module, int major, int minor)
-/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
+os_makedev_impl(PyObject *module, dev_t major, dev_t minor)
+/*[clinic end generated code: output=cad6125c51f5af80 input=2146126ec02e55c1]*/
{
+ if (!major_minor_check(major) || !major_minor_check(minor)) {
+ PyErr_SetString(PyExc_OverflowError,
+ "Python int too large to convert to C unsigned int");
+ return (dev_t)-1;
+ }
return makedev(major, minor);
}
#endif /* HAVE_DEVICE_MACROS */