Skip to content

Conversation

@tausbn
Copy link
Contributor

@tausbn tausbn commented Feb 6, 2025

Observed on some test files in Nuitka/Nuitka, having break and continue outside of loops in Python is (to Python) a syntax error, but our parser happily accepted this broken syntax.

This then caused issues further downstream in the control-flow construction, as it broke some invariants.

To fix this we now skip the code that would previously fail when the invariants are broken.

Observed on some test files in Nuitka/Nuitka, having `break` and
`continue` outside of loops in Python is (to Python) a syntax error, but
our parser happily accepted this broken syntax.

This then caused issues further downstream in the control-flow
construction, as it broke some invariants.

To fix this we now skip the code that would previously fail when the
invariants are broken.

Co-authored-by: yoff <yoff@github.com>
Copilot AI review requested due to automatic review settings February 6, 2025 14:31
@tausbn tausbn requested a review from a team as a code owner February 6, 2025 14:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Overview

This pull request addresses the handling of Python’s "break" and "continue" statements that appear outside of valid loop constructs. Previously, such occurrences were accepted as syntactically valid, which caused downstream failures in control-flow construction. Now, the parser code checks for loop context before adding flow information, preventing these failures.

  • Adds a test file demonstrating "break" and "continue" usage outside a loop.
  • Updates a change note to reflect this fix.
  • Updates the flow analysis code to skip the invalid "break" or "continue" when they appear outside a loop context.

Changes

File Description
python/ql/test/extractor-tests/syntax_error/without_loop.py Adds a test file with intentionally invalid Python code (break/continue outside loops).
python/ql/lib/change-notes/2025-02-06-robustly-handle-loop-constructs.md Documents the fix affecting "break" and "continue" statements in invalid contexts.
python/extractor/semmle/python/passes/flow.py Updates flow analysis to gracefully handle out-of-loop "break" and "continue" statements.

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Tip: If you use Visual Studio Code, you can request a review from Copilot before you push from the "Source Control" tab. Learn more

Copy link
Contributor

@yoff yoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yoff yoff merged commit 381cc20 into main Feb 6, 2025
13 checks passed
@yoff yoff deleted the tausbn/python-robustly-handle-loop-constructs branch February 6, 2025 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants