From 31ac033c1594fbabd513bbe5c1c4185f4ffe3829 Mon Sep 17 00:00:00 2001 From: Richardas Kuchinskas Date: Tue, 19 Mar 2024 19:48:19 +0300 Subject: [PATCH] added solution prototype for '&&' conditions --- src/linter/block_linter.go | 44 +++++++++----- src/linter/report.go | 10 ++++ .../checkers/dangerouse_condition_test.go | 59 +++++++++++++++++++ 3 files changed, 99 insertions(+), 14 deletions(-) create mode 100644 src/tests/checkers/dangerouse_condition_test.go diff --git a/src/linter/block_linter.go b/src/linter/block_linter.go index 972898e3..89de957b 100644 --- a/src/linter/block_linter.go +++ b/src/linter/block_linter.go @@ -3,7 +3,6 @@ package linter import ( "bytes" "fmt" - "reflect" "strings" "github.com/VKCOM/noverify/src/constfold" @@ -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))*/ } } diff --git a/src/linter/report.go b/src/linter/report.go index d1914da3..04a51170 100644 --- a/src/linter/report.go +++ b/src/linter/report.go @@ -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, diff --git a/src/tests/checkers/dangerouse_condition_test.go b/src/tests/checkers/dangerouse_condition_test.go new file mode 100644 index 00000000..678439c9 --- /dev/null +++ b/src/tests/checkers/dangerouse_condition_test.go @@ -0,0 +1,59 @@ +package checkers + +import ( + "github.com/VKCOM/noverify/src/linttest" + "testing" +) + +func TestDangerousCondition1(t *testing.T) { + test := linttest.NewSuite(t) + test.AddFile(`