DeFiFoundry
60,000 USDC
View results
Submission Details
Severity: medium
Invalid

Missing Existence Check in cancelMarketOrder Function

Summary

The cancelMarketOrder function in the OrderBranch contract does not verify the existence of a market order before attempting to clear it. If a market order does not exist for the given tradingAccountId, the function's behavior is undefined and could lead to errors or unexpected outcomes.

Vulnerability Details

The cancelMarketOrder function is intended to cancel an active market order associated with a specific tradingAccountId. However, it directly calls the MarketOrder.loadExisting(tradingAccountId) function, which will revert if no active market order exists for the given account. This means that the function will fail if there is no order to cancel, potentially disrupting the user experience and causing confusion.

https://github.com/Cyfrin/2024-07-zaros/blob/main/src/perpetuals/branches/OrderBranch.sol#L391

MarketOrder.Data storage marketOrder = MarketOrder.loadExisting(tradingAccountId);

Impact

If a user attempts to cancel a non-existent market order, the transaction will revert, wasting gas fees and potentially causing frustration.

Tools Used

Manual review

Recommendations

Before calling MarketOrder.loadExisting, add a check to verify if an active market order exists for the given tradingAccountId. This can be done by directly checking the storage slot associated with the market order or by adding a helper function to the MarketOrder library to check for existence.

MarketOrder.Data storage marketOrder = MarketOrder.load(tradingAccountId); // Load the market order without reverting
if (marketOrder.marketId != 0) { // Check if the marketId is not 0 (indicating an active order)
// ... proceed with clearing the order ...
} else {
revert Errors.NoActiveMarketOrder(tradingAccountId); // Or handle the absence of an order gracefully
}
Updates

Lead Judging Commences

inallhonesty Lead Judge
11 months ago
inallhonesty Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.