]> granicus.if.org Git - python/commitdiff
PyPcre_expand(): Fixed two memory leaks, where a PyString_FromString()
authorBarry Warsaw <barry@python.org>
Mon, 1 Feb 1999 17:09:00 +0000 (17:09 +0000)
committerBarry Warsaw <barry@python.org>
Mon, 1 Feb 1999 17:09:00 +0000 (17:09 +0000)
was appended to a list.  Lists are reference count neutral, so the
string must be DECREF'd.  Also added some checks for the return value
of PyList_Append().

Note: there are still some memory problems reported by Purify (I get
two Array Bounds Reads still and an Unitialized Memory Read).  Also,
in scanning the code, there appears to be some potential problems
where return values aren't checked.  To much to attack now though.

Modules/pcremodule.c

index fa986964887881d8f344f145266ed5d236c7c7b2..3b61697e00130e5322f9c028c274fbd238c48ab9 100644 (file)
@@ -489,8 +489,19 @@ PyPcre_expand(self, args)
 
                        if (start!=i)
                        {
-                               PyList_Append(results, 
-                                             PyString_FromStringAndSize((char *)repl+start, i-start));
+                               int status;
+                               PyObject *s = PyString_FromStringAndSize(
+                                       (char *)repl+start, i-start);
+                               if (s == NULL) {
+                                       Py_DECREF(results);
+                                       return NULL;
+                               }
+                               status = PyList_Append(results, s);
+                               Py_DECREF(s);
+                               if (status < 0) {
+                                       Py_DECREF(results);
+                                       return NULL;
+                               }
                                total_len += i-start;
                        }
                        i++;
@@ -574,7 +585,19 @@ PyPcre_expand(self, args)
 
        if (start!=i)
        {
-               PyList_Append(results, PyString_FromStringAndSize((char *)repl+start, i-start));
+               int status;
+               PyObject *s = PyString_FromStringAndSize((char *)repl+start, 
+                                                        i-start);
+               if (s == NULL) {
+                       Py_DECREF(results);
+                       return NULL;
+               }
+               status = PyList_Append(results, s);
+               Py_DECREF(s);
+               if (status < 0) {
+                       Py_DECREF(results);
+                       return NULL;
+               }
                total_len += i-start;
        }