Class: Pedant::CheckFlippedOperandsOnMatchOrSubstring
- Inherits:
-
Check
- Object
- Check
- Pedant::CheckFlippedOperandsOnMatchOrSubstring
show all
- Defined in:
- lib/pedant/checks/flipped_operands_on_match_or_substring.rb
Instance Attribute Summary
Attributes inherited from Check
#result
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Check
all, depends, #fail, #fatal, friendly_name, inherited, #initialize, initialize!, list, #pass, provides, ready?, #report, run_checks_in_dependency_order, #skip, #warn
Constructor Details
This class inherits a constructor from Pedant::Check
Class Method Details
.requires ⇒ Object
29
30
31
|
# File 'lib/pedant/checks/flipped_operands_on_match_or_substring.rb', line 29
def self.requires
super + [:trees]
end
|
Instance Method Details
#check(file, tree) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/pedant/checks/flipped_operands_on_match_or_substring.rb', line 33
def check(file, tree)
def walk(node, root)
def is_get_kb_item_with_literal? node
return true if node.is_a?(Nasl::Call) and
node.name.ident.name == 'get_kb_item' and
node.name.indexes == [] and
node.args.length == 1 and
node.args.first.expr.is_a?(Nasl::String)
return false
end
if node.is_a? Nasl::Expression
[:lhs, :rhs].each { |side| walk(node.send(side), root) }
return unless node.op.is_a?(Nasl::Token)
if ["=~", "!~"].include?(node.op.body)
side = :lhs
opposite = :rhs
end
if ["><", ">!<"].include?(node.op.body)
side = :rhs
opposite = :lhs
end
return if side.nil?
if node.send(side).is_a?(Nasl::String) && (node.send(opposite).is_a?(Nasl::Lvalue) && node.send(opposite).indexes == []) or is_get_kb_item_with_literal?(node.send(opposite))
warn
report(:error, "A '#{node.op.body}' operator has a literal string on the #{if side == :lhs then 'left' else 'right' end}-hand side.")
report(:error, "The operands may be accidentally swapped.")
report(:error, node.send(side).context(node))
end
end
end
cond_stmts = [:For, :Repeat, :While, :If].map { |cls| tree.all(cls) }.flatten
cond_stmts.each { |cond_stmt| walk(cond_stmt.cond, cond_stmt) }
end
|
#is_get_kb_item_with_literal?(node) ⇒ Boolean
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/pedant/checks/flipped_operands_on_match_or_substring.rb', line 35
def is_get_kb_item_with_literal? node
return true if node.is_a?(Nasl::Call) and
node.name.ident.name == 'get_kb_item' and
node.name.indexes == [] and
node.args.length == 1 and
node.args.first.expr.is_a?(Nasl::String)
return false
end
|
#run ⇒ Object
94
95
96
97
98
99
100
|
# File 'lib/pedant/checks/flipped_operands_on_match_or_substring.rb', line 94
def run
pass
@kb[:trees].each { |file, tree| check(file, tree) }
end
|
#walk(node, root) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/pedant/checks/flipped_operands_on_match_or_substring.rb', line 34
def walk(node, root)
def is_get_kb_item_with_literal? node
return true if node.is_a?(Nasl::Call) and
node.name.ident.name == 'get_kb_item' and
node.name.indexes == [] and
node.args.length == 1 and
node.args.first.expr.is_a?(Nasl::String)
return false
end
if node.is_a? Nasl::Expression
[:lhs, :rhs].each { |side| walk(node.send(side), root) }
return unless node.op.is_a?(Nasl::Token)
if ["=~", "!~"].include?(node.op.body)
side = :lhs
opposite = :rhs
end
if ["><", ">!<"].include?(node.op.body)
side = :rhs
opposite = :lhs
end
return if side.nil?
if node.send(side).is_a?(Nasl::String) && (node.send(opposite).is_a?(Nasl::Lvalue) && node.send(opposite).indexes == []) or is_get_kb_item_with_literal?(node.send(opposite))
warn
report(:error, "A '#{node.op.body}' operator has a literal string on the #{if side == :lhs then 'left' else 'right' end}-hand side.")
report(:error, "The operands may be accidentally swapped.")
report(:error, node.send(side).context(node))
end
end
end
|