Skip to content

Bug: Loop protection generates invalid JS from do while loop with standalone comment #4171

Description

@TerenceP1

p5.js version

No response

What is your operating system?

Windows

Web browser and version

Edge 148.0.3967.83

Actual Behavior

When transforming a do-while loop with a standalone comment, the infinite loop guard erroneously treats the while (...); as if it were the start of a standalone while loop.

Expected Behavior

The loop guard should not be treating the while statement after a do while loop as standalone and should not add an infinite loop detector.

Steps to reproduce

Steps:

  1. Make a project, whether its saves or not or version doesn't matter.
  2. Paste in the following snippet into anywhere, it can be setup, draw, or even outside any function
  3. Press run (also crashes in the full view)

Snippet:

  do{
    // Hello this is a comment and this comment is very important to this bug
  }while (false);

Link is here

Transformed code

The code that actually runs according to inspect is here:

  {;loopProtect.protect({ line: 7, reset: true }); do{;if (loopProtect.protect({ line: 7 })) break;

  }{;loopProtect.protect({ line: 9, reset: true }); while (false){if (loopProtect.protect({ line: 9 })) break;;}}

When the bug occurs

This bug only occurs in a do while loop. When the do statement has no comment, the bug does not occur. A inline comment does not trigger this. Both single and multi-line comments trigger this. This does not happen when the loop has // noprotect. EDIT: This also can trigger when code is within the do clause as long as the comment is standalone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting Maintainer ApprovalNeeds review from a maintainer before moving forwardBugError or unexpected behaviors

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions