Unnecessary Storage Pointer Declaration batchMintWithFee
The batchMintWithFee function declares a storage pointer to the contract's storage struct before its loop but never uses it, since the internal mint function fetches storage itself. This wastes gas on an unnecessary storage read and clutters the code, though no security impact is claimed.
- Disclosed after under an hour
#38148 [SC-Insight] Unnecessary Storage Pointer Declaration batchMintWithFee
Submitted on Dec 26th 2024 at 09:15:34 UTC by @c4a4dda89 for Audit Comp | Lombard
- Report ID: #38148
- Report Type: Smart Contract
- Report severity: Insight
- Target: https://github.com/lombard-finance/evm-smart-contracts/blob/main/contracts/LBTC/LBTC.sol
- Impacts:
- Theft of gas
Description
The function declares a storage pointer LBTCStorage storage $ = _getLBTCStorage(); before the loop, but this pointer is never used within the loop or elsewhere in the function. The _mintWithFee function internally calls _getLBTCStorage() to access storage when needed.
// Current implementation
LBTCStorage storage $ = _getLBTCStorage(); // Unnecessary SLOAD operation
for (uint256 i; i < mintPayload.length; ++i) {
_mintWithFee(
mintPayload[i],
proof[i],
feePayload[i],
userSignature[i]
);
}
This creates an unused storage pointer that: Consumes unnecessary gas (approximately 2100 gas for SLOAD operation) Makes the code less clean by introducing an unused variable Could potentially mislead other developers into thinking the storage pointer is being used
Proof of Concept
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {Test} from "forge-std/Test.sol";
import {LBTC} from "../contracts/LBTC/LBTC.sol";
import {Actions} from "../contracts/libs/Actions.sol";
contract LBTCTest is Test {
LBTC public lbtc;
address public owner = makeAddr("owner");
address public claimer = makeAddr("claimer");
address public user = makeAddr("user");
function setUp() public {
// Deploy and initialize LBTC contract
lbtc = new LBTC();
lbtc.initialize(
address(1), // consortium
100, // burnCommission
address(2), // treasury
owner // owner
);
// Setup claimer role
vm.prank(owner);
lbtc.addClaimer(claimer);
}
function testBatchMintWithFee() public {
// Create test data arrays
bytes[] memory mintPayloads = new bytes[](2);
bytes[] memory proofs = new bytes[](2);
bytes[] memory feePayloads = new bytes[](2);
bytes[] memory userSignatures = new bytes[](2);
// First mint data
mintPayloads[0] = _createDepositBtcAction(user, 1000);
proofs[0] = "0x"; // Simplified for demo
feePayloads[0] = _createFeeApprovalAction(100);
userSignatures[0] = _createUserSignature();
// Second mint data
mintPayloads[1] = _createDepositBtcAction(user, 2000);
proofs[1] = "0x"; // Simplified for demo
feePayloads[1] = _createFeeApprovalAction(200);
userSignatures[1] = _createUserSignature();
// Call batchMintWithFee as claimer
vm.prank(claimer);
lbtc.batchMintWithFee(
mintPayloads,
proofs,
feePayloads,
userSignatures
);
}
// Helper function to create DepositBtcAction payload
function _createDepositBtcAction(address recipient, uint256 amount) internal pure returns (bytes memory) {
return abi.encodePacked(
Actions.DEPOSIT_BTC_ACTION,
abi.encode(recipient, amount)
);
}
// Helper function to create FeeApprovalAction payload
function _createFeeApprovalAction(uint256 fee) internal view returns (bytes memory) {
return abi.encodePacked(
Actions.FEE_APPROVAL_ACTION,
abi.encode(fee, block.timestamp + 1 hours)
);
}
// Helper function to create a mock user signature
function _createUserSignature() internal pure returns (bytes memory) {
return new bytes(65); // Mock signature of correct length
}
}
This is a simple proof-of-concept (PoC) code, but I believe that this issue doesn’t require a PoC to be taken seriously; it can be identified through a visual inspection alone.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely a leftover or copy-paste declaration: the storage pointer was fetched before the loop even though the called function retrieves storage itself.
Impact
Each call wastes roughly 2100 gas on an unused storage read and may mislead developers, but no security impact is claimed.
What the attacker needed
None
The fix
The unused storage pointer should be removed from the function.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
Smart contracts on Ethereum keep data in 'storage', which is expensive to read. The code here reads that storage into a variable at the start of a batch function but never actually uses the variable, so it pays a small gas cost for nothing. Gas is the fee paid to run code on the network.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.