def make_label(path, dict_vuln2testcase, _type):
f = open(path, 'r')
context = f.read().split('------------------------------')[:-1]
f.close()
context[0] = '\n' + context[0]
list_all_label = []
list_all_vulline = []
for _slice in context:
vulline = []
index_line = _slice.split('\n')[1]
list_codes = _slice.split('\n')[2:-1]
case_name = index_line.split(' ')[1]
key_name = '/'.join(index_line.split(' ')[1].split('/')[-2:])
print index_line
if key_name in dict_vuln2testcase.keys():
list_codeline = [code.split(' ')[-1] for code in list_codes]
dicti = dict_vuln2testcase[key_name]
_dict_cwe2line_target = {}
_dict_cwe2line = {}
dicto = dicti
for _dict in dicto:
for key in _dict.keys():
if _dict[key] not in _dict_cwe2line_target.keys():
_dict_cwe2line_target[_dict[key]] = [key]
else:
_dict_cwe2line_target[_dict[key]].append(key)
for line in list_codeline:
line = line.strip()
if line in _dict.keys():
if not ' '.join((list_codes[list_codeline.index(line)].strip()).split(' ')[:-1]) == dict_testcase2code[key_name+"/"+line].strip():
continue
cweid = _dict[line]
vulline.append(list_codeline.index(line))
if cweid not in _dict_cwe2line.keys():
_dict_cwe2line[cweid] = [line]
else:
_dict_cwe2line[cweid].append(line)
if _type:
list_vuln_cwe = []
for key in _dict_cwe2line.keys():
if key == 'Any...':
continue
if len(_dict_cwe2line[key]) == len(_dict_cwe2line_target[key]):
label_cwe = []
label_cwe = get_label_cwe(key, label_cwe)
list_vuln_cwe += label_cwe
else:
list_vuln_cwe = []
for key in _dict_cwe2line.keys():
if key == 'Any...':
continue
label_cwe = []
label_cwe = get_label_cwe(key, label_cwe)
list_vuln_cwe += label_cwe
if list_vuln_cwe == []:
list_label = [0] * len(label_vec_type)
else:
list_vuln_cwe = list(set(list_vuln_cwe))
list_label = get_label_veclist(list_vuln_cwe)
else:
list_label = [0] * len(label_vec_type)
list_all_label.append(list_label)
list_all_vulline.append(vulline)
return list_all_label, list_all_vulline
def main():
f = open("dict_flawline2filepath.pkl", 'rb')
dict_vuln2testcase = pickle.load(f)
f.close()
_type = False
time = '4'
lang = 'C/test_data/' + time
path = os.path.join(lang, 'api_slices.txt')
list_all_apilabel, list_all_vulline = make_label(path, dict_vuln2testcase, _type)
我收到此错误:
1 /home/user/project/joern-0.3.1/mytestcode/NVD/CVE-2010-2068/CVE_2010_2068_PATCHED_ap_proxy_http_process_response.c sscanf 122
Traceback (most recent call last):
File "make_label.py", line 167, in <module>
main()
File "make_label.py", line 134, in main
list_all_apilabel, list_all_vulline = make_label(path, dict_vuln2testcase, _type)
File "make_label.py", line 71, in make_label
for _dict in dict:
TypeError: 'type' object is not iterable
如何解决?我必须在哪里更改此typeerror?请说明为什么会发生此错误!!!我无法更改此错误。我尝试了很多更改此错误的方法!实际上,该程序试图为我为python程序提供输入的易受攻击的代码做标签。
答案 0 :(得分:2)
只需将dict
传递到您的函数中即可:
def make_label(path, dict_vuln2testcase, _type, dicti):
# most contents elided
# typical usage:
for _dict in dicti:
def main():
f = open("dict_flawline2filepath.pkl", 'rb')
# details elided
dicti = {}
list_all_apilabel, list_all_vulline = make_label(path, dict_vuln2testcase, _type, dicti)