Skip to content

Commit

Permalink
added solution prototype for '&&' conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidanio committed Mar 19, 2024
1 parent b0460a8 commit 31ac033
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/linter/block_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package linter
import (
"bytes"
"fmt"
"reflect"
"strings"

"github.com/VKCOM/noverify/src/constfold"
Expand Down Expand Up @@ -764,40 +763,57 @@ func (b *blockLinter) checkIfStmt(s *ir.IfStmt) {
}

func (b *blockLinter) checkDangerousBoolCond(s *ir.IfStmt) {

cond, ok := s.Cond.(*ir.BooleanOrExpr)
if !ok {
switch c := s.Cond.(type) {
case *ir.ConstFetchExpr:
if c.Constant.Value == "true" || c.Constant.Value == "false" {
b.report(s, LevelWarning, "DangerousCondition", "Potential dangerous bool value: you have constant bool value in condition")
fmt.Println("Bad")
}

case *ir.Lnumber:
if c.Value == "0" || c.Value == "1" {
b.report(s, LevelWarning, "DangerousCondition", "Potential dangerous value: you have constant int value that interpreted as bool")
fmt.Println("Bad")
}
case *ir.BooleanAndExpr:
checkIfStatementConditionBool(c.Left, c.Right, b)
}
return
}

println(cond)
checkIfStatementConditionBool(cond.Left, cond.Right)
checkIfStatementConditionBool(cond.Left, cond.Right, b)
}
func checkIfStatementConditionBool(left ir.Node, right ir.Node) {
checkNode(left)

checkNode(right)
func checkIfStatementConditionBool(left ir.Node, right ir.Node, b *blockLinter) {
checkNode(left, b)
checkNode(right, b)
}

func checkNode(node ir.Node) {
func checkNode(node ir.Node, b *blockLinter) {
switch n := node.(type) {
case *ir.SimpleVar:
fmt.Println("SimpleVar:", n)
case *ir.ConstFetchExpr:

if n.Constant.Value == "true" || n.Constant.Value == "false" {
b.report(node, LevelWarning, "DangerousCondition", "Potential dangerous bool value: you have constant bool value in condition")
fmt.Println("Bad")
}

case *ir.Lnumber:
if n.Value == "0" || n.Value == "1" {
b.report(node, LevelWarning, "DangerousCondition", "Potential dangerous value: you have constant int value that interpreted as bool")
fmt.Println("Bad")
}
case *ir.BooleanOrExpr:
checkNode(n.Left)
checkNode(n.Right)
default:
fmt.Println("Unknown type:", reflect.TypeOf(node))
checkNode(n.Left, b)
checkNode(n.Right, b)

case *ir.BooleanAndExpr:
checkNode(n.Left, b)
checkNode(n.Right, b)
/*default:
fmt.Println("Unknown type:", reflect.TypeOf(node))*/
}

}
Expand Down
10 changes: 10 additions & 0 deletions src/linter/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,16 @@ function main(): void {
After: `(string)$x`,
},

{
Name: "DangerousCondition",
Default: true,
Quickfix: false,
Comment: "Report a dangerous condition",
Before: "if(true){}",
After: `$a = getCond(); // get bool value from some func
if($a){}`,
},

{
Name: "reverseAssign",
Default: true,
Expand Down
59 changes: 59 additions & 0 deletions src/tests/checkers/dangerouse_condition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package checkers

import (
"github.com/VKCOM/noverify/src/linttest"

Check failure on line 4 in src/tests/checkers/dangerouse_condition_test.go

View workflow job for this annotation

GitHub Actions / Build

File is not `goimports`-ed (goimports)
"testing"
)

func TestDangerousCondition1(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
if(true){
}
`)
test.Expect = []string{
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:2`,
}
test.RunAndMatch()
}

func TestDangerousCondition2(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
$a = true;
if(true||$a){
echo "test";
}
if(1||$a||1||true||false||0){
}
`)
test.Expect = []string{
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:4`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:8`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:8`,
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:8`,
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:8`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:8`,
}
test.RunAndMatch()
}

func TestDangerousCondition3(t *testing.T) {
test := linttest.NewSuite(t)
test.AddFile(`<?php
$a = true;
if($a && false && true && 1 && 0){
}
`)
test.Expect = []string{
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:4`,
`Potential dangerous bool value: you have constant bool value in condition at _file0.php:4`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:4`,
`Potential dangerous value: you have constant int value that interpreted as bool at _file0.php:4`,
}
test.RunAndMatch()
}

0 comments on commit 31ac033

Please sign in to comment.