Skip to content

gh-154781: Zero the in_wstr() buffer in curses - #154782

Merged
serhiy-storchaka merged 3 commits into
python:mainfrom
fedonman:fix-curses-in-wstr-uninit
Jul 29, 2026
Merged

gh-154781: Zero the in_wstr() buffer in curses#154782
serhiy-storchaka merged 3 commits into
python:mainfrom
fedonman:fix-curses-in-wstr-uninit

Conversation

@fedonman

@fedonman fedonman commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

winnwstr() only writes the terminating null when it stored at least one character, so
in_wstr(y, x, 0) read the result from uninitialized memory and usually raised
ValueError: character U+xxxxxxxx is not in range. Use PyMem_Calloc, like in_wchstr()
already does since gh-152503.

in_wstr() is new in 3.16, so there is no NEWS entry.

winnwstr() only writes the terminating null when it stored at least one
character, so with n of 0 the result was read from uninitialized memory. Use
PyMem_Calloc, like in_wchstr() already does.
Comment thread Modules/_cursesmodule.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not this be enough?

Suggested change
PyObject *res = PyUnicode_FromWideChar(buf, rtn);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice call! Changed it.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@serhiy-storchaka
serhiy-storchaka merged commit 9c91fd9 into python:main Jul 29, 2026
54 checks passed
@fedonman
fedonman deleted the fix-curses-in-wstr-uninit branch July 29, 2026 08:12
mbeijen pushed a commit to mbeijen/cpython that referenced this pull request Aug 14, 2026
…54782)

in_wstr() searched the result for a terminating null, but winnwstr()
writes one only if it stored at least one character.  Use the number of
characters it returns as the length.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants