From 1bf03d02c001abae31bc06eeb0d1247f522dee11 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Apr 23 2020 14:26:58 +0000 Subject: Return NULL string as None from utf8FromString() Commit 84920f898315d09a57a3f1067433eaeb7de5e830 regressed dnf install to segfault at the end due to some NULL string passed to strlen(). Check for NULL and return it as None, make it an inline function to make this saner. --- diff --git a/python/rpmsystem-py.h b/python/rpmsystem-py.h index c55be37..ae9418b 100644 --- a/python/rpmsystem-py.h +++ b/python/rpmsystem-py.h @@ -52,11 +52,17 @@ typedef Py_ssize_t (*lenfunc)(PyObject *); #define PyInt_AsSsize_t PyLong_AsSsize_t #endif +static inline PyObject * utf8FromString(const char *s) +{ /* In Python 3, we return all strings as surrogate-escaped utf-8 */ #if PY_MAJOR_VERSION >= 3 -#define utf8FromString(_s) PyUnicode_DecodeUTF8(_s, strlen(_s), "surrogateescape") + if (s != NULL) + return PyUnicode_DecodeUTF8(s, strlen(s), "surrogateescape"); #else -#define utf8FromString(_s) PyBytes_FromString(_s) + if (s != NULL) + return PyBytes_FromString(s); #endif + Py_RETURN_NONE; +} #endif /* H_SYSTEM_PYTHON */