--- /dev/null
+import os
+import textwrap
+import unittest
+
+from test import support
+from test.support.script_helper import assert_python_ok
+
+
+class TestLLTrace(unittest.TestCase):
+
+ def test_lltrace_does_not_crash_on_subscript_operator(self):
+ # If this test fails, it will reproduce a crash reported as
+ # bpo-34113. The crash happened at the command line console of
+ # debug Python builds with __ltrace__ enabled (only possible in console),
+ # when the interal Python stack was negatively adjusted
+ with open(support.TESTFN, 'w') as fd:
+ self.addCleanup(os.unlink, support.TESTFN)
+ fd.write(textwrap.dedent("""\
+ import code
+
+ console = code.InteractiveConsole()
+ console.push('__ltrace__ = 1')
+ console.push('a = [1, 2, 3]')
+ console.push('a[0] = 1')
+ print('unreachable if bug exists')
+ """))
+
+ assert_python_ok(support.TESTFN)
+
+if __name__ == "__main__":
+ unittest.main()
assert(STACK_LEVEL() <= co->co_stacksize); }
#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
BASIC_POP())
-#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
+#define STACK_GROW(n) do { \
+ assert(n >= 0); \
+ (void)(BASIC_STACKADJ(n), \
lltrace && prtrace(TOP(), "stackadj")); \
- assert(STACK_LEVEL() <= co->co_stacksize); }
+ assert(STACK_LEVEL() <= co->co_stacksize); \
+ } while (0)
+#define STACK_SHRINK(n) do { \
+ assert(n >= 0); \
+ (void)(lltrace && prtrace(TOP(), "stackadj")); \
+ (void)(BASIC_STACKADJ(-n)); \
+ assert(STACK_LEVEL() <= co->co_stacksize); \
+ } while (0)
#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
prtrace((STACK_POINTER)[-1], "ext_pop")), \
*--(STACK_POINTER))
#else
#define PUSH(v) BASIC_PUSH(v)
#define POP() BASIC_POP()
-#define STACKADJ(n) BASIC_STACKADJ(n)
+#define STACK_GROW(n) BASIC_STACKADJ(n)
+#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
#endif
PyObject *second = SECOND();
Py_INCREF(top);
Py_INCREF(second);
- STACKADJ(2);
+ STACK_GROW(2);
SET_TOP(top);
SET_SECOND(second);
FAST_DISPATCH();
SET_TOP(Py_False);
DISPATCH();
}
- STACKADJ(-1);
+ STACK_SHRINK(1);
goto error;
}
PyObject *container = SECOND();
PyObject *v = THIRD();
int err;
- STACKADJ(-3);
+ STACK_SHRINK(3);
/* container[sub] = v */
err = PyObject_SetItem(container, sub, v);
Py_DECREF(v);
PyObject *sub = TOP();
PyObject *container = SECOND();
int err;
- STACKADJ(-2);
+ STACK_SHRINK(2);
/* del container[sub] */
err = PyObject_DelItem(container, sub);
Py_DECREF(container);
}
} else if (unpack_iterable(seq, oparg, -1,
stack_pointer + oparg)) {
- STACKADJ(oparg);
+ STACK_GROW(oparg);
} else {
/* unpack_iterable() raised an exception */
Py_DECREF(seq);
PyObject *owner = TOP();
PyObject *v = SECOND();
int err;
- STACKADJ(-2);
+ STACK_SHRINK(2);
err = PyObject_SetAttr(owner, name, v);
Py_DECREF(v);
Py_DECREF(owner);
err = PySet_Add(set, item);
Py_DECREF(item);
}
- STACKADJ(-oparg);
+ STACK_SHRINK(oparg);
if (err != 0) {
Py_DECREF(set);
goto error;
PyObject *value = SECOND();
PyObject *map;
int err;
- STACKADJ(-2);
+ STACK_SHRINK(2);
map = PEEK(oparg); /* dict */
assert(PyDict_CheckExact(map));
err = PyDict_SetItem(map, key, value); /* map[key] = value */
PyObject *cond = TOP();
int err;
if (cond == Py_True) {
- STACKADJ(-1);
+ STACK_SHRINK(1);
Py_DECREF(cond);
FAST_DISPATCH();
}
}
err = PyObject_IsTrue(cond);
if (err > 0) {
- STACKADJ(-1);
+ STACK_SHRINK(1);
Py_DECREF(cond);
}
else if (err == 0)
PyObject *cond = TOP();
int err;
if (cond == Py_False) {
- STACKADJ(-1);
+ STACK_SHRINK(1);
Py_DECREF(cond);
FAST_DISPATCH();
}
JUMPTO(oparg);
}
else if (err == 0) {
- STACKADJ(-1);
+ STACK_SHRINK(1);
Py_DECREF(cond);
}
else
PyErr_Clear();
}
/* iterator ended normally */
- STACKADJ(-1);
+ STACK_SHRINK(1);
Py_DECREF(iter);
JUMPBY(oparg);
PREDICT(POP_BLOCK);
val = tb = Py_None;
exc = TOP();
if (exc == NULL) {
- STACKADJ(-1);
+ STACK_SHRINK(1);
exit_func = TOP();
SET_TOP(exc);
exc = Py_None;