mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	bpo-30708: Check for null characters in PyUnicode_AsWideCharString(). (#2285)
Raise a ValueError if the second argument is NULL and the wchar_t\* string contains null characters.
This commit is contained in:
		
							parent
							
								
									65474b9d7a
								
							
						
					
					
						commit
						e613e6add5
					
				
					 6 changed files with 44 additions and 42 deletions
				
			
		|  | @ -2953,8 +2953,7 @@ PyUnicode_FromFormat(const char *format, ...) | |||
| 
 | ||||
| #ifdef HAVE_WCHAR_H | ||||
| 
 | ||||
| /* Helper function for PyUnicode_AsWideChar() and PyUnicode_AsWideCharString():
 | ||||
|    convert a Unicode object to a wide character string. | ||||
| /* Convert a Unicode object to a wide character string.
 | ||||
| 
 | ||||
|    - If w is NULL: return the number of wide characters (including the null | ||||
|      character) required to convert the unicode object. Ignore size argument. | ||||
|  | @ -2962,14 +2961,18 @@ PyUnicode_FromFormat(const char *format, ...) | |||
|    - Otherwise: return the number of wide characters (excluding the null | ||||
|      character) written into w. Write at most size wide characters (including | ||||
|      the null character). */ | ||||
| static Py_ssize_t | ||||
| unicode_aswidechar(PyObject *unicode, | ||||
|                    wchar_t *w, | ||||
|                    Py_ssize_t size) | ||||
| Py_ssize_t | ||||
| PyUnicode_AsWideChar(PyObject *unicode, | ||||
|                      wchar_t *w, | ||||
|                      Py_ssize_t size) | ||||
| { | ||||
|     Py_ssize_t res; | ||||
|     const wchar_t *wstr; | ||||
| 
 | ||||
|     if (unicode == NULL) { | ||||
|         PyErr_BadInternalCall(); | ||||
|         return -1; | ||||
|     } | ||||
|     wstr = PyUnicode_AsUnicodeAndSize(unicode, &res); | ||||
|     if (wstr == NULL) | ||||
|         return -1; | ||||
|  | @ -2986,23 +2989,12 @@ unicode_aswidechar(PyObject *unicode, | |||
|         return res + 1; | ||||
| } | ||||
| 
 | ||||
| Py_ssize_t | ||||
| PyUnicode_AsWideChar(PyObject *unicode, | ||||
|                      wchar_t *w, | ||||
|                      Py_ssize_t size) | ||||
| { | ||||
|     if (unicode == NULL) { | ||||
|         PyErr_BadInternalCall(); | ||||
|         return -1; | ||||
|     } | ||||
|     return unicode_aswidechar(unicode, w, size); | ||||
| } | ||||
| 
 | ||||
| wchar_t* | ||||
| PyUnicode_AsWideCharString(PyObject *unicode, | ||||
|                            Py_ssize_t *size) | ||||
| { | ||||
|     wchar_t* buffer; | ||||
|     const wchar_t *wstr; | ||||
|     wchar_t *buffer; | ||||
|     Py_ssize_t buflen; | ||||
| 
 | ||||
|     if (unicode == NULL) { | ||||
|  | @ -3010,19 +3002,22 @@ PyUnicode_AsWideCharString(PyObject *unicode, | |||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     buflen = unicode_aswidechar(unicode, NULL, 0); | ||||
|     if (buflen == -1) | ||||
|     wstr = PyUnicode_AsUnicodeAndSize(unicode, &buflen); | ||||
|     if (wstr == NULL) { | ||||
|         return NULL; | ||||
|     buffer = PyMem_NEW(wchar_t, buflen); | ||||
|     } | ||||
|     if (size == NULL && wcslen(wstr) != (size_t)buflen) { | ||||
|         PyErr_SetString(PyExc_ValueError, | ||||
|                         "embedded null character"); | ||||
|         return NULL; | ||||
|     } | ||||
| 
 | ||||
|     buffer = PyMem_NEW(wchar_t, buflen + 1); | ||||
|     if (buffer == NULL) { | ||||
|         PyErr_NoMemory(); | ||||
|         return NULL; | ||||
|     } | ||||
|     buflen = unicode_aswidechar(unicode, buffer, buflen); | ||||
|     if (buflen == -1) { | ||||
|         PyMem_FREE(buffer); | ||||
|         return NULL; | ||||
|     } | ||||
|     memcpy(buffer, wstr, (buflen + 1) * sizeof(wchar_t)); | ||||
|     if (size != NULL) | ||||
|         *size = buflen; | ||||
|     return buffer; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Serhiy Storchaka
						Serhiy Storchaka