Skip to content

Commit

Permalink
Merge pull request #333 from EYBlockchain/lydia/ownershipError
Browse files Browse the repository at this point in the history
fix: ensure whenever we have mappings of addresses, the address is al…
  • Loading branch information
SwatiEY committed Sep 17, 2024
2 parents 5a5e9df + 6f51773 commit 7e41ad7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/traverse/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export class VariableBinding extends Binding {
}

updateOwnership(ownerNode: any, msgIsMappingKeyorMappingValue?: string | null) {
if (this.isOwned && this.owner.mappingOwnershipType === 'key') return;
if (
ownerNode.expression?.name === 'msg' &&
msgIsMappingKeyorMappingValue === 'value'
Expand Down Expand Up @@ -521,19 +522,9 @@ export class VariableBinding extends Binding {
*/
inferOwnership() {
if (this.kind !== 'VariableDeclaration') return;
let msgSenderEverywhereMappingKey: boolean = false;
let msgSenderEverywhereMappingValue: boolean = false;
let msgSenderEverywhereMappingKey: boolean;
let msgSenderEverywhereMappingValue: boolean;
this.nullifyingPaths.forEach(path => {
const functionDefScope = path.scope.getAncestorOfScopeType(
'FunctionDefinition',
);
if (functionDefScope.callerRestriction === 'match') {
this.updateOwnership(functionDefScope.callerRestrictionNode);
return;
}
if (functionDefScope.callerRestriction === 'exclude') {
this.updateBlacklist(functionDefScope.callerRestrictionNode);
}
if (this.isMapping && path.getAncestorOfType('IndexAccess') && this.addMappingKey(path).isMsgSender ) {
// if its unassigned, we assign true
// if its true, it remains true
Expand All @@ -542,6 +533,7 @@ export class VariableBinding extends Binding {
this.isMapping && path.getAncestorOfType('IndexAccess') &&
(path.isMsgSender(path.getCorrespondingRhsNode()) || path.isMsgValue(path.getCorrespondingRhsNode()))
) {
msgSenderEverywhereMappingKey = false;
msgSenderEverywhereMappingValue ??= true;
} else {
// if we find a single non-msg sender mapping key, then msg sender can't be the owner
Expand All @@ -562,6 +554,19 @@ export class VariableBinding extends Binding {
this.nullifyingPaths[0].parentPath.parent.rightHandSide;
this.updateOwnership(owner, 'value');
}
this.nullifyingPaths.forEach(path => {
const functionDefScope = path.scope.getAncestorOfScopeType(
'FunctionDefinition',
);
// we update the ownership of the variable if the function has a restriction on the caller, e.g. require(msg.sender == admin)
if (functionDefScope.callerRestriction === 'match') {
this.updateOwnership(functionDefScope.callerRestrictionNode);
return;
}
if (functionDefScope.callerRestriction === 'exclude') {
this.updateBlacklist(functionDefScope.callerRestrictionNode);
}
});
}

ownerSetToZeroCheck() {
Expand Down
8 changes: 7 additions & 1 deletion test/contracts/mapping-3.zol
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ contract MyContract {
secret uint256 private b;
address public admin;

constructor() {
admin = msg.sender;
}

function assign(secret address param1, secret uint256 param2) public {
unknown a[param1] += param2;
}

function assign2(secret uint256 param3) public {
require(msg.sender == admin);
a[msg.sender] = a[msg.sender] - param3;
known b += param3;
}

function assign3(secret uint256 param5) public {
function assign3(secret uint256 param5, secret address recipient) public {
require(msg.sender == admin);
known b += param5;
a[recipient] += param5;
}
}

0 comments on commit 7e41ad7

Please sign in to comment.