Skip to content

Commit 5953e21

Browse files
committed
[FEATURE] Added forbidden function check for the libft. Fixes #20
1 parent 3440bc7 commit 5953e21

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

42PyChecker.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
import glob
44
import subprocess
5-
5+
import re
66

77
def check_author_file(project_path: str):
88
"""
@@ -203,6 +203,24 @@ def check_42_commandements(project_path:str):
203203
return 2
204204
return 0
205205

206+
207+
def check_forbidden_functions(project_path: str, binary: str):
208+
# @todo: Refactor check_forbidden_functions to have a modular authorized_functions (to be used on multiple projects)
209+
authorized_functions = ['free', 'malloc', 'write', 'main']
210+
functions_called = []
211+
result = subprocess.run(['nm', project_path + '/' + binary], stdout=subprocess.PIPE).stdout.decode('utf-8')
212+
for line in result.splitlines():
213+
if "U _" in line:
214+
functions_called.append(re.sub(' +', ' ', line))
215+
sys_calls = {function.replace(' U _', '') for function in functions_called}
216+
sys_calls = [item for item in sys_calls if not item.startswith("ft_")]
217+
extra_function_call = [item for item in sys_calls if item not in authorized_functions]
218+
with open(os.path.dirname(os.path.realpath(__file__)) + "/.myforbiddenfunctions", 'w+') as file:
219+
for item in extra_function_call:
220+
file.write("You should justify the use of this function: `{}'\n".format(item))
221+
return 0
222+
223+
206224
def check_libft(project_path: str):
207225
required_functions = ['libft.h', 'ft_strcat.c', 'ft_strncat.c',
208226
'ft_strlcat.c', 'ft_strchr.c', 'ft_strnstr.c',
@@ -246,11 +264,11 @@ def check_libft(project_path: str):
246264
result = subprocess.run(['sh', 'check_static.sh', project_path], stdout=subprocess.PIPE).stdout.decode('utf-8')
247265
file.write(result)
248266
check_makefile(project_path, "libft.a") # @todo: Have number of errors printed for makefile check
249-
#forbidden @todo: Add forbidden function check utility method
267+
check_forbidden_functions(project_path, "libft.a")
250268
#moulitest
251269
#libft-unit-test
252270
#maintest
253271
return 0
254272

255273

256-
sys.exit(check_libft("/tmp/libft"))
274+
sys.exit(check_forbidden_functions("/tmp/libft", "libft.a"))

0 commit comments

Comments
 (0)