Fix invalid UTF8 strings being copied into the Python console

Add a ensure_utf8 argument to WM_clipboard_text_get so callers don't
have to handle validation themselves.

Copying non-utf8 text into the Python console and buttons was possible,
causing invalid cursor position and a UnicodeDecodeError accessing
ConsoleLine.body from Python.
This commit is contained in:
2023-05-19 12:29:53 +10:00
parent 0f67ac4547
commit bb543620ae
9 changed files with 50 additions and 29 deletions

View File

@@ -111,7 +111,8 @@ PyDoc_STRVAR(pyrna_WindowManager_clipboard_doc, "Clipboard text storage.\n\n:typ
static PyObject *pyrna_WindowManager_clipboard_get(PyObject *UNUSED(self), void *UNUSED(flag))
{
int text_len = 0;
char *text = WM_clipboard_text_get(false, &text_len);
/* No need for UTF8 validation as #PyC_UnicodeFromBytesAndSize handles invalid byte sequences. */
char *text = WM_clipboard_text_get(false, false, &text_len);
PyObject *result = PyC_UnicodeFromBytesAndSize(text ? text : "", text_len);
if (text != NULL) {
MEM_freeN(text);