mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	PyUnicode_WriteChar() raises IndexError on invalid index
PyUnicode_WriteChar() raises also a ValueError if the string has more than 1 reference.
This commit is contained in:
		
							parent
							
								
									2fe5ced752
								
							
						
					
					
						commit
						cd9950fd09
					
				
					 2 changed files with 24 additions and 8 deletions
				
			
		|  | @ -647,7 +647,9 @@ PyAPI_FUNC(Py_UCS4) PyUnicode_ReadChar( | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
| /* Write a character to the string. The string must have been created through
 | /* Write a character to the string. The string must have been created through
 | ||||||
|    PyUnicode_New, must not be shared, and must not have been hashed yet. */ |    PyUnicode_New, must not be shared, and must not have been hashed yet. | ||||||
|  | 
 | ||||||
|  |    Return 0 on success, -1 on error. */ | ||||||
| 
 | 
 | ||||||
| PyAPI_FUNC(int) PyUnicode_WriteChar( | PyAPI_FUNC(int) PyUnicode_WriteChar( | ||||||
|     PyObject *unicode, |     PyObject *unicode, | ||||||
|  |  | ||||||
|  | @ -622,6 +622,19 @@ unicode_convert_wchar_to_ucs4(const wchar_t *begin, const wchar_t *end, | ||||||
| } | } | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
|  | static int | ||||||
|  | _PyUnicode_Dirty(PyObject *unicode) | ||||||
|  | { | ||||||
|  |     assert(PyUnicode_Check(unicode)); | ||||||
|  |     if (Py_REFCNT(unicode) != 1) { | ||||||
|  |         PyErr_SetString(PyExc_ValueError, | ||||||
|  |                         "Cannot modify a string having more than 1 reference"); | ||||||
|  |         return -1; | ||||||
|  |     } | ||||||
|  |     _PyUnicode_DIRTY(unicode); | ||||||
|  |     return 0; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| Py_ssize_t | Py_ssize_t | ||||||
| PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, | PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, | ||||||
|                          PyObject *from, Py_ssize_t from_start, |                          PyObject *from, Py_ssize_t from_start, | ||||||
|  | @ -651,12 +664,8 @@ PyUnicode_CopyCharacters(PyObject *to, Py_ssize_t to_start, | ||||||
|     if (how_many == 0) |     if (how_many == 0) | ||||||
|         return 0; |         return 0; | ||||||
| 
 | 
 | ||||||
|     if (Py_REFCNT(to) != 1) { |     if (_PyUnicode_Dirty(to)) | ||||||
|         PyErr_SetString(PyExc_ValueError, |  | ||||||
|                         "Cannot modify a string having more than 1 reference"); |  | ||||||
|         return -1; |         return -1; | ||||||
|     } |  | ||||||
|     _PyUnicode_DIRTY(to); |  | ||||||
| 
 | 
 | ||||||
|     from_kind = PyUnicode_KIND(from); |     from_kind = PyUnicode_KIND(from); | ||||||
|     from_data = PyUnicode_DATA(from); |     from_data = PyUnicode_DATA(from); | ||||||
|  | @ -2855,10 +2864,15 @@ int | ||||||
| PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) | PyUnicode_WriteChar(PyObject *unicode, Py_ssize_t index, Py_UCS4 ch) | ||||||
| { | { | ||||||
|     if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { |     if (!PyUnicode_Check(unicode) || !PyUnicode_IS_COMPACT(unicode)) { | ||||||
|         return PyErr_BadArgument(); |         PyErr_BadArgument(); | ||||||
|         return -1; |         return -1; | ||||||
|     } |     } | ||||||
| 
 |     if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) { | ||||||
|  |         PyErr_SetString(PyExc_IndexError, "string index out of range"); | ||||||
|  |         return -1; | ||||||
|  |     } | ||||||
|  |     if (_PyUnicode_Dirty(unicode)) | ||||||
|  |         return -1; | ||||||
|     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), |     PyUnicode_WRITE(PyUnicode_KIND(unicode), PyUnicode_DATA(unicode), | ||||||
|                     index, ch); |                     index, ch); | ||||||
|     return 0; |     return 0; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner