Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BooleanExpressionsRule can generate incorrect code. #1284

Closed
HaukeRa opened this issue May 2, 2022 · 2 comments · Fixed by #1295
Closed

BooleanExpressionsRule can generate incorrect code. #1284

HaukeRa opened this issue May 2, 2022 · 2 comments · Fixed by #1295
Assignees
Labels
bug Something isn't working
Milestone

Comments

@HaukeRa
Copy link

HaukeRa commented May 2, 2022

Describe the bug

The fixBooleanExpression method in BooleanExpressionsRule can generate incorrect code.

Any boolean logic is represented there as a String with each of the elemental subexpressions as a single uppercase letter.
Example:
A & G & !D & (!B | !C | !H)

As a last step, the placeholder letters are replaced with the actual expressions:
https://github.com/analysis-dev/diktat/blob/90b852939fb5b606e96611004435c8038e991982/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt#L170

The problem arises when any of the already replaced expressions contains one of the yet to replace characters.
Example:
A & B & !C
A --> isBold()
B --> foo
C --> bar

First A is replaced:
isBold() & B & !C

When now B is replaced, actually all instanced from the beginning of the whole expression String are replaced:
isfooold() & foo & C

and so on ...

Expected behavior

Correct code is generated for arbitrarily complex boolean expressions

Observed behavior

Exception KotlinParseException("Text is not valid: [...]") when the incorrectly generated code is parsed.

Usually this results in invalid code that can not be parsed, but surely an example can be crafted that goes unnoticed, potentially permanently changing the code logic (which easily slips review when relying on the correct behaviour of a code formatting tool)

Environment information

  • diktat version: 1.1.0
  • build tool (maven/gradle): gradle
  • how is diktat run (CLI, plugin, etc.): spotless
  • kotlin version: 1.6.20
  • operating system: windows/ubuntu
@HaukeRa HaukeRa added the bug Something isn't working label May 2, 2022
@orchestr7
Copy link
Member

orchestr7 commented May 2, 2022

@HaukeRa thank you for this report! Boolean logic in diktat is quite complex, so we will need some time to investigate it… right now you can disable this inspection using Suppress annotation

@orchestr7 orchestr7 added this to the 1.1.1 milestone May 15, 2022
@nulls
Copy link
Member

nulls commented May 25, 2022

@HaukeRa, we fixed this issue. We will release a new version with this fix approximately at one month.
thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants