From 0f22b32cb68eef2cd058f59450949ff2c0fbb705 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 14 Feb 2022 14:26:31 +0000 Subject: [PATCH 1/4] Make sure all backward jumps use JUMP_ABSOLUTE. --- Lib/test/test_compile.py | 10 ++++++++++ Python/compile.c | 5 +++++ 2 files changed, 15 insertions(+) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index 096501513c14bb..7ebe837fce1b51 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1008,6 +1008,16 @@ def if_else_break(): elif instr.opname in HANDLED_JUMPS: self.assertNotEqual(instr.arg, (line + 1)*INSTR_SIZE) + def test_no_wraparound_jump(self): + # See https://bugs.python.org/issue46724 + + def while_not_chained(a, b, c): + while not (a < b < c): + pass + + for instr in dis.Bytecode(while_not_chained): + self.assertNotEqual(instr.opname, "EXTENDED_ARG") + @requires_debug_ranges() class TestSourcePositions(unittest.TestCase): # Ensure that compiled code snippets have correct line and column numbers diff --git a/Python/compile.c b/Python/compile.c index bfe451b8c10479..f9d32994aa3bb1 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -7530,6 +7530,11 @@ normalize_jumps(struct assembler *a) last->i_opcode = JUMP_FORWARD; } } + if (last->i_opcode == JUMP_FORWARD) { + if (last->i_target->b_visited) { + last->i_opcode = JUMP_ABSOLUTE; + } + } } } From 413570cc935867f2f1b81682e59eea0bb97451d0 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 14 Feb 2022 14:44:20 +0000 Subject: [PATCH 2/4] Add news. --- .../Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst new file mode 100644 index 00000000000000..3917037937e5a2 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst @@ -0,0 +1,2 @@ +Make sure that all backwards jumps use the JUMP_ABSOLUTE instruction, rather +than a JUMP_FORWARD jump of (2**32)+offset. From 4bea330a0993f26a038b65ccc6ee305e64984f58 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 14 Feb 2022 14:47:47 +0000 Subject: [PATCH 3/4] Fix up news item. --- .../2022-02-14-14-44-06.bpo-46724.jym_K6.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst index 3917037937e5a2..7324182677abfd 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2022-02-14-14-44-06.bpo-46724.jym_K6.rst @@ -1,2 +1,2 @@ -Make sure that all backwards jumps use the JUMP_ABSOLUTE instruction, rather -than a JUMP_FORWARD jump of (2**32)+offset. +Make sure that all backwards jumps use the ``JUMP_ABSOLUTE`` instruction, rather +than ``JUMP_FORWARD`` with an argument of ``(2**32)+offset``. From 525a1dd463d38e820db82a85ea2e4971cda24dbe Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Mon, 14 Feb 2022 18:03:02 +0000 Subject: [PATCH 4/4] Make test use consistent style. --- Python/compile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/compile.c b/Python/compile.c index f9d32994aa3bb1..1f2e571841857d 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -7531,7 +7531,7 @@ normalize_jumps(struct assembler *a) } } if (last->i_opcode == JUMP_FORWARD) { - if (last->i_target->b_visited) { + if (last->i_target->b_visited == 1) { last->i_opcode = JUMP_ABSOLUTE; } }