]> granicus.if.org Git - python/commitdiff
SyntaxError__str__(): Fix two memory problems discovered by Insure.
authorBarry Warsaw <barry@python.org>
Wed, 16 Aug 2000 19:43:17 +0000 (19:43 +0000)
committerBarry Warsaw <barry@python.org>
Wed, 16 Aug 2000 19:43:17 +0000 (19:43 +0000)
First, the allocated buffer was never freed after using it to create
the PyString object.  Second, it was possible that have_filename would
be false (meaning that filename was not a PyString object), but that
the code would still try to PyString_GET_SIZE() it.

Python/exceptions.c

index abacda4d995fa61cf47f0c26650fed0f502b3f70..690af7e92eed298c2df2cefdd44e8478062b7643 100644 (file)
@@ -775,18 +775,20 @@ SyntaxError__str__(PyObject *self, PyObject *args)
        int have_lineno = 0;
        char *buffer = NULL;
 
-       if (filename = PyObject_GetAttrString(self, "filename"))
+       if ((filename = PyObject_GetAttrString(self, "filename")) != NULL)
            have_filename = PyString_Check(filename);
        else
            PyErr_Clear();
-       if (lineno = PyObject_GetAttrString(self, "lineno"))
+
+       if ((lineno = PyObject_GetAttrString(self, "lineno")) != NULL)
            have_lineno = PyInt_Check(lineno);
        else
            PyErr_Clear();
 
        if (have_filename || have_lineno) {
-           int bufsize = (PyString_GET_SIZE(str) + 64 +
-                          PyString_GET_SIZE(filename));
+           int bufsize = PyString_GET_SIZE(str) + 64;
+           if (have_filename)
+               bufsize += PyString_GET_SIZE(filename);
 
            buffer = PyMem_Malloc(bufsize);
            if (buffer != NULL) {
@@ -803,7 +805,10 @@ SyntaxError__str__(PyObject *self, PyObject *args)
                    sprintf(buffer, "%s (line %d)",
                            PyString_AS_STRING(str),
                            PyInt_AsLong(lineno));
+
                result = PyString_FromString(buffer);
+               PyMem_FREE(buffer);
+
                if (result == NULL)
                    result = str;
                else