Skip to content

Commit dcd2dba

Browse files
[3.13] gh-140734: fix off-by-one error when comparing to _SUN_PATH_MAX (GH-140903) (#141182)
gh-140734: fix off-by-one error when comparing to `_SUN_PATH_MAX` (GH-140903) The limit includes a NULL terminator. (cherry picked from commit 9a19900) Co-authored-by: Bénédikt Tran <[email protected]>
1 parent e753887 commit dcd2dba

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

Lib/multiprocessing/util.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,14 @@ def is_abstract_socket_namespace(address):
126126
# Function returning a temp directory which will be removed on exit
127127
#
128128

129-
# Maximum length of a socket file path is usually between 92 and 108 [1],
130-
# but Linux is known to use a size of 108 [2]. BSD-based systems usually
131-
# use a size of 104 or 108 and Windows does not create AF_UNIX sockets.
129+
# Maximum length of a NULL-terminated [1] socket file path is usually
130+
# between 92 and 108 [2], but Linux is known to use a size of 108 [3].
131+
# BSD-based systems usually use a size of 104 or 108 and Windows does
132+
# not create AF_UNIX sockets.
132133
#
133-
# [1]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_un.h.html
134-
# [2]: https://man7.org/linux/man-pages/man7/unix.7.html.
134+
# [1]: https://github.com/python/cpython/issues/140734
135+
# [2]: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_un.h.html
136+
# [3]: https://man7.org/linux/man-pages/man7/unix.7.html
135137

136138
if sys.platform == 'linux':
137139
_SUN_PATH_MAX = 108
@@ -171,11 +173,13 @@ def _get_base_temp_dir(tempfile):
171173
# generated by tempfile._RandomNameSequence, which, by design,
172174
# is 8 characters long.
173175
#
174-
# Thus, the length of socket filename will be:
176+
# Thus, the socket file path length (without NULL terminator) will be:
175177
#
176178
# len(base_tempdir + '/pymp-XXXXXXXX' + '/sock-XXXXXXXX')
177179
sun_path_len = len(base_tempdir) + 14 + 14
178-
if sun_path_len <= _SUN_PATH_MAX:
180+
# Strict inequality to account for the NULL terminator.
181+
# See https://github.com/python/cpython/issues/140734.
182+
if sun_path_len < _SUN_PATH_MAX:
179183
return base_tempdir
180184
# Fallback to the default system-wide temporary directory.
181185
# This ignores user-defined environment variables.
@@ -201,7 +205,7 @@ def _get_base_temp_dir(tempfile):
201205
return base_tempdir
202206
_warn("Ignoring user-defined temporary directory: %s", base_tempdir)
203207
# at most max(map(len, dirlist)) + 14 + 14 = 36 characters
204-
assert len(base_system_tempdir) + 14 + 14 <= _SUN_PATH_MAX
208+
assert len(base_system_tempdir) + 14 + 14 < _SUN_PATH_MAX
205209
return base_system_tempdir
206210

207211
def get_temp_dir():
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`multiprocessing`: fix off-by-one error when checking the length
2+
of a temporary socket file path. Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)