summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2000-03-13 16:01:29 (GMT)
committerGuido van Rossum <guido@python.org>2000-03-13 16:01:29 (GMT)
commitd724b23420f8e9d73a656c941c45b234e08ff9d6 (patch)
treea87fcb839783274d3d0fee3c9609793189a83d8e /Objects
parent96a45adf808ee9798d0c2637aeed2c895fa8e929 (diff)
downloadcpython-d724b23420f8e9d73a656c941c45b234e08ff9d6.zip
cpython-d724b23420f8e9d73a656c941c45b234e08ff9d6.tar.gz
cpython-d724b23420f8e9d73a656c941c45b234e08ff9d6.tar.bz2
Christian Tismer's "trashcan" patch:
Added wrapping macros to dictobject.c, listobject.c, tupleobject.c, frameobject.c, traceback.c that safely prevends core dumps on stack overflow. Macros and functions in object.c, object.h. The method is an "elevator destructor" that turns cascading deletes into tail recursive behavior when some limit is hit.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/dictobject.c2
-rw-r--r--Objects/frameobject.c2
-rw-r--r--Objects/listobject.c2
-rw-r--r--Objects/object.c45
-rw-r--r--Objects/tupleobject.c5
5 files changed, 55 insertions, 1 deletions
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index ceec39d..ea32e23 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -479,6 +479,7 @@ dict_dealloc(mp)
{
register int i;
register dictentry *ep;
+ Py_TRASHCAN_SAFE_BEGIN(mp)
for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) {
if (ep->me_key != NULL) {
Py_DECREF(ep->me_key);
@@ -489,6 +490,7 @@ dict_dealloc(mp)
}
PyMem_XDEL(mp->ma_table);
PyMem_DEL(mp);
+ Py_TRASHCAN_SAFE_END(mp)
}
static int
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index dcd760c..4c716cd 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -103,6 +103,7 @@ frame_dealloc(f)
int i;
PyObject **fastlocals;
+ Py_TRASHCAN_SAFE_BEGIN(f)
/* Kill all local variables */
fastlocals = f->f_localsplus;
for (i = f->f_nlocals; --i >= 0; ++fastlocals) {
@@ -120,6 +121,7 @@ frame_dealloc(f)
Py_XDECREF(f->f_exc_traceback);
f->f_back = free_list;
free_list = f;
+ Py_TRASHCAN_SAFE_END(f)
}
PyTypeObject PyFrame_Type = {
diff --git a/Objects/listobject.c b/Objects/listobject.c
index d77b546..673028f 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -215,6 +215,7 @@ list_dealloc(op)
PyListObject *op;
{
int i;
+ Py_TRASHCAN_SAFE_BEGIN(op)
if (op->ob_item != NULL) {
/* Do it backwards, for Christian Tismer.
There's a simple test case where somehow this reduces
@@ -227,6 +228,7 @@ list_dealloc(op)
free((ANY *)op->ob_item);
}
free((ANY *)op);
+ Py_TRASHCAN_SAFE_END(op)
}
static int
diff --git a/Objects/object.c b/Objects/object.c
index 7a41aa2..d2aa542 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -906,3 +906,48 @@ Py_ReprLeave(obj)
}
}
}
+
+/*
+ trashcan
+ CT 2k0130
+ non-recursively destroy nested objects
+
+ CT 2k0223
+ everything is now done in a macro.
+
+ CT 2k0305
+ modified to use functions, after Tim Peter's suggestion.
+
+ CT 2k0309
+ modified to restore a possible error.
+*/
+
+int _PyTrash_delete_nesting = 0;
+PyObject * _PyTrash_delete_later = NULL;
+
+void
+_PyTrash_deposit_object(op)
+ PyObject *op;
+{
+ PyObject *error_type, *error_value, *error_traceback;
+ PyErr_Fetch(&error_type, &error_value, &error_traceback);
+
+ if (!_PyTrash_delete_later)
+ _PyTrash_delete_later = PyList_New(0);
+ if (_PyTrash_delete_later)
+ PyList_Append(_PyTrash_delete_later, (PyObject *)op);
+
+ PyErr_Restore(error_type, error_value, error_traceback);
+}
+
+void
+_PyTrash_destroy_list()
+{
+ while (_PyTrash_delete_later) {
+ PyObject *shredder = _PyTrash_delete_later;
+ _PyTrash_delete_later = NULL;
+ ++_PyTrash_delete_nesting;
+ Py_DECREF(shredder);
+ --_PyTrash_delete_nesting;
+ }
+}
diff --git a/Objects/tupleobject.c b/Objects/tupleobject.c
index 568c4b3..5112468 100644
--- a/Objects/tupleobject.c
+++ b/Objects/tupleobject.c
@@ -172,6 +172,7 @@ tupledealloc(op)
{
register int i;
+ Py_TRASHCAN_SAFE_BEGIN(op)
if (op->ob_size > 0) {
i = op->ob_size;
while (--i >= 0)
@@ -180,11 +181,13 @@ tupledealloc(op)
if (op->ob_size < MAXSAVESIZE) {
op->ob_item[0] = (PyObject *) free_tuples[op->ob_size];
free_tuples[op->ob_size] = op;
- return;
+ goto done; /* return */
}
#endif
}
free((ANY *)op);
+done:
+ Py_TRASHCAN_SAFE_END(op)
}
static int