-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrisk_block_scanner.py
More file actions
249 lines (195 loc) · 7.42 KB
/
risk_block_scanner.py
File metadata and controls
249 lines (195 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
#coding=utf-8
#---------------------------------------------
# Name: blockScanner.py
# Author: Xing Chen
# Date: 2016-07-15
# Description: blockScanner is a text Scanner
# which could detect simple circular reference
# Support Object-C only now.
#----------------------------------------------
import re
import os
import sys
import fileinput
weak_regex=ur"\@weakify\(.*\)"
block_regex=ur"(self\..*\=\s?(.*)?\^|\[self.*\^)\(.*\).*\{?"
func_regex=ur"(\-|\+)\s?\(.*\).*(\:\s?\(.*\).*)?{?"
singleton_regex=ur"(\+\s?\(.*\)\s?(shared|default).*{?|.*SINGLETON\_FOR\_CLASS\(.*\))"
ignore_regex=ur"(mas_.*Constraints\:|enumerateObjectsUsingBlock\:|enumerateAttribute\:)"
## Multi-lines case is hard to cover.
block_line_start_regex=ur"(self\..*\=|\[self.*)"
block_line_end_regex=ur"\^(\(.*\))?.*\{"
show_detail=0
show_more=0
show_singleton=0
show_file_path=0
def scan_files(directory,prefix=None,postfix=None):
files_list=[]
for root, sub_dirs, files in os.walk(directory):
for special_file in files:
if postfix:
if special_file.endswith(postfix):
files_list.append(os.path.join(root,special_file))
elif prefix:
if special_file.startswith(prefix):
files_list.append(os.path.join(root,special_file))
else:
files_list.append(os.path.join(root,special_file))
return files_list
def left_bracket_count(line):
count=0
for word in line:
if word=="{":
count=count+1
elif word=="}":
count=count-1
return count;
def detect_block(file_path):
global show_detail
global show_singleton
line_count=1
potential_blc=0
block_arr=[]
weak_arr=[]
func_arr=[]
potential_arr=[]
cycref_set=set()
safe_set=set()
cycref_map={}
bracket_map={}
is_singleton=0
for line in fileinput.input(file_path):
# comments code is invalid
# object-c comments is startswith "//"
if not line.strip().startswith("//"):
if re.findall(weak_regex, line):
weak_arr.append(line_count)
elif re.findall(func_regex, line):
func_arr.append(line_count)
elif re.findall(block_line_start_regex, line):
if re.findall(block_regex, line):
block_arr.append(line_count);
if not re.findall(ignore_regex, line):
potential_arr.append(line_count);
else:
safe_set.add(line_count);
bracket_map[line_count] = left_bracket_count(line);
cycref_map[line_count] = 0;
elif not re.findall(ur"(\;|\&\&|\{)$", line):
potential_blc = line_count
if potential_blc > 0:
if re.findall(block_line_end_regex, line):
potential_arr.append(line_count);
block_arr.append(line_count);
bracket_map[line_count] = left_bracket_count(line);
cycref_map[line_count] = 0;
potential_blc = 0
elif re.findall(ur"(\;|\&\&|\{)$", line) or re.findall(func_regex, line):
potential_blc = 0
if re.findall(singleton_regex, line):
is_singleton=1
for potential_lc in potential_arr:
if potential_lc==line_count:
continue
bracket_map[potential_lc]=bracket_map[potential_lc]+left_bracket_count(line)
if bracket_map[potential_lc]<=0:
if cycref_map[potential_lc]==1:
cycref_set.add(potential_lc)
else:
safe_set.add(potential_lc)
break
if cycref_map[potential_lc]==0:
if re.findall(ur"(\[self\s|self\.|make.*\(self\))", line):
cycref_map[potential_lc]=1
# remove potential_arr if it's not cycle reference
potential_arr = list(set(potential_arr).difference(cycref_set))
potential_arr = list(set(potential_arr).difference(safe_set))
line_count=line_count+1
pass
#weak_arr detect
weakified_set=set()
# last line for last function
func_arr.append(line_count)
for i in range(0, len(func_arr)-1):
fst_line=func_arr[i]
sec_line=func_arr[i+1]
for cr_line in list(cycref_set):
if cr_line > fst_line and cr_line < sec_line:
for weak_line in weak_arr:
if weak_line > fst_line and weak_line < cr_line:
weakified_set.add(cr_line)
#except for block which weak before perform
unsafe_arr=list(cycref_set.difference(weakified_set))
if show_singleton==0 and is_singleton==1:
unsafe_arr=[]
if show_detail==1:
show_detail_info(file_path, unsafe_arr, block_arr, list(cycref_set), list(weakified_set))
unsafe_arr.sort()
return unsafe_arr
def show_detail_info(file_path, unsafe_arr, block_arr, cycref_arr, weakified_arr):
global show_more
global show_file_path
cycref_arr.sort()
weakified_arr.sort()
unsafe_arr.sort()
arr_len = len(unsafe_arr)
if show_more:
arr_len = len(block_arr)
if show_file_path:
print_name = file_path
else:
print_name = file_name(file_path)
if arr_len:
print print_name
print "All Block Lines: %s" % block_arr
print "Self-Block Lines: %s" % cycref_arr
print "Weak-Block Lines: %s" % weakified_arr
print "Risk-Block Lines: %s" % unsafe_arr
def file_name(file_path):
name_arr=file_path.split("/")
file_name=name_arr[len(name_arr) - 1]
return file_name
def main():
# Accoring to Argv as a Scanner Target root path
root_path=sys.argv[1]
global show_detail
global show_more
global show_singleton
global show_file_path
for arg in sys.argv:
if arg=="--detail":
show_detail=1
elif arg=="--more":
show_more=1
elif arg=="--show-singleton":
show_singleton=1
elif arg=="--show-filepath":
show_file_path=1
if os.path.isdir(root_path):
total_risk_block_count=0
total_file_count=0
for file_path in scan_files(root_path, None, ".m"):
risk_arr = detect_block(file_path)
risk_arr_len = len(risk_arr)
if risk_arr_len:
total_file_count = total_file_count+1
total_risk_block_count = total_risk_block_count+risk_arr_len
if show_file_path:
print_name = file_path
else:
print_name = file_name(file_path)
if not show_detail:
print "%s --> %s" % (print_name, risk_arr)
print "\nTotal Risk File Count: %d, Total Risk Line Count: %s" % (total_file_count, total_risk_block_count)
else:
risk_arr=detect_block(root_path)
risk_arr_len = len(risk_arr)
if show_file_path:
print_name = file_path
else:
print_name = file_name(file_path)
if not show_detail:
print "%s --> %s" % (print_name, risk_arr)
print "\nTotal Risk Line Count: %s" % risk_arr_len
main()