Static analysis: Add module for deletion from dynamic array

pull/7/head
Paulius 7 years ago committed by yann300
parent 20eae79a08
commit 48352e01a0
  1. 3
      remix-analyzer/src/solidity-analyzer/modules/list.js
  2. 20
      remix-analyzer/src/solidity-analyzer/modules/staticAnalysisCommon.js
  3. 29
      remix-solidity/src/analysis/modules/deleteFromDynamicArray.js

@ -15,5 +15,6 @@ module.exports = [
require('./deleteDynamicArrays'), require('./deleteDynamicArrays'),
require('./assignAndCompare'), require('./assignAndCompare'),
require('./erc20Decimals'), require('./erc20Decimals'),
require('./stringBytesLength') require('./stringBytesLength'),
require('./deleteFromDynamicArray')
] ]

@ -501,6 +501,24 @@ function isDynamicArrayAccess (node) {
return node && nodeType(node, exactMatch(nodeTypes.IDENTIFIER)) && (node.attributes.type.endsWith('[] storage ref') || node.attributes.type === 'bytes storage ref' || node.attributes.type === 'string storage ref') return node && nodeType(node, exactMatch(nodeTypes.IDENTIFIER)) && (node.attributes.type.endsWith('[] storage ref') || node.attributes.type === 'bytes storage ref' || node.attributes.type === 'string storage ref')
} }
/**
* True if node is a delete instruction for an element from a dynamic array
* @node {ASTNode} node to check for
* @return {bool}
*/
function isDeleteFromDynamicArray (node) {
return isDeleteUnaryOperation(node) && isIndexAccess(node.children[0])
}
/**
* True if node is the access of an index
* @node {ASTNode} node to check for
* @return {bool}
*/
function isIndexAccess (node) {
return node && node.name === 'IndexAccess'
}
/** /**
* True if call to code within the current contracts context including (delegate) library call * True if call to code within the current contracts context including (delegate) library call
* @node {ASTNode} some AstNode * @node {ASTNode} some AstNode
@ -999,9 +1017,11 @@ module.exports = {
// #################### Complex Node Identification // #################### Complex Node Identification
isDeleteOfDynamicArray: isDeleteOfDynamicArray, isDeleteOfDynamicArray: isDeleteOfDynamicArray,
isDeleteFromDynamicArray: isDeleteFromDynamicArray,
isAbiNamespaceCall: isAbiNamespaceCall, isAbiNamespaceCall: isAbiNamespaceCall,
isSpecialVariableAccess: isSpecialVariableAccess, isSpecialVariableAccess: isSpecialVariableAccess,
isDynamicArrayAccess: isDynamicArrayAccess, isDynamicArrayAccess: isDynamicArrayAccess,
isIndexAccess: isIndexAccess,
isSubScopeWithTopLevelUnAssignedBinOp: isSubScopeWithTopLevelUnAssignedBinOp, isSubScopeWithTopLevelUnAssignedBinOp: isSubScopeWithTopLevelUnAssignedBinOp,
hasFunctionBody: hasFunctionBody, hasFunctionBody: hasFunctionBody,
isInteraction: isInteraction, isInteraction: isInteraction,

@ -0,0 +1,29 @@
var name = 'Delete from dynamic Array: '
var desc = 'Using delete on an array leaves a gap'
var categories = require('./categories')
var common = require('./staticAnalysisCommon')
function deleteFromDynamicArray () {
this.relevantNodes = []
}
deleteFromDynamicArray.prototype.visit = function (node) {
if (common.isDeleteFromDynamicArray(node)) this.relevantNodes.push(node)
}
deleteFromDynamicArray.prototype.report = function (compilationResults) {
return this.relevantNodes.map((node) => {
return {
warning: 'Using delete on an array leaves a gap. The length of the array remains the same. If you want to remove the empty position you need to shift items manually and update the length property.\n',
location: node.src,
more: 'https://github.com/miguelmota/solidity-idiosyncrasies'
}
})
}
module.exports = {
name: name,
description: desc,
category: categories.MISC,
Module: deleteFromDynamicArray
}
Loading…
Cancel
Save