0b2921
From aea53a4aead8bd71f519df35fcffd9eec76fbc01 Mon Sep 17 00:00:00 2001
0b2921
Message-Id: <aea53a4aead8bd71f519df35fcffd9eec76fbc01.1554884465.git.pmatilai@redhat.com>
0b2921
From: Panu Matilainen <pmatilai@redhat.com>
0b2921
Date: Tue, 26 Feb 2019 11:27:51 +0200
0b2921
Subject: [PATCH] Return NULL string as None from utf8FromString()
0b2921
0b2921
Commit 84920f898315d09a57a3f1067433eaeb7de5e830 regressed dnf install
0b2921
to segfault at the end due to some NULL string passed to strlen().
0b2921
Check for NULL and return it as None, make it an inline function
0b2921
to make this saner.
0b2921
---
0b2921
 python/rpmsystem-py.h | 10 ++++++++--
0b2921
 1 file changed, 8 insertions(+), 2 deletions(-)
0b2921
0b2921
diff --git a/python/rpmsystem-py.h b/python/rpmsystem-py.h
0b2921
index 87c750571..25938464a 100644
0b2921
--- a/python/rpmsystem-py.h
0b2921
+++ b/python/rpmsystem-py.h
0b2921
@@ -19,11 +19,17 @@
0b2921
 #define PyInt_AsSsize_t PyLong_AsSsize_t
0b2921
 #endif
0b2921
 
0b2921
+static inline PyObject * utf8FromString(const char *s)
0b2921
+{
0b2921
 /* In Python 3, we return all strings as surrogate-escaped utf-8 */
0b2921
 #if PY_MAJOR_VERSION >= 3
0b2921
-#define utf8FromString(_s) PyUnicode_DecodeUTF8(_s, strlen(_s), "surrogateescape")
0b2921
+    if (s != NULL)
0b2921
+	return PyUnicode_DecodeUTF8(s, strlen(s), "surrogateescape");
0b2921
 #else
0b2921
-#define utf8FromString(_s) PyBytes_FromString(_s)
0b2921
+    if (s != NULL)
0b2921
+	return PyBytes_FromString(s);
0b2921
 #endif
0b2921
+    Py_RETURN_NONE;
0b2921
+}
0b2921
 
0b2921
 #endif	/* H_SYSTEM_PYTHON */
0b2921
-- 
0b2921
2.20.1
0b2921