]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-96819: multiprocessing.resource_tracker: check if length of pipe write <= 512...
authorKoki Saito <49419225+saito828koki@users.noreply.github.com>
Mon, 3 Oct 2022 00:41:01 +0000 (09:41 +0900)
committerGitHub <noreply@github.com>
Mon, 3 Oct 2022 00:41:01 +0000 (17:41 -0700)
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Lib/multiprocessing/resource_tracker.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2022-09-17-13-15-10.gh-issue-96819.6RfqM7.rst [new file with mode: 0644]

index cc42dbdda05b913efd43cbe988773a08bc177e05..ea369507297f86ca97ff289712da340a3b51fdf3 100644 (file)
@@ -161,10 +161,10 @@ class ResourceTracker(object):
     def _send(self, cmd, name, rtype):
         self.ensure_running()
         msg = '{0}:{1}:{2}\n'.format(cmd, name, rtype).encode('ascii')
-        if len(name) > 512:
+        if len(msg) > 512:
             # posix guarantees that writes to a pipe of less than PIPE_BUF
             # bytes are atomic, and that PIPE_BUF >= 512
-            raise ValueError('name too long')
+            raise ValueError('msg too long')
         nbytes = os.write(self._fd, msg)
         assert nbytes == len(msg), "nbytes {0:n} but len(msg) {1:n}".format(
             nbytes, len(msg))
index a35e527eea4400d9f1ebdb56ac6a7dc5d36d02f6..f74d8d7fbd726cf92a6d9684711e49f559fcea55 100644 (file)
@@ -5432,6 +5432,14 @@ class TestResourceTracker(unittest.TestCase):
 
         self.assertTrue(is_resource_tracker_reused)
 
+    def test_too_long_name_resource(self):
+        # gh-96819: Resource names that will make the length of a write to a pipe
+        # greater than PIPE_BUF are not allowed
+        rtype = "shared_memory"
+        too_long_name_resource = "a" * (512 - len(rtype))
+        with self.assertRaises(ValueError):
+            resource_tracker.register(too_long_name_resource, rtype)
+
 
 class TestSimpleQueue(unittest.TestCase):
 
diff --git a/Misc/NEWS.d/next/Library/2022-09-17-13-15-10.gh-issue-96819.6RfqM7.rst b/Misc/NEWS.d/next/Library/2022-09-17-13-15-10.gh-issue-96819.6RfqM7.rst
new file mode 100644 (file)
index 0000000..07b62a8
--- /dev/null
@@ -0,0 +1 @@
+Fixed check in :mod:`multiprocessing.resource_tracker` that guarantees that the length of a write to a pipe is not greater than ``PIPE_BUF``.