|
| 1 | +""" |
| 2 | +Module detecting unused custom errors |
| 3 | +""" |
| 4 | + |
| 5 | +from slither.core.declarations import Function, Contract |
| 6 | +from slither.core.declarations.custom_error import CustomError |
| 7 | +from slither.core.declarations.custom_error_contract import CustomErrorContract |
| 8 | +from slither.core.declarations.custom_error_top_level import CustomErrorTopLevel |
| 9 | +from slither.core.declarations.solidity_variables import SolidityCustomRevert |
| 10 | +from slither.detectors.abstract_detector import ( |
| 11 | + AbstractDetector, |
| 12 | + DetectorClassification, |
| 13 | + DETECTOR_INFO, |
| 14 | +) |
| 15 | +from slither.slithir.operations import SolidityCall |
| 16 | +from slither.utils.output import Output |
| 17 | + |
| 18 | + |
| 19 | +def _detect_unused_custom_errors_in_contract( |
| 20 | + contract: Contract, |
| 21 | +) -> list[CustomErrorContract]: |
| 22 | + """ |
| 23 | + Detect unused custom errors declared in a contract. |
| 24 | +
|
| 25 | + Args: |
| 26 | + contract: The contract to analyze |
| 27 | +
|
| 28 | + Returns: |
| 29 | + List of unused custom errors declared in the contract |
| 30 | + """ |
| 31 | + # Get all custom errors declared in this contract |
| 32 | + declared_errors = set(contract.custom_errors_declared) |
| 33 | + |
| 34 | + if not declared_errors: |
| 35 | + return [] |
| 36 | + |
| 37 | + # Find all custom errors used in this contract and its derived contracts |
| 38 | + used_errors: set[CustomError] = set() |
| 39 | + |
| 40 | + # Check all functions in the contract (including inherited) |
| 41 | + all_functions = [ |
| 42 | + f |
| 43 | + for f in contract.all_functions_called + list(contract.modifiers) |
| 44 | + if isinstance(f, Function) |
| 45 | + ] |
| 46 | + |
| 47 | + for func in all_functions: |
| 48 | + for node in func.nodes: |
| 49 | + for ir in node.all_slithir_operations(): |
| 50 | + if isinstance(ir, SolidityCall) and isinstance( |
| 51 | + ir.function, SolidityCustomRevert |
| 52 | + ): |
| 53 | + used_errors.add(ir.function.custom_error) |
| 54 | + |
| 55 | + # Return unused errors |
| 56 | + return [error for error in declared_errors if error not in used_errors] |
| 57 | + |
| 58 | + |
| 59 | +def _detect_unused_custom_errors_top_level( |
| 60 | + compilation_unit, |
| 61 | +) -> list[CustomErrorTopLevel]: |
| 62 | + """ |
| 63 | + Detect unused top-level custom errors. |
| 64 | +
|
| 65 | + Args: |
| 66 | + compilation_unit: The compilation unit to analyze |
| 67 | +
|
| 68 | + Returns: |
| 69 | + List of unused top-level custom errors |
| 70 | + """ |
| 71 | + # Get all top-level custom errors |
| 72 | + top_level_errors = set(compilation_unit.custom_errors) |
| 73 | + |
| 74 | + if not top_level_errors: |
| 75 | + return [] |
| 76 | + |
| 77 | + # Find all custom errors used across all contracts |
| 78 | + used_errors: set[CustomError] = set() |
| 79 | + |
| 80 | + for contract in compilation_unit.contracts: |
| 81 | + all_functions = [ |
| 82 | + f |
| 83 | + for f in contract.all_functions_called + list(contract.modifiers) |
| 84 | + if isinstance(f, Function) |
| 85 | + ] |
| 86 | + |
| 87 | + for func in all_functions: |
| 88 | + for node in func.nodes: |
| 89 | + for ir in node.all_slithir_operations(): |
| 90 | + if isinstance(ir, SolidityCall) and isinstance( |
| 91 | + ir.function, SolidityCustomRevert |
| 92 | + ): |
| 93 | + used_errors.add(ir.function.custom_error) |
| 94 | + |
| 95 | + # Also check top-level functions |
| 96 | + for func in compilation_unit.functions_top_level: |
| 97 | + for node in func.nodes: |
| 98 | + for ir in node.all_slithir_operations(): |
| 99 | + if isinstance(ir, SolidityCall) and isinstance( |
| 100 | + ir.function, SolidityCustomRevert |
| 101 | + ): |
| 102 | + used_errors.add(ir.function.custom_error) |
| 103 | + |
| 104 | + # Return unused top-level errors |
| 105 | + return [error for error in top_level_errors if error not in used_errors] |
| 106 | + |
| 107 | + |
| 108 | +class UnusedCustomErrors(AbstractDetector): |
| 109 | + """ |
| 110 | + Detector for unused custom error definitions |
| 111 | + """ |
| 112 | + |
| 113 | + ARGUMENT = "unused-error" |
| 114 | + HELP = "Unused custom error definitions" |
| 115 | + IMPACT = DetectorClassification.INFORMATIONAL |
| 116 | + CONFIDENCE = DetectorClassification.HIGH |
| 117 | + |
| 118 | + WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#unused-custom-error" |
| 119 | + |
| 120 | + WIKI_TITLE = "Unused custom error" |
| 121 | + WIKI_DESCRIPTION = "Detects custom error definitions that are never used. Unused custom errors may indicate missing error handling logic or dead code that should be removed." |
| 122 | + |
| 123 | + WIKI_EXPLOIT_SCENARIO = """ |
| 124 | +```solidity |
| 125 | +contract VendingMachine { |
| 126 | + error Unauthorized(); // Defined but never used |
| 127 | + address payable owner = payable(msg.sender); |
| 128 | +
|
| 129 | + function withdraw() public { |
| 130 | + // Missing: if (msg.sender != owner) revert Unauthorized(); |
| 131 | + owner.transfer(address(this).balance); |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | +The `Unauthorized` error is defined but never used, suggesting the developer may have forgotten to add access control checks.""" |
| 136 | + |
| 137 | + WIKI_RECOMMENDATION = "Use the custom error in a `revert` statement, or remove the error definition if it is not needed." |
| 138 | + |
| 139 | + def _detect(self) -> list[Output]: |
| 140 | + """Detect unused custom errors""" |
| 141 | + results: list[Output] = [] |
| 142 | + |
| 143 | + # Check for unused custom errors in each contract |
| 144 | + for contract in self.compilation_unit.contracts_derived: |
| 145 | + if contract.is_signature_only(): |
| 146 | + continue |
| 147 | + |
| 148 | + unused_errors = _detect_unused_custom_errors_in_contract(contract) |
| 149 | + for error in unused_errors: |
| 150 | + info: DETECTOR_INFO = [ |
| 151 | + error, |
| 152 | + " is declared but never used in ", |
| 153 | + contract, |
| 154 | + "\n", |
| 155 | + ] |
| 156 | + results.append(self.generate_result(info)) |
| 157 | + |
| 158 | + # Check for unused top-level custom errors |
| 159 | + unused_top_level = _detect_unused_custom_errors_top_level(self.compilation_unit) |
| 160 | + for error in unused_top_level: |
| 161 | + info = [error, " is declared but never used\n"] |
| 162 | + results.append(self.generate_result(info)) |
| 163 | + |
| 164 | + return results |
0 commit comments