-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-134744: Fix fcntl error handling #134748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@serhiy-storchaka: It seems like only the main branch is affected. I wrote a patch adding tests but it doesn't check modified error paths: diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py
index e0e6782258f..e1c72c6ab7e 100644
--- a/Lib/test/test_fcntl.py
+++ b/Lib/test/test_fcntl.py
@@ -11,7 +11,7 @@
cpython_only, get_pagesize, is_apple, requires_subprocess, verbose
)
from test.support.import_helper import import_module
-from test.support.os_helper import TESTFN, unlink
+from test.support.os_helper import TESTFN, unlink, make_bad_fd
# Skip test if no fcntl module.
@@ -274,6 +274,13 @@ def test_fcntl_small_buffer(self):
def test_fcntl_large_buffer(self):
self._check_fcntl_not_mutate_len(2024)
+ @unittest.skipUnless(hasattr(fcntl, 'FICLONE'), 'need fcntl.FICLONE')
+ def test_bad_fd(self):
+ # gh-134744: Test error handling
+ fd = make_bad_fd()
+ with self.assertRaises(OSError):
+ fcntl.ioctl(fd, fcntl.F_DUPFD, 0)
+
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py
index 3c7a58aa2bc..e2f94dbf5dd 100644
--- a/Lib/test/test_ioctl.py
+++ b/Lib/test/test_ioctl.py
@@ -5,7 +5,7 @@
import threading
import unittest
from test import support
-from test.support import threading_helper
+from test.support import os_helper, threading_helper
from test.support.import_helper import import_module
fcntl = import_module('fcntl')
termios = import_module('termios')
@@ -201,6 +201,15 @@ def test_ioctl_set_window_size(self):
new_winsz = struct.unpack("HHHH", result)
self.assertEqual(new_winsz[:2], (20, 40))
+ @unittest.skipUnless(hasattr(fcntl, 'FICLONE'), 'need fcntl.FICLONE')
+ def test_bad_fd(self):
+ # gh-134744: Test error handling
+ with open(__file__) as fp:
+ fd1 = fp.fileno()
+ fd2 = os_helper.make_bad_fd()
+ with self.assertRaises(OSError):
+ fcntl.ioctl(fd1, fcntl.FICLONE, fd2)
+
if __name__ == "__main__":
unittest.main() |
@vstinner It seems that on 139 and 321 lines it may leak. |
Would you mind to elaborate? What leak? In which file? |
Sorry, I was not clear. In Lines 130 to 141 in 3c05251
As you can see result decrefed on 134, but not on 139 lines.
Lines 312 to 323 in 3c05251
Same here - it decrefed on 316, but not on 321. |
Thank you @vstinner. The code is the same in 3.14 and main, but 3.13 is not affected. It is my fail. For testing you need to pass a large bytes object (more than 1024 bytes). Simply pass Thank you @sergey-miryanov for noticing a leak. |
Ok, I added tests and I fixed the reference leak spotted by @sergey-miryanov. |
@serhiy-storchaka: Would you mind to review my fix? |
3.14 doesn't seem to be affected, the code is different. if (ret < 0) {
return !async_err ? PyErr_SetFromErrno(PyExc_OSError) : NULL;
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. 👍
Ah, so both bugs was introduced while adding a buffer overflow check. |
It would be nice to backport the tests. |
Fix also reference leak on buffer overflow. (cherry picked from commit 9300a59)
GH-134795 is a backport of this pull request to the 3.14 branch. |
I wrote #134795 to backport tests. I had to modify tests since Python 3.14 only supports buffer up to 1024 bytes. |
…ythonGH-134795) pythongh-134744: Fix fcntl error handling (pythonGH-134748) Fix also reference leak on buffer overflow. (cherry picked from commit 8a6a6f3) Co-authored-by: Victor Stinner <vstinner@python.org> (cherry picked from commit 9300a59)
_PyEval_EvalFrameDefault
originating on callingfcntl.ioctl
#134744