[Python-checkins] cpython: Issue #18408: Fix usage of _PyBytes_Resize()

victor.stinner python-checkins at python.org
Tue Jul 9 00:53:51 CEST 2013


http://hg.python.org/cpython/rev/ed0c9d77e179
changeset:   84520:ed0c9d77e179
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Jul 09 00:35:22 2013 +0200
summary:
  Issue #18408: Fix usage of _PyBytes_Resize()

_PyBytes_Resize(&v, new_size) sets v to NULL on error, so v cannot be used
anymore. Replace "Py_DECREF(v); v = NULL;" with "Py_CLEAR(v);".

files:
  Modules/binascii.c   |  23 ++++++++---------------
  Modules/zlibmodule.c |  24 ++++++++----------------
  2 files changed, 16 insertions(+), 31 deletions(-)


diff --git a/Modules/binascii.c b/Modules/binascii.c
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -361,8 +361,7 @@
     if (_PyBytes_Resize(&rv,
                        (ascii_data -
                         (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
-        Py_DECREF(rv);
-        rv = NULL;
+        Py_CLEAR(rv);
     }
     PyBuffer_Release(&pbin);
     return rv;
@@ -491,8 +490,7 @@
     */
     if (bin_len > 0) {
         if (_PyBytes_Resize(&rv, bin_len) < 0) {
-            Py_DECREF(rv);
-            rv = NULL;
+            Py_CLEAR(rv);
         }
     }
     else {
@@ -563,8 +561,7 @@
     if (_PyBytes_Resize(&rv,
                        (ascii_data -
                         (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
-        Py_DECREF(rv);
-        rv = NULL;
+        Py_CLEAR(rv);
     }
     PyBuffer_Release(&pbuf);
     return rv;
@@ -642,8 +639,7 @@
     if (_PyBytes_Resize(&rv,
                        (bin_data -
                         (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
-        Py_DECREF(rv);
-        rv = NULL;
+        Py_CLEAR(rv);
     }
     if (rv) {
         PyObject *rrv = Py_BuildValue("Oi", rv, done);
@@ -713,8 +709,7 @@
     if (_PyBytes_Resize(&rv,
                        (out_data -
                         (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
-        Py_DECREF(rv);
-        rv = NULL;
+        Py_CLEAR(rv);
     }
     PyBuffer_Release(&pbuf);
     return rv;
@@ -770,8 +765,7 @@
     if (_PyBytes_Resize(&rv,
                        (ascii_data -
                         (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
-        Py_DECREF(rv);
-        rv = NULL;
+        Py_CLEAR(rv);
     }
     PyBuffer_Release(&pbin);
     return rv;
@@ -834,7 +828,7 @@
              if ( --out_len_left < 0 ) { \
                       if ( out_len > PY_SSIZE_T_MAX / 2) return PyErr_NoMemory(); \
                       if (_PyBytes_Resize(&rv, 2*out_len) < 0) \
-                        { Py_DECREF(rv); PyBuffer_Release(&pin); return NULL; } \
+                        { Py_XDECREF(rv); PyBuffer_Release(&pin); return NULL; } \
                       out_data = (unsigned char *)PyBytes_AS_STRING(rv) \
                                                              + out_len; \
                       out_len_left = out_len-1; \
@@ -887,8 +881,7 @@
     if (_PyBytes_Resize(&rv,
                        (out_data -
                         (unsigned char *)PyBytes_AS_STRING(rv))) < 0) {
-        Py_DECREF(rv);
-        rv = NULL;
+        Py_CLEAR(rv);
     }
     PyBuffer_Release(&pin);
     return rv;
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -549,8 +549,7 @@
        so extend the output buffer and try again */
     while (err == Z_OK && self->zst.avail_out == 0) {
         if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
-            Py_DECREF(RetVal);
-            RetVal = NULL;
+            Py_CLEAR(RetVal);
             goto error;
         }
         self->zst.next_out =
@@ -574,8 +573,7 @@
         goto error;
     }
     if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
-        Py_DECREF(RetVal);
-        RetVal = NULL;
+        Py_CLEAR(RetVal);
     }
 
  error:
@@ -722,8 +720,7 @@
             length = max_length;
 
         if (_PyBytes_Resize(&RetVal, length) < 0) {
-            Py_DECREF(RetVal);
-            RetVal = NULL;
+            Py_CLEAR(RetVal);
             goto error;
         }
         self->zst.next_out =
@@ -757,8 +754,7 @@
     }
 
     if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
-        Py_DECREF(RetVal);
-        RetVal = NULL;
+        Py_CLEAR(RetVal);
     }
 
  error:
@@ -811,8 +807,7 @@
        so extend the output buffer and try again */
     while (err == Z_OK && self->zst.avail_out == 0) {
         if (_PyBytes_Resize(&RetVal, length << 1) < 0) {
-            Py_DECREF(RetVal);
-            RetVal = NULL;
+            Py_CLEAR(RetVal);
             goto error;
         }
         self->zst.next_out =
@@ -851,8 +846,7 @@
     }
 
     if (_PyBytes_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) {
-        Py_DECREF(RetVal);
-        RetVal = NULL;
+        Py_CLEAR(RetVal);
     }
 
  error:
@@ -1012,8 +1006,7 @@
        so extend the output buffer and try again */
     while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {
         if (_PyBytes_Resize(&retval, length << 1) < 0) {
-            Py_DECREF(retval);
-            retval = NULL;
+            Py_CLEAR(retval);
             goto error;
         }
         self->zst.next_out = (Byte *)PyBytes_AS_STRING(retval) + length;
@@ -1045,8 +1038,7 @@
     }
 
     if (_PyBytes_Resize(&retval, self->zst.total_out - start_total_out) < 0) {
-        Py_DECREF(retval);
-        retval = NULL;
+        Py_CLEAR(retval);
     }
 
 error:

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


More information about the Python-checkins mailing list