New submission from Serhiy Storchaka:

The second parameter of the PyUnicode_AsWideCharString() function

    wchar_t* PyUnicode_AsWideCharString(PyObject *unicode, Py_ssize_t *size)

is a pointer to Py_ssize_t. The size of created wchar_t array is saved on this 
pointer if it is not NULL. If NULL is passed as the second argument, the only 
way to determine the size of the wchar_t string is using wcslen(). But if the 
string contains the null characters, it looks truncated for wcslen() and other 
C API functions.

Reliable code should always pass the non-NULL second argument and check that 
wcslen() is equal to the returned string size. See for example the code in 
Modules/_io/winconsoleio.c. Passing NULL as the second argument is unsafe. But 
most code doesn't do such check (see all other usages of 
PyUnicode_AsWideCharString(..., NULL)). And this check complicates the callers 
code.

I propose to make the check for null characters inside of 
PyUnicode_AsWideCharString() if NULL is passes as the second argument. This 
will fix all unsafe usages of PyUnicode_AsWideCharString() and allow to 
simplify the reliable code.

This issue fixes the part of issue13617.

----------
components: Interpreter Core, Unicode
messages: 296401
nosy: ezio.melotti, haypo, serhiy.storchaka, steve.dower
priority: normal
severity: normal
stage: patch review
status: open
title: Ensure that the result of PyUnicode_AsWideCharString() doesn't contain 
null characters if size is not returned
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30708>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to