summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2000-08-16 19:43:17 (GMT)
committerBarry Warsaw <barry@python.org>2000-08-16 19:43:17 (GMT)
commit77c9f50422833829fe1c001831f778e4fcd512df (patch)
tree10c78a36978caa501009d946da0a184f557dab0c /Python
parent9f6e6c63ef84a5171b0c8f2e665d853c5cd99905 (diff)
downloadcpython-77c9f50422833829fe1c001831f778e4fcd512df.zip
cpython-77c9f50422833829fe1c001831f778e4fcd512df.tar.gz
cpython-77c9f50422833829fe1c001831f778e4fcd512df.tar.bz2
SyntaxError__str__(): Fix two memory problems discovered by Insure.
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.
Diffstat (limited to 'Python')
-rw-r--r--Python/exceptions.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/Python/exceptions.c b/Python/exceptions.c
index abacda4..690af7e 100644
--- a/Python/exceptions.c
+++ b/Python/exceptions.c
@@ -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