diff --git a/remix-analyzer/src/solidity-analyzer/modules/stringBytesLength.js b/remix-analyzer/src/solidity-analyzer/modules/stringBytesLength.js new file mode 100644 index 0000000000..8143d962d7 --- /dev/null +++ b/remix-analyzer/src/solidity-analyzer/modules/stringBytesLength.js @@ -0,0 +1,34 @@ +var name = 'String Length: ' +var desc = 'Bytes length != String length' +var categories = require('./categories') +var common = require('./staticAnalysisCommon') + +function stringBytesLength () { + this.stringToBytesConversions = [] + this.bytesLengthChecks = [] +} + +stringBytesLength.prototype.visit = function (node) { + if (common.isStringToBytesConversion(node)) this.stringToBytesConversions.push(node) + else if (common.isBytesLengthCheck(node)) this.bytesLengthChecks.push(node) +} + +stringBytesLength.prototype.report = function (compilationResults) { + if (this.stringToBytesConversions.length > 0 && this.bytesLengthChecks.length > 0) { + return [{ + warning: 'Bytes and string length are not the same since strings are assumed to be UTF-8 encoded (according to the ABI defintion) therefore one character is not nessesarily encoded in one byte of data.', + location: this.bytesLengthChecks[0], + more: 'https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#argument-encoding' + }] + } else { + return [] + } +} + +module.exports = { + name: name, + description: desc, + category: categories.MISC, + Module: stringBytesLength +} + diff --git a/remix-analyzer/test/analysis/staticAnalysisIntegration-test.js b/remix-analyzer/test/analysis/staticAnalysisIntegration-test.js index 9bfa5a96eb..4ba5fc4af7 100644 --- a/remix-analyzer/test/analysis/staticAnalysisIntegration-test.js +++ b/remix-analyzer/test/analysis/staticAnalysisIntegration-test.js @@ -487,7 +487,7 @@ test('Integration test selfdestruct.js', function (t) { 'selfdestruct.sol': 3, 'deleteDynamicArray.sol': 0, 'blockLevelCompare.sol': 0, - 'intDivisionTruncate.sol': 1, // 5 + 'intDivisionTruncate.sol': 5, 'stringBytesLength.sol': 0 } @@ -639,7 +639,7 @@ test('Integration test intDivisionTruncate.js', function (t) { test('Integration test stringBytesLength.js', function (t) { t.plan(testFiles.length) - var module = require('../../src/analysis/modules/stringBytesLength') + var module = require('../../src/solidity-analyzer/modules/stringBytesLength') var lengthCheck = { 'KingOfTheEtherThrone.sol': 0,