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