summaryrefslogtreecommitdiff
path: root/src/plugins/python
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2020-04-04 18:10:02 +0200
committerSébastien Helleu <flashcode@flashtux.org>2020-04-04 18:10:02 +0200
commitfae149b361bc52096d6bd41ec219a0f61936d96c (patch)
treea81a905b19c52350a16e2b744193452b8a74ed0a /src/plugins/python
parente4bd582a97acf0dc7b41ebfea6c37937a83d49fe (diff)
downloadweechat-fae149b361bc52096d6bd41ec219a0f61936d96c.zip
python: fix crash when invalid UTF-8 string is in a WeeChat hashtable converted to a Python dict (closes #1463)
Diffstat (limited to 'src/plugins/python')
-rw-r--r--src/plugins/python/weechat-python.c28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/plugins/python/weechat-python.c b/src/plugins/python/weechat-python.c
index b1ec9bb3a..4ef9e553d 100644
--- a/src/plugins/python/weechat-python.c
+++ b/src/plugins/python/weechat-python.c
@@ -234,13 +234,31 @@ weechat_python_hashtable_map_cb (void *data,
dict = (PyObject *)data;
- dict_key = Py_BuildValue ("s", key);
- dict_value = Py_BuildValue ("s", value);
+#if PY_MAJOR_VERSION >= 3
+ /* key */
+ if (weechat_utf8_is_valid (key, -1, NULL))
+ dict_key = Py_BuildValue ("s", key); /* Python 3: str */
+ else
+ dict_key = Py_BuildValue ("y", key); /* Python 3: bytes */
+ /* value */
+ if (weechat_utf8_is_valid (value, -1, NULL))
+ dict_value = Py_BuildValue ("s", value); /* Python 3: str */
+ else
+ dict_value = Py_BuildValue ("y", value); /* Python 3: bytes */
+#else
+ /* key */
+ dict_key = Py_BuildValue ("s", key); /* Python 2: str */
+ /* value */
+ dict_value = Py_BuildValue ("s", value); /* Python 2: str */
+#endif
- PyDict_SetItem (dict, dict_key, dict_value);
+ if (dict_key && dict_value)
+ PyDict_SetItem (dict, dict_key, dict_value);
- Py_DECREF (dict_key);
- Py_DECREF (dict_value);
+ if (dict_key)
+ Py_DECREF (dict_key);
+ if (dict_value)
+ Py_DECREF (dict_value);
}
/*