https://github.com/python/cpython/commit/9668d260a197207b440c8e65f09f4cc24b9cd566
commit: 9668d260a197207b440c8e65f09f4cc24b9cd566
branch: main
author: petervanvugt <[email protected]>
committer: encukou <[email protected]>
date: 2026-05-01T17:37:36+02:00
summary:

gh-148850: Fix memory sanitizer false positive in os.getrandom (GH-148851)


Co-authored-by: Petr Viktorin <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-04-21-19-29-29.gh-issue-148850.MSH0J_.rst
M Include/pyport.h
M Modules/posixmodule.c

diff --git a/Include/pyport.h b/Include/pyport.h
index c975921beafb9e..73a3e6cdaf0920 100644
--- a/Include/pyport.h
+++ b/Include/pyport.h
@@ -553,6 +553,7 @@ extern "C" {
 #    if !defined(_Py_MEMORY_SANITIZER)
 #      define _Py_MEMORY_SANITIZER
 #      define _Py_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
+#      define _Py_MSAN_UNPOISON(PTR, SIZE)  (__msan_unpoison(PTR, SIZE))
 #    endif
 #  endif
 #  if __has_feature(address_sanitizer)
@@ -591,6 +592,9 @@ extern "C" {
 #ifndef _Py_NO_SANITIZE_MEMORY
 #  define _Py_NO_SANITIZE_MEMORY
 #endif
+#ifndef _Py_MSAN_UNPOISON
+#  define _Py_MSAN_UNPOISON(PTR, SIZE)
+#endif
 
 /* AIX has __bool__ redefined in it's system header file. */
 #if defined(_AIX) && defined(__bool__)
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-04-21-19-29-29.gh-issue-148850.MSH0J_.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-21-19-29-29.gh-issue-148850.MSH0J_.rst
new file mode 100644
index 00000000000000..324d1610310158
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-04-21-19-29-29.gh-issue-148850.MSH0J_.rst
@@ -0,0 +1 @@
+Fix the memory sanitizer false positive in :func:`os.getrandom`.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index e5ce487723b25b..3bdbf2ef81679e 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -17195,6 +17195,8 @@ os_getrandom_impl(PyObject *module, Py_ssize_t size, 
int flags)
         goto error;
     }
 
+    _Py_MSAN_UNPOISON(data, size);
+
     return PyBytesWriter_FinishWithSize(writer, n);
 
 error:

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to