mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	[3.13] gh-128961: Fix exhausted array iterator crash in __setstate__() (GH-128962) (#128976)
(cherry picked from commit 4dade055f4)
Co-authored-by: Tomasz Pytel <tompytel@gmail.com>
			
			
This commit is contained in:
		
							parent
							
								
									d8a442637b
								
							
						
					
					
						commit
						c75894a36b
					
				
					 3 changed files with 19 additions and 5 deletions
				
			
		|  | @ -1665,5 +1665,13 @@ def test_tolist(self, size): | ||||||
|         self.assertEqual(ls[:8], list(example[:8])) |         self.assertEqual(ls[:8], list(example[:8])) | ||||||
|         self.assertEqual(ls[-8:], list(example[-8:])) |         self.assertEqual(ls[-8:], list(example[-8:])) | ||||||
| 
 | 
 | ||||||
|  |     def test_gh_128961(self): | ||||||
|  |         a = array.array('i') | ||||||
|  |         it = iter(a) | ||||||
|  |         list(it) | ||||||
|  |         it.__setstate__(0) | ||||||
|  |         self.assertRaises(StopIteration, next, it) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|     unittest.main() |     unittest.main() | ||||||
|  |  | ||||||
|  | @ -0,0 +1 @@ | ||||||
|  | Fix a crash when setting state on an exhausted :class:`array.array` iterator. | ||||||
|  | @ -3074,11 +3074,16 @@ array_arrayiterator___setstate__(arrayiterobject *self, PyObject *state) | ||||||
|     Py_ssize_t index = PyLong_AsSsize_t(state); |     Py_ssize_t index = PyLong_AsSsize_t(state); | ||||||
|     if (index == -1 && PyErr_Occurred()) |     if (index == -1 && PyErr_Occurred()) | ||||||
|         return NULL; |         return NULL; | ||||||
|     if (index < 0) |     arrayobject *ao = self->ao; | ||||||
|  |     if (ao != NULL) { | ||||||
|  |         if (index < 0) { | ||||||
|             index = 0; |             index = 0; | ||||||
|     else if (index > Py_SIZE(self->ao)) |         } | ||||||
|         index = Py_SIZE(self->ao); /* iterator exhausted */ |         else if (index > Py_SIZE(ao)) { | ||||||
|  |             index = Py_SIZE(ao); /* iterator exhausted */ | ||||||
|  |         } | ||||||
|         self->index = index; |         self->index = index; | ||||||
|  |     } | ||||||
|     Py_RETURN_NONE; |     Py_RETURN_NONE; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Miss Islington (bot)
						Miss Islington (bot)