From b52ac3d430e9ecfca1db2b29e7edc4928852d99d 2018-05-27 17:38:22 From: Thomas Kluyver Date: 2018-05-27 17:38:22 Subject: [PATCH] Prevent infinite loops in input transformation --- diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index 6ccfd0e..624e07a 100644 --- a/IPython/core/inputtransformer2.py +++ b/IPython/core/inputtransformer2.py @@ -405,6 +405,9 @@ def show_linewise_tokens(s: str): for tokinfo in line: print(" ", tokinfo) +# Arbitrary limit to prevent getting stuck in infinite loops +TRANSFORM_LOOP_LIMIT = 500 + class TransformerManager: def __init__(self): self.cleanup_transforms = [ @@ -451,11 +454,14 @@ class TransformerManager: return True, transformer.transform(lines) def do_token_transforms(self, lines): - while True: + for _ in range(TRANSFORM_LOOP_LIMIT): changed, lines = self.do_one_token_transform(lines) if not changed: return lines + raise RuntimeError("Input transformation still changing after " + "%d iterations. Aborting." % TRANSFORM_LOOP_LIMIT) + def transform_cell(self, cell: str): if not cell.endswith('\n'): cell += '\n' # Ensure the cell has a trailing newline