Blob Blame History Raw
From b95e9b2eff6541d03a4c490d46bce068b2c98499 Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Tue, 16 Jun 2015 14:53:30 +0200
Subject: [PATCH] Use UTF-8 encoding when working with user files

Environment variables may hold string values in any encoding, not
necessarily in ASCII.

Converting a Unicode object holding a UTF-8 string into an ASCII string
raises an exception and the returned value is NULL.

Resolves: rhbz#1231844

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/gnome_abrt/wrappers/problem_app.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/gnome_abrt/wrappers/problem_app.c b/src/gnome_abrt/wrappers/problem_app.c
index 10aa75a..55e50fd 100644
--- a/src/gnome_abrt/wrappers/problem_app.c
+++ b/src/gnome_abrt/wrappers/problem_app.c
@@ -53,8 +53,21 @@ PyObject *p_get_app_for_env(PyObject *module, PyObject *args)
         for (i = 0; i < size; i++) {
             PyObject *seqItem = PySequence_Fast_GET_ITEM(envp_seq, i);
 
-            PyObject *asciiItem = PyUnicode_AsASCIIString(seqItem);
-            g_ptr_array_insert (envp_array, -1, g_strdup(PyBytes_AsString(asciiItem)));
+            const char *strItem = PyUnicode_AsUTF8(seqItem);
+            if (strItem == NULL)
+            {
+                PyObject *unicodeObj = PyObject_Str(seqItem);
+                const char *str = PyUnicode_AsUTF8(unicodeObj);
+
+                fprintf(stderr, "BUG:%s:%d: failed to get a UTF-8 string from: %s\n", __FILE__, __LINE__, str);
+                /* Catch all exceptions, print them out and continue in
+                 * processing (try, catch, log, continue). */
+                PyErr_Print();
+                PyErr_Clear();
+                continue;
+            }
+
+            g_ptr_array_insert (envp_array, -1, g_strdup(strItem));
         }
         g_ptr_array_insert (envp_array, -1, NULL);
 
-- 
2.4.2