Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d24271

Browse files
committed
Fix os.urandom() on Solaris 11.3
Issue #26735: Fix os.urandom() on Solaris 11.3 and newer when reading more than 1,024 bytes: call getrandom() multiple times with a limit of 1024 bytes per call.
1 parent c6ec54d commit 9d24271

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ Core and Builtins
102102
Library
103103
-------
104104

105+
- Issue #26735: Fix :func:`os.urandom` on Solaris 11.3 and newer when reading
106+
more than 1,024 bytes: call ``getrandom()`` multiple times with a limit of
107+
1024 bytes per call.
108+
105109
- Issue #16329: Add .webm to mimetypes.types_map. Patch by Giampaolo Rodola'.
106110

107111
- Issue #13952: Add .csv to mimetypes.types_map. Patch by Geoff Wilson.

Python/random.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,28 +131,35 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise)
131131
return 0;
132132

133133
while (0 < size) {
134-
errno = 0;
134+
#ifdef sun
135+
/* Issue #26735: On Solaris, getrandom() is limited to returning up
136+
to 1024 bytes */
137+
n = Py_MIN(size, 1024);
138+
#else
139+
n = size;
140+
#endif
135141

142+
errno = 0;
136143
#ifdef HAVE_GETRANDOM
137144
if (raise) {
138145
Py_BEGIN_ALLOW_THREADS
139-
n = getrandom(buffer, size, flags);
146+
n = getrandom(buffer, n, flags);
140147
Py_END_ALLOW_THREADS
141148
}
142149
else {
143-
n = getrandom(buffer, size, flags);
150+
n = getrandom(buffer, n, flags);
144151
}
145152
#else
146153
/* On Linux, use the syscall() function because the GNU libc doesn't
147154
* expose the Linux getrandom() syscall yet. See:
148155
* https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
149156
if (raise) {
150157
Py_BEGIN_ALLOW_THREADS
151-
n = syscall(SYS_getrandom, buffer, size, flags);
158+
n = syscall(SYS_getrandom, buffer, n, flags);
152159
Py_END_ALLOW_THREADS
153160
}
154161
else {
155-
n = syscall(SYS_getrandom, buffer, size, flags);
162+
n = syscall(SYS_getrandom, buffer, n, flags);
156163
}
157164
#endif
158165

0 commit comments

Comments
 (0)