This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·573 lines (486 loc) · 20 KB
/
main.py
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/bin/python3
import os
import subprocess
import sys
import fileinput
import re
VERSION = "0.3.0"
SUPPORTED_CODING_STYLES = ["2020", "2021"]
class color:
MAJOR = '\033[91m'
MINOR = '\033[92m'
INFO = '\033[94m'
NORMAL = '\033[0m'
year = "2021"
strict = False
stdin_is_header = False
blacklist = []
allowed_syscalls = []
disallowed_syscalls = []
def usage():
print("cnormitek " + VERSION)
print()
print(
"Please use it as shown : cnormitek [folder] "
"[--allowed=malloc,free,...] [--no-CODE]\n")
print("If you think this is an error please open an issue!")
print()
print("OPTIONS")
print("\t--allowed=<functions>")
print("\t\tA comma-separated list of allowed system calls")
print("\t\tIf omitted, all system calls are allowed, unless those specified with --disallowed")
print()
print("\t--disallowed=<functions>")
print("\t\tA comma-separated list of disallowed system calls")
print()
print("FLAGS")
print("\t--cs-2020, --cs-2021\tSelect the prefered coding style (default: cs-" + str(year) + ")")
print("\t--strict\t\tallow more strict error checks (cause more false positives), also enables checks for " + ', '.join(strict_error_checks))
print("\t--no-gitignore\t\tdo not read .gitignore files")
print("\t--no-color\t\tdo not show colors")
errors_tuple = list(dict([
(error[:-5], errors[error][0]) if error.endswith("-2020") or error.endswith("-2021")
else (error, errors[error][0]) for error in errors
]).items())
for (error, desc) in errors_tuple:
strict_check_message = ""
if error in strict_error_checks:
strict_check_message = "[when strict only] "
spacing = "\t" * (1 + 1 * (len(error) < 8))
print("\t--no-" + error + " " + spacing + strict_check_message +
"ignore " + error + " (" + desc + ")")
exit()
header_regex = (
r"^"
r"\/\*\n"
r"\*\* EPITECH PROJECT, [0-9]{4}\n"
r"\*\* (.*)\n"
r"\*\* File description:\n"
r"\*\* (.*)\n"
r"\*\/"
)
makefile_header_regex = (
r"^"
r"##\n"
r"## EPITECH PROJECT, [0-9]{4}\n"
r"## (.*)\n"
r"## File description:\n"
r"## (.*)\n"
r"##"
)
forbidden_syscall_regex = (
r'(?:^|[^0-9a-zA-Z_])(printf|dprintf|fprintf|vprintf|sprintf|snprintf'
r'|vprintf|vfprintf|vsprintf|vsnprintf|asprintf|scranf|memcpy|memset'
r'|memmove|strcat|strchr|strcpy|atoi|strlen|strstr|strncat|strncpy'
r'|strcasestr|strncasestr|strcmp|strncmp|strtok|strnlen|strdup|realloc'
r'|write|free|malloc|opendir|readdir|closedir|stat|lstat|getpwuid'
r'|getgrgid|time|ctime|readlink|perror|strerrir|exit|calloc|realloc'
r'|qsort|bsearch|rand|srand|atof|atoi|atol|strtod|strtol|strtoll'
r'|strtoul|mblen|mbtowc|wctomb|mbstowcs|wcstombs|fscanf|scanf'
r'|sscanf|fopen|fflush|fclose|freopen|remove|rename|setbuf|setvbuf'
r'|tmpfile|tmpnam|fgetc|fgets|fputc|fputs|getc|getchar|gets|putc'
r'|putchar|puts|ungetc|fread|fwrite|fgetpos|fseek|fsetpos|ftell|rewind'
r'|clearerr|feof|perror|memcmp|strcmp|strcoll|strxfrm|strcspn'
r'|strpbrk|acos|asin|atan|atan2|ceil|cos|cosh|exp|fabs|floor|fmod|frexp'
r'|ldexp|log|log10|modf|pow|sin|sinh|sqrt|tan|tanh|clock|asctime'
r'|difftime|gmtime|localtime|mktime|strftime|assert|isalpha|isalnum'
r'|iscntrl|isdigit|isgraph|islower|isprint|inpunct|isspace|isupper'
r'|isxdigit|tolower|toupper|localeconv|setlocale|longjmp|setjmp'
r'|raise|signal|sigaction|atexit|div|abs|labs|ldiv|llabs|atoi'
r'|atol|strtod|strtol|strtoll|strtoul|wcstombs|mbstowcs|mblen|mbtowc'
r'|wctomb|malloc|calloc|realloc|free|abort|exit|getenv|system|rand|srand'
r'|qsort|bsearch|qsort_r|bsearch_r|lldiv|atoll|strtoull|strtouq|strtoul'
r'|strtoumax|strtof|strtold|strtoimax|strtol|strtoll|strtoumax|strtold'
r'|strtof|strtod|strtoq|strtok|strchr|strrchr|strstr|strcasestr|strncat'
r'|strspn|strcspn|strpbrk|strtok_r|strsep|strsignal|strverscmp|strxfrm'
r'|strcoll|strxfrm|strxfrm_l|strxfrm_l|strxfrm_l|strxfrm_l|strxfrm_l'
r'|strxfrm_l|strxfrm_l|strxfrm_l|strxfrm_l|strxfrm_l|strxfrm_l|strxfrm_l'
r'|sbrk|wbrk|mbrk|mallinfo|mallopt|malloc_info|malloc_stats|malloc_trim'
r'|posix_memalign|valloc|pvalloc|memalign|aligned_alloc|valloc|pvalloc'
r'|strtoupper|strtolower|assert'
r')( |\t)*\('
)
unnecessary_files_regex = (
r'('
r'^vgcore\.'
r'|\.(o|a|so|d|gcda|gcno|swp|elf|obj)$'
r'|^\#(.*)\#$'
r'|~$'
r')'
)
function_impl_regex = (
r"(?:^|\n)" # beginning of file or newline
r"(?:\w+?" # function type or keywords
r"(?: \w+?| \w+? )*?" # additional function type or keywords (if any)
r"([\* ]+?)" # pointer stars and spaces (group 1)
r")"
r"(\w+?)" # function name
r"\(" # function arguments open parenthesis
r"(" # function arguments content (group 2)
r"(?:"
r"\n[\t ]+?" # newline and spaces
r"|"
r"[^\)\{]" # anything but close parenthesis or open bracket
r")*?" # can have 0 or more arguments
r")"
r"\)" # function arguments close parenthesis
r"((?:\\[\r\n]|[\r\n\s])*?)" # newlines/spaces between args and body (group 3)
r"\{" # function body open bracket
r"(" # function body (group 4)
r"(?:"
r"\s*?" # each line of body CAN start with spaces (not enforced due to backtracking issues!)
r"(?:[^\n]*?)" # match any character except newline
r"(?:\n|\r)" # match newline or carriage return
r"|"
r"(?:\n|\r)" # maybe the line can alse be empty
r")*?" # a function have 0 or more lines in body
r")"
r"\}" # function body close bracket
)
macro_statement_regex = (
r"(?:\n|^)(#define) ([A-Za-z0-9]+)\([^\n]+\)"
r"(?:\\\n|\\\r\n| |\t)*?(?:|\()(?:\\\n|\\\r\n| |\t)*?\{"
)
misplaced_multiline_before_else_regex = (
r"}(?: |\t)*(?:\n|\r\n)(?: |\t)*else(?: |\t)+?"
)
misplaced_multiline_after_else_regex = (
r"(?: |\t)else(?:(?:\n|\r\n)|(?: |\t)+?(.*?)(?:\n|\r\n))(?: |\t)*?{"
)
strict_error_checks = [
"H3-2020"
]
errors = {
"F2": ("function name should be in snake_case", "major"),
"F3-2020": ("too many columns (CS2020)", "major"),
"F3-2021": ("too many columns (CS2021)", "major"),
"F4-2020": ("too long function (should be <=20 lines) (CS2020)", "major"),
"F4-2021": ("too long function (should be <=20 lines) (CS2021)", "major"),
"F5": ("too many arguments or missing void", "major"),
"G1-2020": ("bad or missing header (CS2020)", "major"),
"G1-2021": ("bad or missing header (CS2021)", "major"),
"O1": ("delivery folder should not contain unnecessary files", "major"),
"O3": ("too many functions in file", "major"),
"O4": ("file or folder should be named in snake_case", "major"),
"C2": ("only header files should contain macros and static inline functions", "major"),
"C1": ("probably too many conditions nested", "minor"),
"C3": ("goto is discouraged", "minor"),
"H2": ("no inclusion guard found", "minor"),
"H3-2020": ("macros should not be used for constants and should only match one statement (CS2020)", "minor"),
"L2": ("bad indentation", "minor"),
"L3": ("misplaced or missing space", "minor"),
"V3": ("pointer symbol is not attached to the name", "minor"),
"G2": ("only one empty line should separate the implementations of functions", "minor"),
"G7-2021": ("line endings must be done in UNIX style (LF) (CS2021)", "minor"),
"G8-2021": ("trailing space (CS2021)", "minor"),
"G9-2021": ("no more than one single trailing line must be present (CS2021)", "minor"),
"F6": ("no comment inside a function", "minor"),
"L6": ("one line break should be present to separate implementations from function remainder", "minor"),
"A3-2021": ("one single trailing line must be present", "info"),
"implicit_LF-2020": ("line endings must be done in UNIX style (LF) (CS2020)", "info"),
"implicit_L001-2020": ("trailing space (CS2020)", "info"),
"syscall": ("suspicious system call found", "info"),
}
def get_line_pos(string, pos):
line = 1
if pos > len(string):
raise IndexError("pos")
for i in range(0, pos):
char = string[i]
if char == "\r":
continue
if char == "\n":
line += 1
return line
def is_header_file(file):
if file == "stdin" and stdin_is_header:
return True
elif file == "stdin":
return False
return file.endswith(".h")
def glob_match(s1, s2):
escaped = re.escape(s2)
pattern = escaped.replace("\\*\\*", "(.*)").replace("\\*", "([^/]+)")
return re.search("^" + pattern + "$", s1) is not None
def is_file_ignored(file):
if "gitignore" in blacklist:
return False
file = os.path.abspath(file)
try:
output = subprocess.check_output(["git", "check-ignore", file], stderr=subprocess.STDOUT)
if output.strip() == "":
return False
else:
return True
except subprocess.CalledProcessError:
return False
def check_file(file):
content = ""
fi = fileinput.input(file)
for line in fi:
content += line
fi.close()
check_content(file, content)
def check_makefile(file):
content = ""
fi = fileinput.input(file)
for line in fi:
content += line
fi.close()
check_makefile_header_comment(file, content)
check_makefile_lines(file, content.splitlines(True))
def check_content(file, content):
check_eol(file, content)
check_eof(file, content)
check_header_comment(file, content)
check_function_implementations(file, content)
check_defines(file, content)
check_misplaced_multiline_spaces(file, content)
check_lines(file, content.splitlines(True))
def get_error_color(error_type):
if "color" in blacklist:
return ""
if error_type == "major":
return color.MAJOR
elif error_type == "minor":
return color.MINOR
elif error_type == "info":
return color.INFO
return color.NORMAL
def show_error(file, code, line = None):
if line is None:
line = "?"
if code in strict_error_checks and not strict:
return
codename = code;
if code.endswith("-2020"):
if year != "2020":
return
codename = code[:-5]
if code.endswith("-2021"):
if year != "2021":
return
codename = code[:-5]
if codename in blacklist:
return
(desc, type) = errors[code]
print(file + ":" + str(line) + "::" + codename + " - "
+ get_error_color(type) + desc + " (" + type + ")"
+ (color.NORMAL if "color" not in blacklist else ""))
def check_misplaced_multiline_spaces(file, content):
for match in re.finditer(misplaced_multiline_before_else_regex, content):
show_error(file, "L3", get_line_pos(content, match.start()))
for match in re.finditer(misplaced_multiline_after_else_regex, content):
show_error(file, "L3", get_line_pos(content, match.start()))
def check_defines(file, content):
matches = re.finditer(macro_statement_regex, content, re.MULTILINE)
for match in matches:
line_nb = get_line_pos(content, match.start() + 1)
show_error(file, "H3-2020", line_nb)
def check_function_implementations(file, content):
matches = re.finditer(function_impl_regex, content, re.MULTILINE)
func_count = 0
previous_function_end = -1
for match in matches:
whole_match = match.group()
line_nb = get_line_pos(content, match.start(3))
line_nb_start = get_line_pos(content, match.end(4))
line_nb_end = get_line_pos(content, match.end())
# too long function (CS2020)
if line_nb_end - line_nb_start - 1 > 20:
show_error(file, "F4-2020", line_nb)
# too long function (CS2021)
if line_nb_end - line_nb_start - 1 > 20:
show_error(file, "F4-2021", line_nb)
# misplaced pointer star in function declaration signature
if match.group(1) is not None and match.group(1).startswith("*") and match.group(1).endswith(" "):
show_error(file, "V3", line_nb)
# function name not in snake_case
if not re.search("^[a-z][a-z_0-9]*$", match.group(2)):
show_error(file, "F2", line_nb)
# too many function arguments or missing "void"
args_str = match.group(3)
if args_str.count(",") > 3 or args_str.replace(" ", "") == "":
show_error(file, "F5", line_nb)
# if no newline present between function ")" and "{"
if not "\n" in match.group(4):
show_error(file, "L3", line_nb)
# too many functions in one file
func_count += 1
if func_count > 5:
show_error(file, "O3", line_nb)
# comment in function implementation
function_content = match.group(5)
if "//" in function_content or re.search("/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", function_content):
show_error(file, "F6", line_nb)
# too many blank lines in function
if len(re.findall("\n\n", function_content.strip("\r\n"))) > 1:
show_error(file, "L6", line_nb)
# too many blank lines between function implementations
if previous_function_end != -1:
content_btw_functions = content[previous_function_end:match.start()];
content_wo_spaces = content_btw_functions.replace(" ", "").replace("\t", "")
content_wo_spaces_nl = content_wo_spaces.replace("\n", "").replace("\r", "")
if len(content_wo_spaces_nl) == 0 and content_wo_spaces.count("\n") != 1:
show_error(file, "G2", line_nb)
previous_function_end = match.end()
def check_eol(file, content):
if "\r" in content:
show_error(file, "G7-2021")
show_error(file, "implicit_LF-2020")
def check_eof(file, content):
if not content.rstrip(' \t').endswith("\n"):
show_error(file, "A3-2021", get_line_pos(content, len(content)))
if content.endswith("\n\n"):
show_error(file, "G9-2021", get_line_pos(content, len(content)))
def check_header_comment(file, content):
matches = re.search(header_regex, content)
if not matches:
show_error(file, "G1-2020")
show_error(file, "G1-2021")
def check_makefile_header_comment(file, content):
matches = re.search(makefile_header_regex, content)
if not matches:
show_error(file, "G1-2021")
def check_makefile_lines(file, lines):
line_nb = 0
for line in lines:
line_nb += 1
# columns length
if len(line.replace("\t", " ")) > 81: # 80 characters + \n
show_error(file, "F3-2021", line_nb)
def check_lines(file, lines):
line_nb = 0
has_include_guard = False
was_statement = False
for line in lines:
line_nb += 1
# don't match headers
if line.startswith("/*") or line.startswith("**") \
or line.startswith("*/"):
continue
if re.search('^\s*\#define [A-Za-z0-9]+ ', line):
show_error(file, "H3-2020", line_nb)
if re.search('^\s*\#define', line) and not is_header_file(file):
show_error(file, "C2", line_nb)
if (re.search('^static inline', line) or re.search('^inline static', line)) \
and not is_header_file(file):
show_error(file, "C2", line_nb)
# match ifndef or other if
if re.search('^\s*\#if', line):
has_include_guard = True
if re.search('^\s*\#pragma once$', line):
has_include_guard = True
# check for forbidden or allowed system_call
syscalls = re.finditer(forbidden_syscall_regex, line)
for matchNum, match in enumerate(syscalls, start=1):
if len(allowed_syscalls) > 0:
if not match.group(1) in allowed_syscalls:
show_error(file, "syscall", line_nb)
if len(disallowed_syscalls) > 0:
if match.group(1) in disallowed_syscalls:
show_error(file, "syscall", line_nb)
# columns length
if len(line.replace("\t", " ")) > 81: # 80 characters + \n
show_error(file, "F3-2020", line_nb)
show_error(file, "F3-2021", line_nb)
# tabs
if "\t" in line or re.search('\t', line):
show_error(file, "L2", line_nb)
# multiple of 4 of indentation
elif re.search('^( )+', line) and not re.search('^( )+[^ ]', line):
show_error(file, "L2", line_nb)
if re.search('(\t| ){3,}(while|for|if)', line):
show_error(file, "C1", line_nb)
if re.search('(\t| ){2,}\}?\s*(else if)', line):
show_error(file, "C1", line_nb)
# goto
if "goto " in line:
show_error(file, "C3", line_nb)
# misplaced spaces
if re.search('(while|for|if|return)\(', line):
show_error(file, "L3", line_nb)
# in while/for/if/...
if was_statement and re.search('^\s+\{', line):
show_error(file, "L3", line_nb)
was_statement = False
if re.search('^(\t| )(while|for|if)', line):
was_statement = True
# trailing spaces
if re.search('\s+\n$', line):
show_error(file, "G8-2021", line_nb)
show_error(file, "implicit_L001-2020", line_nb)
if is_header_file(file) and not has_include_guard:
show_error(file, "H2")
def is_elf(file):
file = open(file, "rb")
magic = file.read(4)
file.close()
return magic == b"\x7fELF"
def read_dir(dir):
try:
for filename in os.listdir(dir):
filepath = dir + "/" + filename
if is_file_ignored(filepath):
continue
if os.path.isfile(filepath):
if filename.lower() == "makefile":
check_makefile(filepath)
elif re.search('\.(c|h)$', filename):
if not re.search('^[a-z][a-z_0-9]*\.(c|h)$', filename):
show_error(filepath, "O4")
check_file(filepath)
else:
if re.search(unnecessary_files_regex, filename) or is_elf(filepath):
show_error(filepath, "O1")
elif not filename.startswith(".") and not (filename == "tests" \
and os.path.exists(dir + "/.git")):
if not filename.startswith(".") \
and not re.search('^[a-z][a-z_0-9]*', filename):
show_error(filepath, "O4")
read_dir(filepath)
except FileNotFoundError as error:
print("cnormitek: " + str(error))
sys.exit(84)
def read_args():
global year
global strict
global blacklist
global allowed_syscalls
global disallowed_syscalls
global stdin_is_header
args = sys.argv
path = None
for i in range(1, len(args)):
if args[i] == "--help" or args[i] == "-h":
usage()
if args[i] == "--strict" or args[i] == "-s":
strict = True
continue
if args[i] == "--stdin-h":
stdin_is_header = True
continue
if args[i].startswith("--cs-") and args[i][5:] in SUPPORTED_CODING_STYLES:
year = args[i][5:]
continue
if args[i].startswith('--no-'):
blacklist.append(args[i][5:])
continue
if args[i].startswith("--allowed="):
allowed_syscalls = args[i][10:].split(",")
if args[i].startswith("--disallowed="):
disallowed_syscalls = args[i][10:].split(",")
continue
if path is not None:
usage()
path = args[i]
if path is None:
path = os.getcwd()
return path
path = read_args()
if path == "-":
check_content("stdin", ''.join(sys.stdin.readlines()))
elif os.path.isfile(path):
check_file(path)
else:
read_dir(path)