@@ -67,10 +67,12 @@ _Py_char2wchar(const char* arg, size_t *size)
6767#ifdef __APPLE__
6868 wchar_t * wstr ;
6969 wstr = _Py_DecodeUTF8_surrogateescape (arg , strlen (arg ));
70- if (wstr == NULL )
71- return NULL ;
72- if (size != NULL )
73- * size = wcslen (wstr );
70+ if (size != NULL ) {
71+ if (wstr != NULL )
72+ * size = wcslen (wstr );
73+ else
74+ * size = (size_t )-1 ;
75+ }
7476 return wstr ;
7577#else
7678 wchar_t * res ;
@@ -204,22 +206,25 @@ _Py_wchar2char(const wchar_t *text, size_t *error_pos)
204206 char * cpath ;
205207
206208 unicode = PyUnicode_FromWideChar (text , wcslen (text ));
207- if (unicode == NULL ) {
208- Py_DECREF (unicode );
209+ if (unicode == NULL )
209210 return NULL ;
210- }
211211
212212 bytes = _PyUnicode_AsUTF8String (unicode , "surrogateescape" );
213213 Py_DECREF (unicode );
214214 if (bytes == NULL ) {
215215 PyErr_Clear ();
216+ if (error_pos != NULL )
217+ * error_pos = (size_t )-1 ;
216218 return NULL ;
217219 }
218220
219221 len = PyBytes_GET_SIZE (bytes );
220222 cpath = PyMem_Malloc (len + 1 );
221223 if (cpath == NULL ) {
224+ PyErr_Clear ();
222225 Py_DECREF (bytes );
226+ if (error_pos != NULL )
227+ * error_pos = (size_t )-1 ;
223228 return NULL ;
224229 }
225230 memcpy (cpath , PyBytes_AsString (bytes ), len + 1 );
@@ -231,9 +236,6 @@ _Py_wchar2char(const wchar_t *text, size_t *error_pos)
231236 size_t i , size , converted ;
232237 wchar_t c , buf [2 ];
233238
234- if (error_pos != NULL )
235- * error_pos = (size_t )-1 ;
236-
237239 /* The function works in two steps:
238240 1. compute the length of the output buffer in bytes (size)
239241 2. outputs the bytes */
@@ -280,8 +282,11 @@ _Py_wchar2char(const wchar_t *text, size_t *error_pos)
280282
281283 size += 1 ; /* nul byte at the end */
282284 result = PyMem_Malloc (size );
283- if (result == NULL )
285+ if (result == NULL ) {
286+ if (error_pos != NULL )
287+ * error_pos = (size_t )-1 ;
284288 return NULL ;
289+ }
285290 bytes = result ;
286291 }
287292 return result ;
0 commit comments