From 3ff8cc58e1d2f7c380288a9b5fe3ac407edfd43b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 14 Nov 2021 11:50:06 +0530 Subject: [PATCH] Ignore invalid unicode condepoints in command lines --- kitty/child.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/kitty/child.c b/kitty/child.c index 920da0a0b..3af3cb5b8 100644 --- a/kitty/child.c +++ b/kitty/child.c @@ -24,10 +24,19 @@ serialize_string_tuple(PyObject *src) { if (!ans) fatal("Out of memory"); for (Py_ssize_t i = 0; i < sz; i++) { const char *pysrc = PyUnicode_AsUTF8(PyTuple_GET_ITEM(src, i)); - size_t len = strlen(pysrc); - ans[i] = calloc(len + 1, sizeof(char)); - if (ans[i] == NULL) fatal("Out of memory"); - memcpy(ans[i], pysrc, len); + if (!pysrc) { + PyErr_Clear(); + DECREF_AFTER_FUNCTION PyObject *u8 = PyUnicode_AsEncodedString(PyTuple_GET_ITEM(src, i), "UTF-8", "ignore"); + if (!u8) { PyErr_Print(); fatal("couldnt parse command line"); } + ans[i] = calloc(PyBytes_GET_SIZE(u8) + 1, sizeof(char)); + if (ans[i] == NULL) fatal("Out of memory"); + memcpy(ans[i], PyBytes_AS_STRING(u8), PyBytes_GET_SIZE(u8)); + } else { + size_t len = strlen(pysrc); + ans[i] = calloc(len + 1, sizeof(char)); + if (ans[i] == NULL) fatal("Out of memory"); + memcpy(ans[i], pysrc, len); + } } return ans; }