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

Skip to content

BUG: avoid seg fault from OOB access in RandomState.set_state() #25426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions numpy/random/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,16 @@ cdef class RandomState:
else:
if not isinstance(state, (tuple, list)):
raise TypeError('state must be a dict or a tuple.')
if state[0] != 'MT19937':
raise ValueError('set_state can only be used with legacy MT19937'
'state instances.')
st = {'bit_generator': state[0],
'state': {'key': state[1], 'pos': state[2]}}
if len(state) > 3:
st['has_gauss'] = state[3]
st['gauss'] = state[4]
value = st
with cython.boundscheck(True):
if state[0] != 'MT19937':
raise ValueError('set_state can only be used with legacy '
'MT19937 state instances.')
st = {'bit_generator': state[0],
'state': {'key': state[1], 'pos': state[2]}}
if len(state) > 3:
st['has_gauss'] = state[3]
st['gauss'] = state[4]
value = st

self._aug_state.gauss = st.get('gauss', 0.0)
self._aug_state.has_gauss = st.get('has_gauss', 0)
Expand Down
5 changes: 5 additions & 0 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ def test_negative_binomial(self):
# arguments without truncation.
self.prng.negative_binomial(0.5, 0.5)

def test_set_invalid_state(self):
# gh-25402
with pytest.raises(IndexError):
self.prng.set_state(())


class TestRandint:

Expand Down