@@ -1229,9 +1229,6 @@ U_get(void *ptr, Py_ssize_t size)
12291229static PyObject *
12301230U_set (void * ptr , PyObject * value , Py_ssize_t length )
12311231{
1232- Py_UNICODE * wstr ;
1233- Py_ssize_t size ;
1234-
12351232 /* It's easier to calculate in characters than in bytes */
12361233 length /= sizeof (wchar_t );
12371234
@@ -1242,9 +1239,14 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
12421239 return NULL ;
12431240 }
12441241
1245- wstr = PyUnicode_AsUnicodeAndSize (value , & size );
1246- if (wstr == NULL )
1242+ Py_ssize_t size = PyUnicode_AsWideChar (value , NULL , 0 );
1243+ if (size < 0 ) {
12471244 return NULL ;
1245+ }
1246+ // PyUnicode_AsWideChar() returns number of wchars including trailing null byte,
1247+ // when it is called with NULL.
1248+ size -- ;
1249+ assert (size >= 0 );
12481250 if (size > length ) {
12491251 PyErr_Format (PyExc_ValueError ,
12501252 "string too long (%zd, maximum length %zd)" ,
@@ -1421,16 +1423,18 @@ BSTR_set(void *ptr, PyObject *value, Py_ssize_t size)
14211423
14221424 /* create a BSTR from value */
14231425 if (value ) {
1424- wchar_t * wvalue ;
14251426 Py_ssize_t wsize ;
1426- wvalue = PyUnicode_AsUnicodeAndSize (value , & wsize );
1427- if (wvalue == NULL )
1427+ wchar_t * wvalue = PyUnicode_AsWideCharString (value , & wsize );
1428+ if (wvalue == NULL ) {
14281429 return NULL ;
1430+ }
14291431 if ((unsigned ) wsize != wsize ) {
14301432 PyErr_SetString (PyExc_ValueError , "String too long for BSTR" );
1433+ PyMem_Free (wvalue );
14311434 return NULL ;
14321435 }
14331436 bstr = SysAllocStringLen (wvalue , (unsigned )wsize );
1437+ PyMem_Free (wvalue );
14341438 } else
14351439 bstr = NULL ;
14361440
0 commit comments