From 25eb6c4e6910dfd0400c15892a8665396b1f42cf Mon Sep 17 00:00:00 2001 From: cgewecke Date: Fri, 14 Feb 2020 10:30:08 -0800 Subject: [PATCH] Allow base contract string constructor args with open curly braces (#479) --- lib/parse.js | 17 +++++++++++++++-- .../contracts/statements/interpolation.sol | 13 +++++++++++++ test/units/statements.js | 5 +++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/sources/solidity/contracts/statements/interpolation.sol diff --git a/lib/parse.js b/lib/parse.js index 146e184..3d561fb 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -55,8 +55,21 @@ parse.ContractOrLibraryStatement = function(contract, expression) { // We need to define a method to pass coverage hashes into at top of each contract. // This lets us get a fresh stack for the hash and avoid stack-too-deep errors. if (expression.kind !== 'interface'){ - const start = expression.range[0]; - const end = contract.instrumented.slice(expression.range[0]).indexOf('{') + 1; + let start = 0; + + // It's possible a base contract will have constructor string arg + // which contains an open curly brace. Skip ahead pass the bases... + if (expression.baseContracts && expression.baseContracts.length){ + for (let base of expression.baseContracts ){ + if (base.range[1] > start){ + start = base.range[1]; + } + } + } else { + start = expression.range[0]; + } + + const end = contract.instrumented.slice(start).indexOf('{') + 1; const loc = start + end;; (contract.injectionPoints[loc]) diff --git a/test/sources/solidity/contracts/statements/interpolation.sol b/test/sources/solidity/contracts/statements/interpolation.sol new file mode 100644 index 0000000..c0d4a08 --- /dev/null +++ b/test/sources/solidity/contracts/statements/interpolation.sol @@ -0,0 +1,13 @@ +pragma solidity ^0.5.0; + +contract Interpolated { + constructor(string memory a) public { + string memory b = a; + } +} + +contract Test is Interpolated("abc{defg}"){ + function a(uint x) public { + uint y = x; + } +} diff --git a/test/units/statements.js b/test/units/statements.js index cd0fbc7..808ec32 100644 --- a/test/units/statements.js +++ b/test/units/statements.js @@ -23,6 +23,11 @@ describe('generic statements', () => { util.report(info.solcOutput.errors); }) + it('should compile a base contract contructor with a string arg containing "{"', ()=> { + const info = util.instrumentAndCompile('statements/interpolation'); + util.report(info.solcOutput.errors); + }) + it('should instrument a single statement (first line of function)', () => { const info = util.instrumentAndCompile('statements/single'); util.report(info.solcOutput.errors);