dcavalca / rpms / rpm

Forked from rpms/rpm 2 years ago
Clone

Blame 0001-Return-NULL-string-as-None-from-utf8FromString.patch

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