Skip to content

Commit a9e2166

Browse files
committed
Prevent traceback loops
1 parent 856b36c commit a9e2166

File tree

1 file changed

+11
-0
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/traceback

1 file changed

+11
-0
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/traceback/TracebackBuiltins.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package com.oracle.graal.python.builtins.objects.traceback;
2727

2828
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
29+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
2930
import static com.oracle.graal.python.builtins.objects.traceback.PTraceback.TB_FRAME;
3031
import static com.oracle.graal.python.builtins.objects.traceback.PTraceback.TB_LASTI;
3132
import static com.oracle.graal.python.builtins.objects.traceback.PTraceback.TB_LINENO;
@@ -58,6 +59,7 @@
5859
import com.oracle.truffle.api.frame.VirtualFrame;
5960
import com.oracle.truffle.api.nodes.Node;
6061
import com.oracle.truffle.api.profiles.ConditionProfile;
62+
import com.oracle.truffle.api.profiles.LoopConditionProfile;
6163
import com.oracle.truffle.api.source.SourceSection;
6264

6365
@CoreFunctions(extendClasses = PythonBuiltinClassType.PTraceback)
@@ -271,7 +273,16 @@ Object get(PTraceback self, @SuppressWarnings("unused") PNone none,
271273

272274
@Specialization(guards = "!isNoValue(next)")
273275
Object set(PTraceback self, PTraceback next,
276+
@Cached("createCountingProfile()") LoopConditionProfile loopProfile,
274277
@Cached MaterializeTruffleStacktraceNode materializeTruffleStacktraceNode) {
278+
// Check for loops
279+
PTraceback tb = next;
280+
while (loopProfile.profile(tb != null)) {
281+
if (tb == self) {
282+
throw raise(ValueError, "traceback loop detected");
283+
}
284+
tb = tb.getNext();
285+
}
275286
// Realize whatever was in the truffle stacktrace, so that we don't overwrite the
276287
// user-set next later
277288
materializeTruffleStacktraceNode.execute(self);

0 commit comments

Comments
 (0)