-
Notifications
You must be signed in to change notification settings - Fork 15
/
unsafe-math-toml.py
executable file
·57 lines (47 loc) · 1.91 KB
/
unsafe-math-toml.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
#!/usr/bin/env python3
import sys
import os
sys.path.insert(0, os.getcwd())
from utils.core import *
import re
import glob
import toml
PROJ_PATH = os.environ["NEAR_SRC_DIR"]
if len(sys.argv) == 2:
if sys.argv[1] == "-h" or sys.argv[1] == "--help":
print("Usage: detectors/unsafe-math-toml.py [path to project]")
sys.exit()
else:
PROJ_PATH = sys.argv[1]
elif len(sys.argv) > 2:
print("Usage: detectors/unsafe-math-toml.py [path to project]")
sys.exit()
TMP_PATH = os.environ["TMP_DIR"]
os.makedirs(TMP_PATH, exist_ok=True)
def checkToml(path) -> bool:
checked = False
with open(path, "r") as cargo_file:
parsed_cargo = toml.loads(cargo_file.read())
profile = parsed_cargo.get("profile")
if profile != None:
if profile.get("release"): # defaults to False
if profile.get("release").get("overflow-checks") == None or profile.get("release").get("overflow-checks") == False:
print("[!] Lack of overflow-checks in release profile at " + path + ".")
log_file.write(path + "\n")
checked = True
if profile.get("dev"): # defaults to True
if profile.get("dev").get("overflow-checks") != None and profile.get("dev").get("overflow-checks") == False:
print("[!] Lack of overflow-checks in dev profile at " + path + ".")
log_file.write(path + "\n")
checked = True
return checked
def findCargoToml(path) -> list:
toml_list = glob.glob(path + "/**/Cargo.toml", recursive=True)
path = os.path.abspath(path + "/..")
while os.path.exists(path + "/Cargo.toml"):
toml_list.append(path + "/Cargo.toml")
path = os.path.abspath(path + "/..")
return toml_list
with open(TMP_PATH + "/.unsafe-math-toml.tmp", "w") as log_file:
for path in findCargoToml(PROJ_PATH):
checkToml(path)