Skip to content

Commit

Permalink
general fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
faculerena committed Jun 11, 2024
1 parent 2382b6b commit 6ba5431
Show file tree
Hide file tree
Showing 18 changed files with 57 additions and 30 deletions.
6 changes: 4 additions & 2 deletions stacks_analyzer/detectors/AssertBlockHeight.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


class AssertBlockHeight(Visitor):
MSG = "Use of block-height inside a assert"
MSG = "Use of block-height inside a assert."
HELP = "Consider using burn-block-height."

def __init__(self):
super().__init__()
Expand All @@ -25,6 +26,7 @@ def visit_node(self, node: Node, i):
node.parent,
node,
self.MSG,
None
None,
self.HELP
)
break
13 changes: 8 additions & 5 deletions stacks_analyzer/detectors/CallInsideAsContract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class CallInsideAsContract(Visitor):
MSG = "Use of call-contract? inside an as-contract context."
checked: []

def __init__(self):
super().__init__()
Expand All @@ -16,7 +15,7 @@ def __init__(self):

def visit_node(self, node: Node, i):
if i > 1:
return
pass
if str(node.text, "utf8") == "as-contract":
descendants = NodeIterator(node.parent)
while True:
Expand All @@ -27,13 +26,17 @@ def visit_node(self, node: Node, i):
self.call = True
if n.grammar_name == "contract_principal_lit":
self.lit = True
if self.call and not self.lit:

if (self.call and not self.lit) and node not in self.checked:
pretty_print_warn(
self,
node.parent,
node,
self.MSG,
None,
None
)
self.call = False
self.lit = False
self.checked.append(node)

self.call = False
self.lit = False
6 changes: 4 additions & 2 deletions stacks_analyzer/detectors/DivideBeforeMultiply.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@


class DivideBeforeMultiply(Visitor):
MSG = "Use of divide inside a multiplication. This could result in a precision loss"
MSG = "Use of divide inside a multiplication. This could result in a precision loss."
NOTE = "Try multiplication before division."

def __init__(self):
super().__init__()
Expand All @@ -25,5 +26,6 @@ def visit_node(self, node: Node, i):
node.parent,
node,
self.MSG,
None
None,
self.NOTE,
)
3 changes: 2 additions & 1 deletion stacks_analyzer/detectors/PrivateFunctionNotUsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def visit_node(self, node: Node, run_number: int):
n,
n,
self.MSG,
None
None,
"Consider removing it."
)
self.read_only_names = []
1 change: 1 addition & 0 deletions stacks_analyzer/detectors/ToDoComment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def visit_node(self, node: Node, run_number: int):
node,
node,
self.MSG,
None,
None
)

3 changes: 2 additions & 1 deletion stacks_analyzer/detectors/TxSenderInAssert.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def visit_node(self, node: Node, i):
node.parent,
node,
self.MSG,
None
None,
"Consider using contract-caller"
)
break

5 changes: 3 additions & 2 deletions stacks_analyzer/detectors/UnwrapPanicUsage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class UnwrapPanicUsage(Visitor):
MSG = "Use of unwrap-panic. Use unwrap! and handle the error."
MSG = "Use of unwrap-panic."

def __init__(self):
super().__init__()
Expand All @@ -19,6 +19,7 @@ def visit_node(self, node: Node, i):
node.parent,
node,
self.MSG,
None
None,
"Use unwrap! and handle the error."
)

5 changes: 3 additions & 2 deletions stacks_analyzer/detectors/UpdatedFunctionsDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ def visit_node(self, node: Node, i):
fun = str(node.text, "utf-8")

if fun in self.functions_updated:
MSG = f"Behavior of '{fun}' changed from Clarity1 to Clarity2, now outputs optional value.\nSuggestion: use '{fun}?' to make this behavior explicit."
MSG = f"Behavior of '{fun}' changed from Clarity1 to Clarity2, now outputs optional value."
pretty_print_warn(
self,
node.parent,
node,
MSG,
None
None,
f"Suggestion: use '{fun}?' to make this behavior explicit."
)

1 change: 1 addition & 0 deletions stacks_analyzer/detectors/VarCouldBeConstant.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def visit_node(self, node: Node, i):
n,
n,
self.MSG,
None,
None
)
self.data_vars = []
10 changes: 9 additions & 1 deletion stacks_analyzer/print_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TerminalColors:
UNDERLINE = '\033[4m'


def pretty_print_warn(visitor: Visitor, parent: Node, specific_node: Node, msg: str, help_msg: str | None):
def pretty_print_warn(visitor: Visitor, parent: Node, specific_node: Node, msg: str, help_msg: str | None, footnote: str | None):
line_number = parent.start_point.row + 1
num_size_spaces = " " * (int(math.log10(line_number)) + 2)
contract_code = visitor.source.split('\n')[line_number - 1]
Expand All @@ -43,5 +43,13 @@ def pretty_print_warn(visitor: Visitor, parent: Node, specific_node: Node, msg:

if help_msg is not None:
print(f" {num_size_spaces}|{spaces}{help_msg}")

if footnote is not None:
if tty:
print(f" {num_size_spaces}{TerminalColors.OKCYAN}Note:{TerminalColors.ENDC}{footnote}")
else:
print(f" {num_size_spaces}Note: {footnote}")
print()



16 changes: 7 additions & 9 deletions stacks_analyzer/stacks_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"UnwrapPanicUsage": UnwrapPanicUsage(),
"UpdatedFunctionsDetector": UpdatedFunctionsDetector(),
"VarCouldBeConstant": VarCouldBeConstant(),

}


Expand All @@ -40,14 +39,13 @@ def main():
lint_parser.add_argument("--exclude", nargs="+", type=str, help="Comma-separated list of detector names to exclude")
list_detectors = subparsers.add_parser("detectors", help="List detectors")

args = arg_parser.parse_args()

filters = args.filter or list(DETECTOR_MAP.keys())
excludes = args.exclude or []
detectors = get_detectors(filters, excludes)

if args.command == "lint":
path = args.path
user_args = arg_parser.parse_args()
if user_args.command == "lint":
filters = user_args.filter or list(DETECTOR_MAP.keys())
excludes = user_args.exclude or []
detectors = get_detectors(filters, excludes)
path = user_args.path
if path.endswith(".clar"):
lint_file(path, detectors)
else:
Expand All @@ -56,7 +54,7 @@ def main():
if file.endswith(".clar"):
lint_file(os.path.join(root, file), detectors)

if args.command == "detectors":
if user_args.command == "detectors":
detectors = list(DETECTOR_MAP.keys())

max_length = max(len(st) for st in detectors)
Expand Down
1 change: 1 addition & 0 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ base_dir="tests"

for test_case in "$base_dir"/*; do
for example in "$test_case"/*; do
echo "Testing detectors in" "$example"
process_example "$example"
done
done
Expand Down
3 changes: 2 additions & 1 deletion tests/assert_block_height/vulnerable-example/stdout
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
====== Linting tests/assert_block_height/vulnerable-example/assert_block_height.clar... ======
Warning: Use of block-height inside a assert
Warning: Use of block-height inside a assert.
|
6 | (asserts! (> (get expiry nft-asset) block-height) err-expiry-in-past)
| ^^^^^^^^
Note: Consider using burn-block-height.

3 changes: 2 additions & 1 deletion tests/divide_before_multiply/vulnerable-example/stdout
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
====== Linting tests/divide_before_multiply/vulnerable-example/divide_before_multiply.clar... ======
Warning: Use of divide inside a multiplication. This could result in a precision loss
Warning: Use of divide inside a multiplication. This could result in a precision loss.
|
4 | (* (/ prize participants) bonus)
| ^
Note: Try multiplication before division.

3 changes: 3 additions & 0 deletions tests/private_function_not_used/vulnerable-example/stdout
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ Warning: This private function is not used.
|
9 | (define-private (get-last-token-id)
| ^
Note: Consider removing it.

Warning: This private function is not used.
|
13 | (define-private (get-token-uri (token-id uint))
| ^
Note: Consider removing it.

Warning: This private function is not used.
|
17 | (define-private (get-owner (token-id uint))
| ^
Note: Consider removing it.

1 change: 1 addition & 0 deletions tests/tx_sender_in_assert/vulnerable-example/stdout
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ Warning: Use of tx-sender inside an assert
|
5 | (asserts! (is-eq tx-sender contract-owner) err-owner-only)
| ^^^^^^^^
Note: Consider using contract-caller

3 changes: 2 additions & 1 deletion tests/unwrap_panic_usage/vulnerable-example/stdout
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
====== Linting tests/unwrap_panic_usage/vulnerable-example/unwrap_panic.clar... ======
Warning: Use of unwrap-panic. Use unwrap! and handle the error.
Warning: Use of unwrap-panic.
|
4 | (listing (unwrap-panic (map-get? listings listing-id) err-unknown-listing))
| ^^^^^^^^^^^^
Note: Use unwrap! and handle the error.

4 changes: 2 additions & 2 deletions tests/updated_functions/vulnerable-example/stdout
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
====== Linting tests/updated_functions/vulnerable-example/updated_functions.clar... ======
Warning: Behavior of 'element-at' changed from Clarity1 to Clarity2, now outputs optional value.
Suggestion: use 'element-at?' to make this behavior explicit.
|
4 | (element-at sequence index)
| ^^^^^^^^^^
Note: Suggestion: use 'element-at?' to make this behavior explicit.

Warning: Behavior of 'index-of' changed from Clarity1 to Clarity2, now outputs optional value.
Suggestion: use 'index-of?' to make this behavior explicit.
|
8 | (index-of places elem)
| ^^^^^^^^
Note: Suggestion: use 'index-of?' to make this behavior explicit.

0 comments on commit 6ba5431

Please sign in to comment.