Function Modifier Explained in Detail
A function modifier wraps function execution. It commonly checks access, pause state, input validity, or reentrancy before the function body runs.
The _ symbol marks where the function body is inserted. Code before _ runs before the function body. Code after _ runs after it.
Smart contract example
modifier onlyOwner() {
require(msg.sender == owner, "not owner");
_;
}
This modifier blocks callers that are not the owner.
Function Modifier in Auditing
Modifiers can hide important security logic. Their order also matters. A function with the right modifiers in the wrong order can still be unsafe.
Auditors expand modifiers mentally and review the full execution path, not just the function body.
Red flags in code
-
Modifiers contain external calls.
-
State changes happen before
_and affect later checks. -
Modifier order changes authorization or reentrancy behavior.
-
Internal helper functions bypass modifier-protected external functions.
-
A modifier name suggests safety but does not enforce it.
How to test or review it
-
Read modifiers as if their code were pasted into the function.
-
Test protected functions with authorized and unauthorized callers.
-
Check modifier order on every sensitive function.
-
Look for external calls, state writes, and reverts inside modifiers.
-
Confirm internal paths cannot bypass required checks.
Keep learning this topic
Access Control Vulnerability
An access control vulnerability lets an unauthorized caller perform privileged actions such as moving funds, changing roles, upgrading contracts, or changing protocol settings.
Reentrancy Guard
A reentrancy guard is a lock that prevents a protected function from being entered again while it is already executing.
Pausable
Pausable is an emergency-control pattern that lets authorized accounts temporarily disable selected contract functions.
Practice this in real audit scenarios
Definitions help, but auditors need reps. SCH turns concepts like Function Modifier into exploit labs, code review habits, and report-writing practice.
Start the free trial or see the full smart contract auditing course.