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

Skip to content

Commit 1e81a39

Browse files
committed
Issue #20025: ssl.RAND_bytes() and ssl.RAND_pseudo_bytes() now raise a
ValueError if num is negative (instead of raising a SystemError).
1 parent cb1f74e commit 1e81a39

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

Lib/test/test_ssl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ def test_random(self):
126126
else:
127127
self.assertRaises(ssl.SSLError, ssl.RAND_bytes, 16)
128128

129+
# negative num is invalid
130+
self.assertRaises(ValueError, ssl.RAND_bytes, -5)
131+
self.assertRaises(ValueError, ssl.RAND_pseudo_bytes, -5)
132+
129133
self.assertRaises(TypeError, ssl.RAND_egd, 1)
130134
self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1)
131135
ssl.RAND_add("this is a random string", 75.0)

Modules/_ssl.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2486,6 +2486,11 @@ PySSL_RAND(int len, int pseudo)
24862486
const char *errstr;
24872487
PyObject *v;
24882488

2489+
if (len < 0) {
2490+
PyErr_SetString(PyExc_ValueError, "num must be positive");
2491+
return NULL;
2492+
}
2493+
24892494
bytes = PyBytes_FromStringAndSize(NULL, len);
24902495
if (bytes == NULL)
24912496
return NULL;

0 commit comments

Comments
 (0)