Skip to content

A feature-rich command-line audio/video downloader

[utils] `setproctitle`: Fix NUL termination (#17724)

doe108039b31b2755ad
diff --git a/test/test_utils.py b/test/test_utils.py
index 7d34fded35..971f68bbec 100644
--- a/test/test_utils.py
+++ b/test/test_utils.py
@@ -107,6 +107,7 @@ from yt_dlp.utils import (
sanitize_filename,
sanitize_path,
sanitize_url,
+ setproctitle,
shell_quote,
strftime_or_none,
smuggle_url,
@@ -613,6 +614,14 @@ class TestUtil(unittest.TestCase):
self.assertEqual(res_url, url)
self.assertEqual(res_data, {'a': 'b', 'c': 'd'})
+ @unittest.mock.patch('ctypes.cdll.LoadLibrary')
+ def test_setproctitle(self, load_library):
+ for title in ('yt-dlp', 'café'):
+ with self.subTest(title=title):
+ setproctitle(title)
+ buf = load_library.return_value.prctl.call_args.args[1]
+ self.assertEqual(bytes(buf), title.encode() + b'\0')
+
def test_shell_quote(self):
args = ['ffmpeg', '-i', 'ñ€ß\'.mp4']
self.assertEqual(
diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py
index 0fcacb9364..b8a7bc447c 100644
--- a/yt_dlp/utils/_utils.py
+++ b/yt_dlp/utils/_utils.py
@@ -1959,9 +1959,7 @@ def setproctitle(title):
except OSError:
return
- title_bytes = title.encode()
- buf = ctypes.create_string_buffer(len(title_bytes))
- buf.value = title_bytes
+ buf = ctypes.create_string_buffer(title.encode())
try:
# PR_SET_NAME = 15 Ref: /usr/include/linux/prctl.h
libc.prctl(15, buf, 0, 0, 0)