[Python-checkins] cpython: Sanitize reference management in the utf-8 encoder

antoine.pitrou python-checkins at python.org
Sat Nov 12 18:43:11 CET 2011


http://hg.python.org/cpython/rev/e3a84fa5cf63
changeset:   73520:e3a84fa5cf63
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Sat Nov 12 18:35:19 2011 +0100
summary:
  Sanitize reference management in the utf-8 encoder

files:
  Objects/unicodeobject.c |  9 ++++-----
  1 files changed, 4 insertions(+), 5 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -4722,6 +4722,7 @@
     int kind;
     void *data;
     Py_ssize_t size;
+    PyObject *rep = NULL;
 
     if (!PyUnicode_Check(unicode)) {
         PyErr_BadArgument();
@@ -4774,7 +4775,6 @@
             *p++ = (char)(0x80 | (ch & 0x3f));
         } else if (0xD800 <= ch && ch <= 0xDFFF) {
             Py_ssize_t newpos;
-            PyObject *rep;
             Py_ssize_t repsize, k, startpos;
             startpos = i-1;
             rep = unicode_encode_call_errorhandler(
@@ -4822,10 +4822,8 @@
                 enum PyUnicode_Kind repkind;
                 void *repdata;
 
-                if (PyUnicode_READY(rep) < 0) {
-                    Py_DECREF(rep);
+                if (PyUnicode_READY(rep) < 0)
                     goto error;
-                }
                 repkind = PyUnicode_KIND(rep);
                 repdata = PyUnicode_DATA(rep);
 
@@ -4841,7 +4839,7 @@
                     *p++ = (char)c;
                 }
             }
-            Py_DECREF(rep);
+            Py_CLEAR(rep);
         } else if (ch < 0x10000) {
             *p++ = (char)(0xe0 | (ch >> 12));
             *p++ = (char)(0x80 | ((ch >> 6) & 0x3f));
@@ -4872,6 +4870,7 @@
     Py_XDECREF(exc);
     return result;
  error:
+    Py_XDECREF(rep);
     Py_XDECREF(errorHandler);
     Py_XDECREF(exc);
     Py_XDECREF(result);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list